code problem using switch

i can’t get this part of code to work right…it don’t work right with the switch statement, works with the if statements.with the switch statement only led is high is printed on the serial monitor…if i use the if statements alone it works right…what am i doing wrong here.

void button(){
 btnstate = digitalRead(led);
 Serial.println("ButtonState"); 
 switch(btnstate){  
   case 1:
   if(btnstate == HIGH){
      Serial.print(btnstate);
      Serial.println("= Led is on");
      Serial.println();
      }
      break;
    case 2:  
  if(btnstate == LOW){
      Serial.print(btnstate);
      Serial.println("= Led is off");
      Serial.println();
      }
      break;
 }
        
delay(2000);
}

oldPGMguy: i can't get this part of code to work right....it don't work right with the switch statement, works with the if statements.with the switch statement only led is high is printed on the serial monitor...if i use the if statements alone it works right...what am i doing wrong here.

What is the point of the switch statement? btnState will never be 2, so that case statement will never be hit.

 switch(btnstate){  
   case 1:
   if(btnstate == HIGH){
      Serial.print(btnstate);
      Serial.println("= Led is on");
      Serial.println();
      }
      break;
    case 2:  
  if(btnstate == LOW){

You are switching on btnstate. The cases need to match the possible values (HIGH and LOW). Yours do not.

then how do i fix it?...i don't understand why it works with just the if statements..just trying to learn how to use the switch statement

then how do i fix it?

switch(btnstate)
{
   case HIGH:
      // Do the HIGH thing
      break;
   case LOW:
      // Do the LOW thing
      break;
}

Using a switch for two cases results in larger code than an if/else statement would. Only when there is a large(r) number of cases does a switch statement make sense.

oldPGMguy:
then how do i fix it?..i don’t understand why it works with just the if statements…just trying to learn how to use the switch statement

Use one or the other, not both; it’s redundant.

Also, case 1: will fire when btnState is 1. You should be using case HIGH: and case LOW:, but a switch statement is overkill in this circumstance.

Shouldn't it be like this.

void button(){
 btnstate = digitalRead(led);
 Serial.println("ButtonState"); 
 switch(btnstate){  
   case 1:
   if(btnstate == digitalRead(led, HIGH);){
      Serial.print(btnstate);
      Serial.println("= Led is on");
      Serial.println();
      }
      break;
    case 2:  
  if(btnstate == digitalRead(led, LOW);){
      Serial.print(btnstate);
      Serial.println("= Led is off");
      Serial.println();
      }
      break;
 }
        
delay(2000);
}

for 2 cases, where one is the opposite of the other, switch is not too useful compared to if () { } else { }, but this is what you want, isn't it:

switch (btnstate) {
   case HIGH:
      Serial.println(" Led is on");
      break;
   case LOW:
      Serial.println(" Led is off");
      break;
}

mcreefer: Shouldn't it be like this.

No,

This

case 2:

will never happen because

btnstate = digitalRead(led);

assigns either HIGH or LOW to btnState, and neither HIGH, nor LOW are equal to 2.

thanks guys

A case statement is a way of doing and If statement type logic by making the code a bit simpler than a bunch of if statements.