The code i first wrote for this example is this. You see the leds blinks with each states correctly. But my code is bad for a better overall program, i wish i could use only one function call instead.
long time = 0;
long timeON1 = 0; long timeOFF1 = 0; int state1 = LOW;
long timeON2 = 0; long timeOFF2 = 0; int state2 = LOW;
long timeON3 = 0; long timeOFF3 = 0;int state3 = LOW;
void setup()
{
Serial.begin(38400);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
}
void loop()
{
flashled1(10, 15, 400);
flashled2(11, 150, 600);
flashled3(12, 400, 1000);
}
void flashled1(int ledpin, int ledON, int ledOFF) {
time = millis();
if (state1 == HIGH) {
if (time >= timeOFF1) {
state1 = LOW;
Serial.print(state1);
digitalWrite(ledpin, state1);
timeON1 = timeOFF1 + ledOFF;
}
}
else {
if (state1 == LOW){
if (time >= timeON1) {
timeON1 = millis();
state1 = HIGH;
Serial.print(state1);
digitalWrite(ledpin, state1);
timeOFF1 = timeON1 + ledON;
}
}
}
}
void flashled2(int ledpin, int ledON, int ledOFF) {
time = millis();
if (state2 == HIGH) {
if (time >= timeOFF2) {
state2 = LOW;
Serial.print(state2);
digitalWrite(ledpin, state2);
timeON2 = timeOFF2 + ledOFF;
}
}
else {
if (state2 == LOW){
if (time >= timeON2) {
timeON2 = millis();
state2 = HIGH;
Serial.print(state2);
digitalWrite(ledpin, state2);
timeOFF2 = timeON2 + ledON;
}
}
}
}
void flashled3(int ledpin, int ledON, int ledOFF) {
time = millis();
if (state3 == HIGH) {
if (time >= timeOFF3) {
state3 = LOW;
Serial.print(state3);
digitalWrite(ledpin, state3);
timeON3 = timeOFF3 + ledOFF;
}
}
else {
if (state3 == LOW){
if (time >= timeON3) {
timeON3 = millis();
state3 = HIGH;
Serial.print(state3);
digitalWrite(ledpin, state3);
timeOFF3 = timeON3 + ledON;
}
}
}
}