Noob switch case

Hello again, syntax problems driving me crackers tonight!

Simply put, I'm trying to toggle an LED on/off using a case statement.

To me, the main loop should be waiting for the button to be pressed before executing the switch, and at that point should toggle the LED to it's opposite state.
All that happens here is the LED stays on and the button appears to do nothing.
Could anyone point me in the right direction please?

int led1 = 2;
int btn1 = 8;

void setup() {
  pinMode(8, INPUT);
  pinMode(2, OUTPUT);
    digitalWrite(2, HIGH);
}

void loop() {
if (btn1 == HIGH)
  {
    switch (led1) 
    {
      case HIGH:
        digitalWrite(led1, LOW);
        break;
      case LOW:
        digitalWrite(led1, HIGH);
        break;
    }
  }
}

You are not reading the state of the button or the led for that matter

UKHeliBob:
You are not reading the state of the button...

Good point!! Added digitalRead(btn1); to the beginning of the main loop.

UKHeliBob:
...or the led for that matter

Isn't the state read during the comparisons made in the switch case?

No, the case statement just compares your led1 variable. It is equal 2 at the moment which doesn't match either case. If you want it to represent the state of the LED then you'll have to put led1 = LOW in the part that makes it LOW and led1 = HIGH in the part that makes it high.

Done some reading on Bounce, if else, and case statements, still none the wiser. Suspecting my breadboard now :sob: :sleeping:

I've stripped the code right back to the very first thing I'd like it to do.... register a push of the button! I'm sure I had something like this working lat week...

Anyway, the serial monitor has yet to output anything about the if statement.
If someone could confirm the code is correct then I'll go back to check the wiring.

Thanks again for your help guys, I'm already way out of my depth at this level :stuck_out_tongue_closed_eyes: :grinning:

void setup() {
  Serial.begin(9600);
  pinMode(8, INPUT);
  pinMode(2, OUTPUT);
    digitalWrite(2, HIGH);  
}

void loop() {
  Serial.println("begin main loop");
  digitalRead(8);
  delay(1000);
    if (8 == HIGH)
      {
        Serial.println("entering if statement");
        delay(1000);
      }
      Serial.println("finished main loop");
}

In the second line of your loop you read the pin but you don't save the value anywhere. Then in your if statement you are comparing the number 8 to HIGH which is defined in the core as 1. 8 will never ever ever equal 1. So the if statement will always be false.

You either need to save the result of digitalRead in a variable or you need to put the digitalRead in the if statement.

For crying out loud, go look at some of the examples that came with the IDE. They go over things like this and show you exactly how to do the simple stuff like this. Reading and studying those would be far more effective than just trying random shit and wondering why it doesn't work. Would probably save you a lot of time and frustration too if you took a few hours to look at some basic C++ tutorials on the web.

Fails to bother with the very first few examples that come with the IDE, immediately suspects breadboard.

:stuck_out_tongue_closed_eyes:
what is this i don't even

Try to add Serial.print(HIGH) in setup(). What does it print. Same for Serial.print(LOW). Will the condition (8 == HIGH) for the if ever be true?

You're comparing the pin number with a level.