Welcome, %1$s. Please login or register.

Login with username, password and session length
 

Author Topic: A simple running light and Knight Rider running light  (Read 2391 times)

Offline manu

  • Captain
  • ****
  • Posts: 273
A simple running light and Knight Rider running light
« on: December 18, 2014, 04:58:39 am »
Hi everybody,

I'd like to contribute the code for a simple running light, in the example with 6 leds.

This code uses some array operators and a modulo function. For those who have little experience, here's a little explanation.
an array is like a cupboard with several drawers. Each drawer has a number, which is used to access the content. The content is a variable, e.g. a number. For an array the type of content must be the same for all drawers, so all must be a number or all must be a string and so on.
The array is defined by

int runLEDPIN[6] = {  2,3,4,5,6,7};

so almost the same as a regular variable. This is an array with the content of integer numbers and it has 6 drawers. The adress to access one drawer starts with 0 and goes to size - 1, here 0 to 5. In the array I store to pin numbers of the running light leds, so these have to be changed only here, and can be in an arbitrary sequence, the don't have to be adjacent numbers, they can even be the same numbers. The pin for the active drawer is accessed with
runLEDPIN[number], then arduino opens the drawer with the number and takes the content to carry on. So it's not that hard.

The modulo function is a mathematical function whicht delivers the rest of a whole number division. Sounds complicated, but its not. Everybody has done that already
Example
7 modulo 3, calculates 7 devided by 3 = 2 leaves 1. the result is 1
8 modulo 3, calculates 8 devided by 3 = 2 leaves 2. the result is 2
9 modulo 3, calculates 8 devided by 3 = 2 leaves 0. the result is 0

So maybe you already see the advantage. For a divisor of 3, the possible results of the functions are 0,1, or 2. and nothing else. after 2 follows a 0, So this can be used to sift through the drawers of the array and start again, when you are through.

but now here is the code for the running light


/*

a running light for the LEDs connected to pins 2 to 7
 */

   
// Define the pins for the Leds in an array with size 6
int runLEDPIN[6] = {  2,3,4,5,6,7};

// last time, the running light switched to the next led
long runLEDPrevMil = 0;

// time intervall for the running light
long runLEDIntervall = 600; 

//number of the LED to be switched on, lets start with number 1 (with is connected to pin 2)
int runLEDNum = 0; 

void setup() {               

  // initialize the pins, access to the ingle pin is via array operations
  pinMode(runLEDPIN[0], OUTPUT);
  pinMode(runLEDPIN[1], OUTPUT);
  pinMode(runLEDPIN[2], OUTPUT);
  pinMode(runLEDPIN[3], OUTPUT);
  pinMode(runLEDPIN[4], OUTPUT);
  pinMode(runLEDPIN[5], OUTPUT);

// and switch them all off
  digitalWrite(runLEDPIN[0], LOW);
  digitalWrite(runLEDPIN[1], LOW);
  digitalWrite(runLEDPIN[2], LOW);
  digitalWrite(runLEDPIN[3], LOW);
  digitalWrite(runLEDPIN[4], LOW);
  digitalWrite(runLEDPIN[5], LOW);

 //switch on the first LED
 digitalWrite(runLEDPIN[runLEDNum], HIGH);
 
 
}

// the loop routine runs over and over again forever:
void loop() {
 
  //runLEDCurrMil is the clock where the actual time is stored
  long runLEDCurrMil  = millis();




//lets see if it is time to switch to the next led
  if (runLEDCurrMil - runLEDPrevMil > runLEDIntervall)
  {
    //yes, it's time, so we store the actual time for the next loop
    runLEDPrevMil = runLEDCurrMil;
   
    // switch off the active LED
    digitalWrite(runLEDPIN[runLEDNum], LOW);
   
    //increase the number of the active LED by 1. the modulo function %6 takes care that only values from 0 to 5 are resulting
    runLEDNum = ((runLEDNum +1 ) % 6);
   
   //switch on the new LED
    digitalWrite(runLEDPIN[runLEDNum], HIGH);
   
  }      


}
« Last Edit: February 10, 2015, 05:01:32 am by manu »

Offline manu

  • Captain
  • ****
  • Posts: 273
Re: A simple running light and Knight Rider running light
« Reply #1 on: February 10, 2015, 05:01:08 am »
The code above can be modified to do a Knight Rider running light effect very easily. the light point goes from left to right to left.

 Modify the line

// Define the pins for the Leds in an array with size 9
int runLEDPIN[9] = {  2,3,4,5,6,7,6,5,4,3};


and this line

 //increase the number of the active LED by 1. the modulo function % 9 takes care that only values from 0 to 8 are resulting
    runLEDNum = ((runLEDNum +1 ) % 9);

now the hole sequnce runs through the pin numbers 2,3,4,5,6,7,6,5,4,3 You don't add pin 2 at the end, because the sequence starts again as soon as it reaches the end. If you add the 2 at the end, then this led will light up twice as long as the rest which looks weird.
Adjust the speed here


// time intervall for the running light
long runLEDIntervall = 600; 

150ms is a good start value.

Critics, comments and corrections are always welcome.




Offline luminous

  • Admiral
  • *****
  • Posts: 527
Re: A simple running light and Knight Rider running light
« Reply #2 on: February 13, 2015, 08:58:07 am »
Thanks Manu very helpful and interesting.

Offline MarkW

  • Plastic Surgeon
  • Admiral
  • *****
  • Posts: 1347
    • MW ModelWerks
Re: A simple running light and Knight Rider running light
« Reply #3 on: February 27, 2015, 12:11:55 pm »
Thanks for the well documented code and explanation.
MarkW
"I can cross stab Hitler to death off the list of cool crap I thought I'd never do"
--Brock Sampson
NEW WEB ADDRESS

Offline MarkW

  • Plastic Surgeon
  • Admiral
  • *****
  • Posts: 1347
    • MW ModelWerks
Re: A simple running light and Knight Rider running light
« Reply #4 on: February 27, 2015, 12:14:52 pm »
One other quick thought...when initializing larger arrays, you can make a simple for loop rather than copying the code multiple times.
MarkW
"I can cross stab Hitler to death off the list of cool crap I thought I'd never do"
--Brock Sampson
NEW WEB ADDRESS

 




Enterprise © Bloc