Hello,
I have a problem with LED effects, the idea is, the same button need to control 3 LEDs, once I press the botton , it changes the leds effects.
The problem is, the botton work correctly just for the first time that I start the Arduino, the next time that botton is pressed I have some delay to change the led effect, unless I keep the botton pressed a litle bit more to change the effect just with 1 touch of the botton.
Link for video:
#define ledVm 7
#define ledAm 8
#define ledVd 9
#define pinBotton 11
int counter = 0;
int statusBotPrev = LOW;
void setup() {
Serial.begin(9600);
pinMode(ledVm, OUTPUT);
pinMode(ledAm, OUTPUT);
pinMode(ledVd, OUTPUT);
pinMode(pinBotton, INPUT_PULLUP);
}
void loop() {
int BottonState = !digitalRead(pinBotton);
//Serial.print(counter);
if( BottonState && !statusBotPrev ) counter++;
if(counter > 3 ) counter = 1;
if(counter == 1){
digitalWrite(ledVm, HIGH);
digitalWrite(ledAm, LOW);
digitalWrite(ledVd, LOW);
delay(200);
digitalWrite(ledVm, LOW);
digitalWrite(ledAm, HIGH);
digitalWrite(ledVd, LOW);
delay(200);
digitalWrite(ledVm, LOW);
digitalWrite(ledAm, LOW);
digitalWrite(ledVd, HIGH);
delay(200);
}
if(counter == 2) {
digitalWrite(ledVm, HIGH);
digitalWrite(ledAm, LOW);
digitalWrite(ledVd, HIGH);
delay(200);
digitalWrite(ledVm, LOW);
digitalWrite(ledAm, HIGH);
digitalWrite(ledVd, LOW);
delay(200);
}
if (counter == 3) {
digitalWrite(ledVm, HIGH);
digitalWrite(ledAm, HIGH);
digitalWrite(ledVd, HIGH);
delay(200);
digitalWrite(ledVm, LOW);
digitalWrite(ledAm, LOW);
digitalWrite(ledVd, LOW);
delay(200);
}
statusBotPrev = BottonState;
}