I have been looking for a code to satisfy my request however nothing has come up so I’m going to make a post about it.
I am in need of code that will check if a condition is still met 0.5 seconds after it has initially been met, and for it to only check once. Then if it is met, follow through with the code.
Here's an example
if(buttonStateM == 0 && buttonStateL == 0 && buttonStateR == 0) {
(wait 0.5 seconds);
check if (buttonStateM == 0 && buttonStateL == 0 && buttonStateR == 0) is still true
then turn led1 on
}
else led1 off
if (Pending == butState && (msec - msecPending) > Timeout)
the Blink without Delay concept is implemented the code above by repeatedly comparing the difference between the current time and the timestamp of some previous event is compared to a Timeout instead of using a delay()
Anyway, what @gcjr says is exactly right, of course. Instead of putting a delay() between the two tests, you should use two states. You might call one state AWAITING_INPUT and the other state might be AWAITING_CONFIRMATION.
//PSEUDOCODE
if (state == AWAITING_INPUT && input is active)
{
timestamp = millis() + 500
state = AWAITING_CONFIRMATION
}
else if (state == AWAITING_CONFIRMATION && millis() >= timestamp) //don't do this in real code
{
if (input is active)
{
do the important thing
}
else //input wasn't still active after 500ms, so abort
{
state = AWAITING_INPUT
}
}
Put that into a boolean function which you call repeatedly from loop(), and make that function return true ("do the important thing") when the requirements are met, else return false.
Where I've said "don't do this is real code" it's because the test will fail if millis() rolls over. I've shown it this way because it's easy to understand. @gcjr has shown the correct way to do it: compare intervals, not timestamps.
I haven't attempted to show you how to do this using your example code at the top, because I'm hoping you will understand the basic principle, rather than having someone write the code for you.