Hi, I can't seem to get case 3 and default case to work with the button press, any help would be grateful https://pastebin.com/XWS4sYD9
1st Press all LEDs should light
2nd Press All LEDs should blink together
3rd Press All LEDs should blink one by one
4th Press All LEDs should be off. Button press count should reset to zero.
I'm using tinkercad and I am new to arduino.
Any help is much appreciated.
int buttonState = 0;
//button is attached to pin 2
int button = 2;
//LEDs are attatched to pins 13,12,11
int ledPin1 = 13;
int ledPin2 = 12;
int ledPin3 = 11;
//interger to hold current state
int state = 0;
//int to hold last state
int old = 0;
//int to hold button state
int buttonPoll = 0;
void setup(){
pinMode(button,INPUT_PULLUP);
//set the LEDs pins as outputs
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
//initial LED state is set to off
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
}
void loop() {
buttonPoll = digitalRead(button);
if (buttonPoll == 1){
delay(50);
buttonPoll = digitalRead(button);
if (buttonPoll == 0){
state = old + 1;
}}
else{
delay(100);
}
switch (state) {
//All LEDs ON if button pressed once
case 1:
digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
old = state;
break;
//All LEDs should blink together if the button is pressed second time
case 2:
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
delay(500); // Wait for 500 millisecond(s)
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
old = state;
break;
//All LEDs should blink but one by one if the button is pressed third time
case 3:
digitalWrite(13, HIGH);
delay(800); // Wait for 800 millisecond(s)
digitalWrite(13, LOW);
delay(800); // Wait for 800 millisecond(s)
digitalWrite(12, HIGH);
delay(800); // Wait for 800 millisecond(s)
digitalWrite(12, LOW);
delay(800); // Wait for 800 millisecond(s)
digitalWrite(11, HIGH);
delay(800); // Wait for 800 millisecond(s)
digitalWrite(11, LOW);
delay(800); // Wait for 800 millisecond(s)
old = state;
break;
//All LEDs should be off if the button is pressed fourth time. The button press count should be reset to zero after the button has been pressed four times
default:
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
old = 0;
break;
}
}
Blights.ino (2.39 KB)
