const int timeBlink = 300;
class Blinker
{
int ledPin;
int ledState;
public:
Blinker(int pin, int state)
{
ledPin = pin;
pinMode(ledPin, OUTPUT);
ledState = state;
}
void turnOn()
{
this->ledState = HIGH;
digitalWrite(this->ledPin, this->ledState);
}
void turnOff()
{
this->ledState = LOW;
digitalWrite(this->ledPin, this->ledState);
}
void Switch(Blinker led_b)
{
this->ledState = LOW;
digitalWrite(this->ledPin, LOW);
led_b.turnOn();
}
int getLedState()
{
return this->ledState;
}
};
Blinker led1(11, HIGH);
Blinker led2(9, LOW);
Blinker led3(3, LOW);
unsigned long previousMillis = 0;
int state1;
int state2;
//int state3;
void setup() {
led1.turnOn();
}
void loop() {
unsigned long currentMillis = millis();
if( currentMillis - previousMillis >= timeBlink){
state1 = led1.getLedState();
state2 = led2.getLedState();
//state3 = led3.getLedState();
if(state1 == HIGH){
led1.Switch(led2);
}
else if (state2 == HIGH){
led2.Switch(led3);
}
else {
led3.Switch(led1);
}
previousMillis = currentMillis;
}
}
I tried like this and what happened is that the 1st led went On then Off, then the 2nd went On then 1st On again. And it stays like with led 1 and 2 On and the 3rd Off.
I explained it right above :
But maybe I was not clear, I want the led 1 to go On then Off, then the 2nd goes On then Off, then the 3rd On then Off, and then the 1st again.
So led1.Switch(led2); is supposed to turn Off the led1 and turn On the led2
Edit : Post with entire code