Stoping a loop with a changing variable

Hello, i'm trying to stop a loop with a variable that increases with a button and when the program enters in the dowhile it stops registering inputs (as is spected, i understand that) because it didn't finish the loop() function so it cannot get the input for the button. I think i can get it working with threading but im not that far in my knowledge and i would appreciate a little help here. I don't want the meat chewed but a point in the right path to solve this... Thank you.

include <EasyButton.h>
// Declare the counter
int counter = 0;
// Declare buttons
EasyButton button1(10);
EasyButton button2(11);
// Do while to keep bussy
void doWhile() {
Serial.println("Button pressed, starting...");
do { Serial.println("test"); delay(1000);
} while (counter <= 15);
}
// Limiter for the do while
void limiter() {
counter++;
}
// Setup
void setup() {
// Starting buttons
button1.begin();
button2.begin();
// Declaring functions
button1.onPressed(doWhile);
button2.onPressed(limiter);
}
void loop() {
// Reading button status
button1.read();
button2.read();
}```

If the code gets into this function

void doWhile()
{
    Serial.println("Button pressed, starting...");
    do {
        Serial.println("test");
        delay(1000);
    } while (counter <= 15);
}

it will never exit because the value of counter never changes. Consider reading button2 inside the do/while loop

1 Like

Yeah, never tought about that. Marked as solved, thank you.

OOC: My man, you're fast as f***. When i posted i used the code tags but it didn't get formatted so i had to edit and use the backticks, not even a minute and already got the advise to use them :rofl:

I just happened to spot your post as I was browsing

The easiest way to add the code tags is to right click in the code in the IDE and choose "Copy for forum". The code tags are added to the copy for you ready to paste the code into the forum

1 Like

Didn't notice that option but it's not on my right click but on the "edit" tab. My copy/paste fckup the indentation too so... another thing to learn. Thank you!

It was on the right click in IDE 1.x but has disappeared from IDE 2.x

I did not notice because I usually use the Ctrl+Shift+C keyboard shortcut

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.