Make sketch wait for button

Robin2:
You MUST get away from using the delay() function if you expect to be able to detect a button press. Use millis() to manage timing without blocking. See the demo Several Things at a Time

You ALSO need to use the concept of states. In simple terms, your system will be in one of 3 (?) states ...

  • waiting for something to happen
  • motion detected but button not pressed
  • motion detected and button pressed

You need a variable to keep track of that - perhaps something called systemState which can have values of 'w', 'm' and 'b'

When the motion is detected the state will be changed from 'w' to 'm' - but nothing will happen. When the button is pressed it will be changed from 'm' to 'b'. When it is 'b' stuff can happen and when the stuff is finished it should be changed back to 'w'. If the button is pressed when the state is 'w' the button press will be ignored.

...R

thank you for that but im still alittle confused on the changing of state if you can help alittle more.
would i use boolean conditions to change states for example
if (MOTION_PIN == HIGH && digitalRead(BUTTON_PIN) == LOW)
{
// systemstate = a
}

Would i use that as a void and call it back in the loop? im sorry if im not following correctly