I am programming an Arduino Uno sketch with some delays at various points throughout the sketch. I am wondering if it is possible to advance the sketch to the next line at some point midway through the delay so that I wouldn't always have to wait for the full delay to play out?
I was hoping I could press a button that could hop me to the next line of the code.
No, that's not possible.
The trick is not to use the delay function.
Please take a look at the blink without delay example in the IDE.
File|Examples|02.Digital| BlikeWithoutDelay <<< take a look. Delay() only stops the CPU from doing things. NOP's; NOP (code) - Wikipedia.
Replace
delay(5000);
with
unsigned long startTime = millis();
while (millis() - startTime < 5000 && digitalRead(puttonPin) == HIGH);
(that's assuming the input pin goes LOW when the button is pressed, which is normal)
Be the change you want to see in the world
best regards Stefan
with a back-translation to a normal worded functionality
there are two conditions that must be true to keep the microcontroller in the delaying loop:
- less than 5 seconds time have passed by
millis() - startTime < 5000
actual time - StartTime < 5000 milliseconds
- button is unpressed (button needs to be used with input_PULLUP and connected to ground)
As long as these two conditions are true the microcontroller stays inside the while-loop which does nothing else than checking the two conditions
as soon as
- button is pressed (which means IO-pin gets connected to ground and IO-pin detects "LOW"
or - five seconds have passed by
the looping ends
Be the change you want to see in the world
best regards Stefan