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$$4w ago
How can I exit this delay() if I need to? Thanks 🙏🏻
Marvee Amasi
Marvee Amasi4w 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 Amasi4w ago
I could help you with using flag variable if you want with your code @Camila_99$$
Boss lady
Boss lady4w 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$$4w ago
Hi! Thank you so much @Boss lady I'll give it a try now 👩‍💻
Want results from more Discord servers?
Add your server
More Posts
Why Certain Untracked Folders Are Not Stashed with git stash -u?Good day everyone, please I used git stash -u to stash changes. While some untracked files were stashow do I use AWS IoT Core to send telemetry data from an edge device@Middleware & OS Hey guys how do I use AWS IoT Core to send telemetry data from an edge device usinEnsuring Synchronization and Reliability of Custom Device Logs with AWS IoT CloudHey guys @IoT Cloud I need to ensure custom device logs, like ambient temperature data sent every 3 Looking for some help with getting Clock output on MCO1 (PA8) from LSELooking for some help with getting Clock output on MCO1 (PA8) from LSE here's a snippet of what I amconfigure an LVDS screen with 800x480 resolution (WVGA) on embedded Linux 3.14.52.@Middleware & OS Hey guys, I'm trying to configure an LVDS screen with 800x480 resolution (WVGA) onany other techniques you recommend to achieve a balance in my library design for I2C, SPI, and GPIOHi Everyone @Middleware & OS I'm developing a device library for various I2C, SPI, and GPIO sensors What is wrong with my code ?Hi everyone @Middleware & OS , please what is wrong with my code ? ```void SensorActions(void *parDoes anyone have experience with directly mapping FPGA memory to the microcontroller's memory space@Middleware & OS I'm redesigning an FPGA for faster SPI communication. The current design utilizes function to swap each 4 bytes of the ramteam, i have this function to swap each 4 bytes of the ram, void swapBytes(uint8_t *array, size_t leinterested in learning about constraint gaugesHi @aymen ammari , I'd be interested in learning about constraint gauges and how they might be