AFTER button pressed timer starts running, after 20 seconds timer stops.

Hi there, new here to this forum.

For a school project I need to turn a motor on after a button is pressed. The motor needs to stay on for 20 seconds. And then goes of. If the reset button is pressed the motor will go of as wel.

I can get the motor on and of with the buttons, but the timer is the hard part. I understand that if I use millis(); the timer starts running the moment the code is uploaded to the arduino, but I want the code to start running only after the button is pressed. I also want the timer to reset when the reset button is pressed.

Because my code is a bit messy now(I've been EXTREMLY frustrated today), I will not upload it for now.

Thanks in advance

rewallt:
Hi there, new here to this forum.

For a school project I need to turn a motor on after a button is pressed. The motor needs to stay on for 20 seconds. And then goes of. If the reset button is pressed the motor will go of as wel.

I can get the motor on and of with the buttons, but the timer is the hard part. I understand that if I use millis(); the timer starts running the moment the code is uploaded to the arduino, but I want the code to start running only after the button is pressed. I also want the timer to reset when the reset button is pressed.

Because my code is a bit messy now(I've been EXTREMLY frustrated today), I will not upload it for now.

Thanks in advance

Okay, good luck!

if (noCode == true)
  {
    Serial.print("Unable to provide help");
  }
1 Like

A clear reason why help is not working...

rewallt:
Thanks in advance

You are most welcome!


When you figure out how to proceed on this, you will need to define two little details:

  • You refer to "after a button is pressed". Does this mean the action should occur when the button is first pressed and then released?
  • Note that you should start off on a good foot by having the buttons wired between an Arduino input and ground, using INPUT_PULLUP as the mode on those pins. You can add external pull-ups if that is not sufficiently reliable such as if you are connecting the buttons by long wires which pick up interference or there is leakage in your connections or buttons.

See this info on Mills - it's also at the start of the forum - it will show you how to work with Mills

https://forum.arduino.cc/index.php?topic=223286.0

rewallt:
I understand that if I use millis(); the timer starts running the moment the code is uploaded to the arduino, but I want the code to start running only after the button is pressed. I also want the timer to reset when the reset button is pressed.

Imagine you want to boil an egg and you use a clock to time it. Are you going to set the clock to zero when you start? Or do you look on the clock, notice the time and occasionally check the clock to see if the required time has lapsed?

millis() based approaches work the same way as the latter. When you push the button, record the start time; next keep on checking the current (millis()) time till the 20 seconds have passed by subtracting the start time from the current (millis()) time.