Go Down

Topic: set up a timer (Read 561 times) previous topic - next topic

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.  :/

wildbill

Take a look at millis(). The blinkwithoutdelay example shows it to you in use

johnwasser


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.  :/


Code: [Select]

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
      }
   }

   
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Thanks very much. I'll take a look at both and work around it. Cheers

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy