Managing Relay Sequences with Non-Blocking Delays on Arduino Leonardo

Hello everyone 👋🏻, I have a small issue. I programmed some Leonardo boards to execute relay sequences in a sound and light installation. Of course, I used delay() in my sequences, and everything is triggered by multiple RF relays. The problem is that when an actor presses the wrong button on the remote control, it triggers another panel. I would like to add an RF relay that resets the panel, but I’m blocked by the delay() which prevents exiting the sequence. I have to wait for it to finish before I can interact with my sketch again. It shouldn’t be too complicated, but I need help with this. Does anyone have an idea? The code is something like this:
5 Replies
Camila_99$$
Camila_99$$6mo ago
How can I exit this delay() if I need to? Thanks 🙏🏻
Marvee Amasi
Marvee Amasi6mo ago
@Camila_99$$ hi consider to use a flag variable for this to work tou avoid getting stuck in the delay() and allow for resetting the panel during the sequence
Marvee Amasi
Marvee Amasi6mo ago
I could help you with using flag variable if you want with your code @Camila_99$$
Boss lady
Boss lady6mo ago
Use millis() instead of delay this should fix it 👇
#include <OneButton.h>

const byte buttonPin = 13;
OneButton button(buttonPin, true);
enum { SECURITE, SEQUENCE } etatCourant = SECURITE;

unsigned long startMillis;
bool timing = false;
const unsigned long delayDuration = 480000; // 8 minutes

void doubleclick() {
switch (etatCourant) {
case SECURITE:
etatCourant = SEQUENCE;
start();
startMillis = millis();
timing = true;
break;
case SEQUENCE:
// Optionally handle double-click in SEQUENCE state
break;
}
}

void setup() {
pinMode(buttonPin, INPUT_PULLUP);
button.attachDoubleClick(doubleclick);
// Other setup code...
}

void loop() {
button.tick();

if (timing && (millis() - startMillis >= delayDuration)) {
etatCourant = SECURITE;
timing = false;
}

// Other loop code...
}

void start() {
// Start sequence actions
}
#include <OneButton.h>

const byte buttonPin = 13;
OneButton button(buttonPin, true);
enum { SECURITE, SEQUENCE } etatCourant = SECURITE;

unsigned long startMillis;
bool timing = false;
const unsigned long delayDuration = 480000; // 8 minutes

void doubleclick() {
switch (etatCourant) {
case SECURITE:
etatCourant = SEQUENCE;
start();
startMillis = millis();
timing = true;
break;
case SEQUENCE:
// Optionally handle double-click in SEQUENCE state
break;
}
}

void setup() {
pinMode(buttonPin, INPUT_PULLUP);
button.attachDoubleClick(doubleclick);
// Other setup code...
}

void loop() {
button.tick();

if (timing && (millis() - startMillis >= delayDuration)) {
etatCourant = SECURITE;
timing = false;
}

// Other loop code...
}

void start() {
// Start sequence actions
}
Camila_99$$
Camila_99$$6mo ago
Hi! Thank you so much @Boss lady I'll give it a try now 👩‍💻
Want results from more Discord servers?
Add your server