Go Down

Topic: Function for multiple blinking led (Read 1 time) previous topic - next topic

sync


Kitep

#11
Jun 30, 2009, 02:30 am Last Edit: Jul 01, 2009, 01:42 am by Kitep Reason: 1
The problem with the original post is that the 3 variables - state, ledON, ledOFF - need to be in an array.  The way you have it, each of the 3 pins are using the same variables, and it's messing things up.  I put together a corrected version, and even tried it, so I know it works :)

Code: [Select]
long time = 0;
long timeON[14];    // I assume your pins go from 0-13
long timeOFF[14];
int state[14];

void setup()
{
int i;
for(i=0;i<=13;i++) {          //Edit: changed 14 to 13
  timeON[i] = 0;
  timeOFF[i] = 0;
  state[i] = LOW;
}
Serial.begin(38400);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
}

void loop()
{
flashled(10, 1000, 500);
flashled(11, 500, 1000);
flashled(12, 500, 500);
}

void flashled(int ledpin, int ledON, int ledOFF) {
time = millis();
if (state[ledpin] == HIGH) {
  if (time >= timeOFF[ledpin]) {
    state[ledpin] = LOW;
    //Serial.print(ledpin);Serial.print(" ");Serial.println(state[ledpin]);
    digitalWrite(ledpin, state[ledpin]);
    timeON[ledpin] = timeOFF[ledpin] + ledOFF;
  }
}
else {
  if (state[ledpin] == LOW){
    if (time >= timeON[ledpin]) {
      timeON[ledpin] = millis();      // you don't really need this line
      state[ledpin] = HIGH;
      //Serial.print(ledpin);Serial.print(" ");Serial.println(state[ledpin]);
      digitalWrite(ledpin, state[ledpin]);
      timeOFF[ledpin] = timeON[ledpin] + ledON;
    }
  }
}
}


Good luck!

sync

#12
Jun 30, 2009, 02:47 am Last Edit: Jun 30, 2009, 03:43 am by sync Reason: 1
Quote
The problem with the original post is that the 3 variables - state, ledON, ledOFF - need to be in an array.  The way you have it, each of the 3 pins are using the same variables, and it's messing things up.  I put together a corrected version, and even tried it, so I know it works


Arrrgh!!
Thanks, i was close from the beginning  :o


Kitep

#13
Jul 01, 2009, 01:43 am Last Edit: Jul 01, 2009, 01:44 am by Kitep Reason: 1
Warning - I made a mistake in my code.  It still works, but just cause I lucked out.

int i;
for(i=0;i<=13;i++) {          //error was in this line
  timeON = 0;
  timeOFF = 0;
  state = LOW;
}

Go Up