I'm trying to set up a project where a timer starts counting once a button is triggered and if my second button isn't triggered within 2 minutes I want to reset a boolean. What would be a good way to set up this timer.
I've been playing around with the arduino for a total of 3 days so go easy on me.
Take a look at millis(). The blinkwithoutdelay example shows it to you in use
sherkdavid:
I'm trying to set up a project where a timer starts counting once a button is triggered and if my second button isn't triggered within 2 minutes I want to reset a boolean. What would be a good way to set up this timer.
I've been playing around with the arduino for a total of 3 days so go easy on me.
const int Button1Pin = 2;Â // LOW means pressed
const int Button2Pin = 3;Â // LOW means pressed
boolean MYBOOLEAN = true;
void setup()
  {
  pinmode(Buton1Pin, INPUT);
  digitalWrite(Button1Pin, HIGH); // Enable the internal pull-up resistor
  pinmode(Buton2Pin, INPUT);
  digitalWrite(Button2Pin, HIGH); // Enable the internal pull-up resistor
  }
void loop()
  {
  static boolean Button1WasPressed = false;
  static unsigned long Button1PressTime;
 if (digitalRead(Button1Pin) == LOW && !Button1WasPressed) // Button 1 pressed for the first time
   {
   Button1WasPressed = true;
   Button1PressTime = millis();
   }
 if (Button1WasPressed && ((millis() - Button1PressTime) > 120000)) // More than 2 min. since Button 1 press
   {
   MYBOOLEAN = false;
   Button1WasPressed = false; // Reset to initial state, waiting for Button 1 to be pressed
   }
  if (Button1WasPressed && digitalRead(Button2Pin) == LOW) // Button 2 pressed before timeout
   {
   Button1WasPressed = false; // Reset to initial state, waiting for Button 1 to be pressed
   }
 }
Thanks very much. I'll take a look at both and work around it. Cheers