i need help using led with switch case and button

Hi,

i want to turn on led when i press button once

when i press it again i want to turn it off.

when i press it third led should blink

and when press fourth it should turn back to start

Thanks for help :slight_smile:

-Problem is in case 3

const int led = 2;
const int button = 3;
int buttonwalue=0;
int a;
int lastwalue=0;


void setup() {

a = 1;
pinMode(led ,OUTPUT);
pinMode(button ,INPUT);
Serial.begin(9600);
}

void loop() {
  buttonwalue = digitalRead(button);
Serial.print(buttonwalue);

switch(a) {

case 1:
digitalWrite(led, HIGH);
if(buttonwalue != lastwalue){
if(buttonwalue == HIGH){
  a=a+1;
}
delay(50);
}
lastwalue = buttonwalue;
break;

case 2:
digitalWrite(led, LOW);
if(buttonwalue != lastwalue){
if(buttonwalue == HIGH){
  a=a+1;
}
delay(50);
}
lastwalue = buttonwalue;
break;

case 3:                                                   
 while(buttonwalue == LOW){                  
digitalWrite(led, HIGH);                          
delay(1000);                                      
digitalWrite(led, LOW);                      
delay(1000);                                      
 }                                                     
if(buttonwalue != lastwalue){            
if(buttonwalue == HIGH){                                              
  a=1;                                                                             
delay(50);                                                                        
}                                                    
lastwalue = buttonwalue;                   
break;                                              

 

 }
}

Problem is in case 3

No, the problem is that you are mixing code to deal with the number of times the switch has been pressed with code to count the number of times the switch has been pressed.

Look at the state change detection example, again if you've looked at it before. It does everything needed to deal with the change, including counting and constraining, and THEN it does what it is suppose to do, based on the number of times the switch was pressed.

 while(buttonwalue == LOW){                 
digitalWrite(led, HIGH);                         
delay(1000);                                     
digitalWrite(led, LOW);                     
delay(1000);                                     
 }

The value in buttonwalue (can't spell, eh?) does not change during the time that the while loop is executing, so, once the while loop starts, it will never end.

-Problem is in case 3

As written case 3 depends on the button remaining pressed. Eliminate the delay()s by using millis() for timing whilst in case 3. Save the start time when the period ends and change state of the LED

See Using millis() for timing. A beginners guide and look at the BlinkWithoutDelay example

UKHeliBob:
As written case 3 depends on the button remaining pressed. Eliminate the delay()s by using millis() for timing whilst in case 3. Save the start time when the period ends and change state of the LED

See Using millis() for timing. A beginners guide and look at the BlinkWithoutDelay example

THANK YOU :smiley: