Go Down

Topic: simple LED chaser without delay (Read 941 times) previous topic - next topic

pm1066

Standard semi newbie disclaimer....

I'm trying to do something relatively simple, and getting stuck.  My goal is to have a simple 1-2-3-1-2-3-1-2-3... chaser of 3 LEDs.  I can do this using a delay function easily, but want to use a millis() type solution like Blink Without Delay.  So, in short;

All LEDs off
Turn on LED 1,
check to see if fixed interval passed
if true, turn off LED 1, turn LED 2 on
Check timer
if true, turn off LED 1, 2, turn LED 3  on

My current code example only turns on the fisrt LED constantly:
Code: [Select]
/*This sketch borrows the array setup from the 5 random blink
without sketch


*/
const int numberOfBlades = 3;
int ledPin[] = { 6, 7, 9}; // LED pins to use.
int ledState[3];
long interval = 83;
long previousMillis = 0;        // will store last time LED was updated
int i = 0;

void setup() {
for(int i = 0; i<numberOfBlades; i++){
   pinMode(ledPin[i],OUTPUT);
   ledState[i] = LOW;
   digitalWrite(ledPin[i], LOW); // all LEDs off
}

}

void loop() {
  unsigned long currentMillis = millis();   
  if(currentMillis - previousMillis > interval) {             // Arduino run time - changeTime value greater then ledDelay variable? Yes then
    previousMillis = currentMillis;
    changeLED();       // Go to changeLED routine.
    i = i++ % 2;       //i is either 0, 1 or 2
  }

 
void changeLED(){
for(; ;){
  if (i % 0 == 0){
    ledState[0] = HIGH;
    ledState[1] = LOW;   
    ledState[2] = LOW; 
    digitalWrite(ledPin[i],ledState[i]);
  }
  else if (i % 1 == 0){
    ledState[0] = LOW;
    ledState[1] = HIGH;   
    ledState[2] = LOW; 
    digitalWrite(ledPin[i],ledState[i]);
  }
  else (i % 2 == 0);  {
    ledState[0] = LOW;
    ledState[1] = LOW;   
    ledState[2] = HIGH; 
    digitalWrite(ledPin[i],ledState[i]);
  }

}
}


Any help appreciated!

Jack Christensen

for (;;) { } is an infinite loop. So the first time that the changeLed() function is called, it just executes the for loop forever, i never gets changed, etc.

Maybe just taking the for loop out is all that is needed.

Oh one more thing, should  i = i++ % 2;  instead be  i = i++ % 3;  ?
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

marco_c

#2
Nov 24, 2013, 01:51 am Last Edit: Nov 24, 2013, 04:38 am by marco_c Reason: 1
Check out my MultiBlink sketch in the code repository (linked below) for ideas on how this is done. You can also use the sketch directly if you want.
Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

pm1066


for (;;) { } is an infinite loop. So the first time that the changeLed() function is called, it just executes the for loop forever, i never gets changed, etc.

Maybe just taking the for loop out is all that is needed.

Oh one more thing, should  i = i++ % 2;  instead be  i = i++ % 3;  ?

Removing the  for loop helped somewhat, now all three LEDs turn on.  The problem is they turn on very solely, and semi randomly blink.

That tells  me that the lighting isn't working on the 0.083 seconds cycle.

Jack Christensen

#4
Nov 24, 2013, 04:42 am Last Edit: Nov 24, 2013, 04:48 am by Jack Christensen Reason: 1
Couple more things.

Code: [Select]
i = i++ % 3;

is probably not doing what you want. It's an odd construct, the right side modifies i  by incrementing it then of course the assignment changes i  too. I'd have to research what the expected behavior would be. It might be better to break it down into two statements. I might just increment i then test if it's 3 or greater, in which case set it back to 0. Simpler, easier to read and understand.

The if clauses in changeLed() are incorrect. The modulo operator is not needed. Just test for 0, 1, and 2. Simpler.
Each if only does one digitalWrite... at least two will always be needed, one to turn off the current LED, one to turn on the next.

The last else is missing an if. Either that, or the condition clause in parens isn't needed.

Keep at it!
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Go Up