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);
}
}
}