Push Button Millis Timer Delay

/*
Simple example of using elapsedMillis library

This example does othing more than blinking a LED… but the difference
here to just using delay(1000) to control the delay between blinks is that
you can do more in the loop - like get user input, check buttons, get sensor
input and other tasks that take time - while you wait for the next time the
LED is to change state. All you need to do is add it before or after the
‘if’ statement that controls the check of the time elapsed.

This example code is in the public domain.
*/

#include <elapsedMillis.h>

elapsedMillis timeElapsed; //declare global if you don’t want it reset every time loop runs

// Pin 13 has an LED connected on most Arduino boards.
int led = 13;
int button = 2;

// delay in milliseconds between blinks of the LED
unsigned int interval = 5000;

// state of the LED = LOW is off, HIGH is on
boolean ledState = HIGH;

void setup()
{
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
pinMode(button, INPUT);
}

void loop()
{
if (timeElapsed > interval)

digitalWrite(led, LOW );
{

if (digitalRead(button == HIGH ))
timeElapsed = 0; // reset the counter to 0 so the counting starts over…
}
}

i am attempting to get a timer to change the output pin state to low after 5 seconds, i want this to happen every time the
pushbutton is pressed,

PLEASE HELP i am a NOOB!

You have to read the "Blink without delay example"... Just try to understand how it works..

zaxarias:
You have to read the "Blink without delay example"... Just try to understand how it works..
http://arduino.cc/en/Tutorial/BlinkWithoutDelay

and you really do not need that elapsedMillis library

Has anyone done this before??? is it as simple as a few changes??? or a whole new approach needed??

I feel like I just answered this question:

This works for me.
The variables are as follows

mDoWtime -This is a long its set to 0 to start (int mDoWtime = 0;)
mDb - This is a motion detector on an in put you can use a button or whatever
fanMTout - This is a software varable output for me but fanMTout = X; can be changed to
something like digitalWrite(LEDpin, X)

if(mDoWtime == 0){
if(digitalRead(mDb) == HIGH){
  mDoWtime = millis() + (5*60*1000);
}}
else{
fanMTout = 1;
}
  if(millis() >= mDoWtime){
    mDoWtime = 0; 
   fanMTout = 0;
  }

Thanks For the help guys!!

I'll give this to you but you have to find the error to make it work!

int led = 13;
int button = 2;
unsigned long interval = 5000;
unsigned long startTime;
byte lastPress;
//
void setup() 
{                
  pinMode(led, OUTPUT);
  pinMode(button, INPUT);  
}
//
void loop()
{
  byte pressed = digitalRead(button);
  if (pressed)
  {
    if (pressed != lastPress)
    {
      digitalWrite(led, HIGH);
      startTime = millis();
    }
  }
  lastPress = pressed;
  if (digitalRead(led))
  {
    if (millis() - startTime >= interval)
    {
      digitalWrite(led, LOW);
    }
  }
}

const int led = 13;
const int button = 2;

Thank you my friend!!

And thank you, both. I was going round in circles trying to get just something like this to work. This will do for now while I figure out the logic!

Have you looked at the demo several things at a time ?

...R

Hello again,

I have been fiddling around with Bulldog's code and just found another use for it - a retriggerable monostable. Probably been done many times before, but new to me. I needed to monitor a gas boiler ignition LED which looked like a solid ON but turned out to be a 50-Hz squarewave. I used a CMOS 74HC123 solution to get a single squarewave to make timing easier, but with the code version, just keep pressing the pushbutton to retrigger the output - so now going to try using the boiler 50-Hz output to retrigger the output and get a solid one shot.

Learn something new every day

PS A bit of help from the likes of Bulldog can get newcomers out of a hole - sniping comments might mean well - God helps those who help themselves and all that- but can seriously undermine someone just starting out. After all, we are all in the same club.