Late night switch case confusion

Howdy fine folks in Arduino-land.

So I have been doing some experimenting with switch statements this evening. I have this handy little sketch uploaded into an ESP32. It has 3 buttons and 3 LED's. When I press the first button the first LED comes on (and the others go off if any are on)..and vise versa for the other switches. I'll post my code below, but first a few questions. My questions are also in the comments in the code.

In my switch statement if I include the break; after any but the last case before the default. Things just don't work. I was under the assumption you wanted a break after each case to prevent 'falling through' the switch.

Also I'm repeating a lot of digitalWrite code in my cases. I was wondering if instead of byte LEDStates; if I'd be better using enum with the 3 different states the LED's can be in as a group. Then in my loop after the switch use an if block to turn off and on the led's based on what returns from the switch.

Lastly in general does it look like I'm on the right track for this program in terms of being efficient and with good programming standards?

Lastly I know everyone and their brother is going to be barking about bracketing and autoformat. Spare me. I like my brackets this way. It makes sense to me the way they are and the compiler is happy...so I'm happy.

#include <Arduino.h>
#include <ezButton.h>

#define redLEDPin 13
#define greenLEDPin 12
#define blueLEDPin 14

ezButton buttonRed(2);
ezButton buttonGreen(4);
ezButton buttonBlue(5);

//enum LEDStates { RED, GREEN, BLUE }; Next bear to tackle. How to use this thing!
byte LEDStates; // I know my switch statement is this but I'm not sure why I need it. I'm not really using this variable for anything.

bool redLEDOn = false;
bool greenLEDOn = false;
bool blueLEDOn = false;

void setup() {
pinMode(redLEDPin, OUTPUT);
pinMode(greenLEDPin, OUTPUT);
pinMode(blueLEDPin, OUTPUT);
buttonRed.setDebounceTime(50);
}

void loop() {
buttonRed.loop();
buttonGreen.loop();
buttonBlue.loop();

switch (LEDStates) {
  case 0:
    if (buttonRed.isPressed()) {
      if (redLEDOn == false) {
        digitalWrite(redLEDPin, HIGH);
        digitalWrite(greenLEDPin, LOW);
        digitalWrite(blueLEDPin, LOW);
        redLEDOn = true;
        greenLEDOn = false;
        blueLEDOn = false;
      } else {
        digitalWrite(redLEDPin, LOW);
        digitalWrite(greenLEDPin, LOW);
        digitalWrite(blueLEDPin, LOW);
        redLEDOn = false;
      }
  }
  // break; I don't know why it's failing with the break statement. I thought those were needed to prevent falling through the switch?

  case 1:
    if (buttonGreen.isPressed()) {
      if (greenLEDOn == false) {
        digitalWrite(redLEDPin, LOW);
        digitalWrite(greenLEDPin, HIGH);
        digitalWrite(blueLEDPin, LOW);
        redLEDOn = false;
        greenLEDOn = true;
        blueLEDOn = false;
      } else {
        digitalWrite(redLEDPin, LOW);
        digitalWrite(greenLEDPin, LOW);
        digitalWrite(blueLEDPin, LOW);
        greenLEDOn = false;
      }
  }
  // break;

    case 2:
    if (buttonBlue.isPressed()) {
      if (blueLEDOn == false) {
        digitalWrite(redLEDPin, LOW);
        digitalWrite(greenLEDPin, LOW);
        digitalWrite(blueLEDPin, HIGH);
        blueLEDOn = true;
        greenLEDOn = false;
        redLEDOn = false;
      } else {
        digitalWrite(redLEDPin, LOW);
        digitalWrite(greenLEDPin, LOW);
        digitalWrite(blueLEDPin, LOW);
        blueLEDOn = false;
      }
  }
  break; // But I need this break here or program doesn't work
  
  default: {
    digitalWrite(redLEDPin, LOW);
    digitalWrite(greenLEDPin, LOW);
    digitalWrite(blueLEDPin, LOW);  
    }
  }
}

You can have your brackets as you like it.
But do not expect me to wrestle through...
And post your full sketch...
LEDstates must be set somewhere...

Work through a simple sw case first.

This is my entire sketch. Did you want me to paste the code from ezButton.h, Arduino.h, and platformio.ini or something??

I did set LEDStates as a global type byte on line 13. I just don't know how to extract that from the switch statement. As I said the program works as is.

As for the brackets if I make them how you like them then the next guy whines. I'm not asking for a bracketing tutorial.

Hello SlimPikkins

What is the task of your sketch in real life?

I got that one working on my own (and I'm not even close to being a good programmer lol. With that I think what I have is pretty simple? :smiley:

If you're saying there's a simpler way to do what I'm doing I'm all ears. Also I'm seriously confused on using the enum thing.

If I do this up at the top...

enum LEDStates { RED, GREEN, BLUE, OFF };

and include this in my cases....

LEDStates = 0;

The compiler shoots darts at me.

But you never assign it a value.
It needs to be set to 0, 1, or 2 for the case statements to work.

My end goal is to have a RGB led matrix and when I press a button it will play animations I made stored in arrays. When I press a different button (or maybe the same button) I'll probably switch it up to only use one button, but I found the ezButton library and figured I'd learn the in's and out's of that with multi-buttons. I'm just using LED's for now to nail the concepts of what I want to do.

Hello SlimPikkins

Take a view to this reference for coding to gain the knowledge:

https://www.learncpp.com/

Please describe what you need to achieve.

LEDStates is initialised to 0. So your switch/case will never ever do something else than case 0 if the break for case 0 is in place.

If you press the red button, there will not be an issue and the red LED will toggle (as far as I can see).

If the green button is pressed with the break in place in case 0, you will never get to case 1 because you don't change the LEDStates variable; removing the break from case 0 solves that issue.

LEDstates is an input used by the switch case.
A switch case does not extract things.
Somewhere you need to change the value of ledstates in order to make the switch case take appropriate actions.
I do not see where you set LEDstates based on buttons or whatever...

I've looked over the reference you linked already. It's kind of lacking though because it doesn't show the whole code... like the part where the var is declared. At any rate you're correct I didn't set a value, but the program still works fine. Not too sure why but it certainty does.

I don't. That's one of my confusion points. I don't understand why 1) I'd need to set it. The switch is going through the cases seeing which one is true and if not doing the defualt. and 2) It's working as is. How so if I did it wrong?

No man it works perfectly as is. I could rig it up on Wokwi if you want me to so you could see.

Ok. So the buttons are read inside the switch...
You do not need the switch at all...

With that break in case 0 in place? I doubt it as it will only execute case 0 and never anything else.

As I tried to explain (I obviously failed), because of the fall through it will go through all cases and handle all buttons. And you don't need a switch/case for that.

It executes all cases as the break; is after //

So basically the switch does nothing but confuse us and op.

Read up and do some basic switch case examples to see how it is supposed to be written.

As I said "with the break in place" meaning "not commented". OP tries to figure out why the it does not work with the break in place.

Dude I commented them out because with them in there it did not work. As I said in my OP as well as the comments themselves. Did you just stop reading after the //? I'm not sure if you're trying to be snide there or what. If so kindly make an exit. I'm trying to learn here. Also newsflash. You too were once new at this stuff.

In regards to your previous response about not needing the switch. I'm aware of that. I know I can just use if's. I wanted to learn how to use switches though. But all my cases have all the if's I would use anyway. So yes I see it's rather pointless as is. But my point is the learning experience.

I'm also experimenting with my LEDStates variable by changing it with each case to the number of the case. It doesn't make any difference when I do though.