How do i code a timer system

I want to use my button to make a timer system where the timer only counts down while the button is being held down and only activates the thing that the button is powering (say a motor) when the timer is done

Welcome to the forums. Have a look at the State Change Detection example in the IDE (File->examples->02.digital->StateChangeDetection). This will show you how to detect when a button gets pressed/released.

When you detect a button press, make note of the time. Then, when the elapsed amount of time has expired [millis() - startTime >= waitTime], turn on your motor or whatever. When the button is released, turn it off. You can also look at several things at the same time to see how to structure your code

Hello,
post your sketch you have designed until now.

const byte ButtonPin = 2;
const unsigned long DebounceTime = 10;

unsigned long TotalPressTime = 0;

const unsigned long TotalTimeLimit = 10000;  // Button must be pressed 10 seconds

boolean ButtonWasPressed = false;
unsigned long ButtonStateChangeTime = 0; // Debounce timer
unsigned long ButtonPressTime = 0;

void setup()
{
  pinMode (ButtonPin, INPUT_PULLUP);  // Button between Pin and Ground
}

void loop()
{
  unsigned long currentTime = millis();

  boolean buttonIsPressed = digitalRead(ButtonPin) == LOW;  // Active LOW

  // Check for button state change and do debounce
  if (buttonIsPressed != ButtonWasPressed &&
      currentTime  -  ButtonStateChangeTime > DebounceTime)
  {
    // Button state has changed
    ButtonWasPressed = buttonIsPressed;
    ButtonStateChangeTime = currentTime;

    if (ButtonWasPressed)
    {
      // Button was just pressed
      ButtonPressTime = millis();
    }
    else
    {
      // Button was just released
      unsigned long thisPress = millis() - ButtonPressTime;
      TotalPressTime += thisPress;
    }
  }

  if (ButtonWasPressed)
  {
    // Button is being held down
    unsigned long thisPress = millis() - ButtonPressTime;
    if (TotalPressTime + thisPress >= TotalTimeLimit)
    {
      // Do thing here
    }
  }
}

thank you it helped out a lot

I think you should reset TotalPressTime when the button is released, not keep adding to it. It never gets reset so only the first long button press will do the right thing.

@nmaniscool What should happen if the button is released before the timer has counted down to zero ?

Is there any need to detect the state change based on what @nmaniscool has said ?

My guess at what the OP intended was that the timer could be across multiple button presses. Feel free to implement a different design. You are welcome to use my sketch as a starting point if that would be of use.

how would I code it to reset total press time?

When the button is released, set TotalPressTIme back to 0.

it just turns off immediately while I am still holding the button down, instead of resetting a certain amount of time after I let go (which is what I want to happen).

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.