I'm beginning to do kind of sophisticated things (well.. for my entry level skill) and I have to control a display with 3 LED and 4/5 types of events and status.
I'm using the BlinkWithoutDelay example duplicating all the variables for different status/events but the code is really messy, full of puzzling nested if
statements.
How to simplify the things? I don't know what to choose, because OOP could be a choice but I think it will hurt the low-memory thing inside the Arduino Uno/Leonardo. Interrupts could be another choice but I think they are like threads, and following the KISS principle I'd like to stay away from them.
So, can someone advice me some article/tutorial/code-to-read/whatever to understand how to use a lot of LEDs and keep a lot internal status without screw my brain?
EDIT:
It is a little bit hard to reproduce a simple skelton of my messy code, but it looks like this (in pseudocode maybe)
void loop() {
if(state==0) handleState0();
if(state==1) handleState1();
if(state==2) handleState2();
if(state==3) handleState3();
readButtons();
readSensors();
…
}
// this is a block of variables that I have to copy-and-paste
// every time I have to blink a led (hoping that there is
// nothing blocking that can skrew up everything)
unsigned long previousMillis_1 = 0;
const long interval_1 = 1000;
int ledState_1 = LOW;
void handleState1() {
// Green led blinking, yellow is fixed
// this is a copy-and-paste code hard to maintain,
// easily broken from any blocking call (like also I2C)
// and hard to refactor or isolate
unsigned long currentMillis_1 = millis();
if(currentMillis_1 - previousMillis_1 >= interval_1) {
previousMillis_1 = currentMillis_1;
if (ledState_1 == LOW) ledState_1 = HIGH;
else ledState_1 = LOW;
digitalWrite(pinGreen, ledState_1);
digitalWrite(pinYellow, LOW);
}
unsigned long previousMillis_2 = 0;
const long interval_2 = 1000;
int ledState_2 = LOW;
void handleState2() {
// Yellow led blinking, green is fixed
// same as before
unsigned long currentMillis_2 = millis();
if(currentMillis_2 - previousMillis_2 >= interval_2) {
previousMillis_2 = currentMillis_2;
if (ledState_2 == LOW) ledState_2 = HIGH;
else ledState_2 = LOW;
digitalWrite(pinYellow, ledState_2);
digitalWrite(pinGreen, LOW);
}
void handleButtonPressed() {
// this skrew everything and because is blocking
// and should be inserted in the main loop with
// some global variables: active/disable, counter,
// and non-blocking vars like previousMills, interval, etc etc
Serial.println("\ndisplayAccessDenied");
for(int i=0; i<3; i++) {
digitalWrite(pinRed, LOW);
delay(100);
digitalWrite(pinRed, HIGH);
delay(100);
} digitalWrite(pinRed, LOW);
}
I feel a little bit lost but all the Arduino example arounds are super simple and to make a middle-level LED interface I have to screw my minds with something that is not working as 100% and other than Arduino I find only super-hard articles on microprocessors with low-level C code. But the pasted example is not working at 100% AND super hard to read, maintain, etc.
interrupt are not the devil
- see Interrupts