Time

I am a very fresh nubby How can I ad time to an output using a button?

Thanks for your help..

How can I ad time to an output using a button

What sort of output? What sort of time?

Digital output, 30 minutes.

Can you explain what you mean by “add time to an output” - or in this case you want to “add 30 minutes of time to a digital output”.

This doesn’t make sense to us.

Are you wanting to know how to set a digital output HIGH or LOW for 30 minutes, then revert back to the opposite level? Or are you wanting something else? Please explain -exactly- what it is you are trying to accomplish, as best as you can (I understand that english is likely not your primary language - at least I hope it isn’t; if it is, someone needs to slap your teacher(s)…).

:slight_smile:

Sorry,

I want the normal state of the output to be LOW, and every time the button the state change to HIGH for 30 minutes, and if the button is pressed again it ad´s 30 minutes to that time.

Sorry again, as you said English is not my primary language and is very rusty since I rarely use it.

Thanks again

Hi Daniel_melo

I this code any good for you?

// Money_timer
// Money tricker (buttonPin) is connected to pin 2. The pin is protected with the internal pull-up resistor
// There is a debounce time before the buttonPin accept another input.
// Due to problems around millis() overflow (if stopTime did a roll-over before millis() the
// relay would go off) the comparison betweeen millis() and stopTime is only made 2 sec
// before and 2 sec after they are equal thus minimising premeture turnoff to max 2 sec


byte buttonPin = 2;
byte relayPin = 13;
unsigned long relayTime = 180000; // Relay open time (in milli sec)
int debounceTime = 200; //ms

unsigned long stopTime, now;
boolean thisReading, lastReading;

void setup(){
  pinMode(relayPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  digitalWrite(buttonPin, HIGH); // Enable pull-up - don't fry the pin!
}

void loop(){
  // Detecting coin input
  thisReading = digitalRead(buttonPin);
  delay(1);
  if ((thisReading != lastReading) and thisReading == LOW){ // A chenge was made on buttonPin
    if (digitalRead(relayPin) == HIGH) stopTime = stopTime + relayTime; //We are running and the button is pressed
    if (digitalRead(relayPin) == LOW){ //Button is pressed for the first time
      stopTime = millis() + relayTime;
      digitalWrite(relayPin, HIGH); // Time to start
    }
  delay(debounceTime);
  }
  lastReading = thisReading;

  // Check to see if relay should be off
  now = millis();
  if (((stopTime - now < 2000) or (stopTime - now > 2000)) and (now > stopTime)){  // minimal Roll-over error
    digitalWrite(relayPin,LOW);
  }
}

-Fletcher

Thanks, that was perfect…