Led laststate toggle with delay

Hi there

Need some help pls

With a toggle led and button, how to add a delay millis() at the end where:
if (relayState == 0) {
digitalWrite(pin2, relayState);

So if the button was pressed, and led comes on, begin timer for 5 seconds and switch back off until next press

int relayState = HIGH;
int lastButtonState;
int currentButtonState2;
const byte buttonPin = 8;
const byte buttonpin2 = 9;
const byte pin2 = 2;
const byte pin3 = 2;
void setup() {

Serial.begin(9600);
  while (!Serial);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(buttonpin2, INPUT_PULLUP);
  pinMode(pin2, OUTPUT);
  pinMode(pin3, OUTPUT);
  
  digitalWrite(pin2, HIGH);
 currentButtonState2 = digitalRead(buttonpin2);

}

void loop() {
 //// Button Overide
  
    lastButtonState    = currentButtonState2;      // save the last state
  currentButtonState2 = digitalRead(buttonpin2); // read new state
  if(lastButtonState == HIGH && currentButtonState2 == LOW) {
    Serial.println("Button is pressed");
    // toggle state of relay
    relayState = !relayState;
  }
  if (relayState == 0) {
        digitalWrite(pin2, relayState);
  }

}

You can do something like this code fragment:

// if button press detected somehow,  begin a timer

uint32_t buttonPressAtMs = millis() ;   


// sometime later, test if the timer has expired (5 seconds)

if ( millis() - buttonPressAtMs > 5000 ) {
   // if the relay is still on, switch it off here
}

This looks rather odd:

If you add another, it could even be Irish - "to be sure, to be sure, to be sure ". :rofl:

1 Like

that's an example for a retriggerable monostable multivibrator (Monotflop). To be precise: two multivibrators with one button and one LED each.

The idea behind is that one button and one LED are building a logical unit. Therefore the variables are held in a struct. Each unit (group) needs a timestamp(millis) for timekeeping, therefore there is a member variable to keep the previousMillis.

As you want several button-LED units ... it uses an array of struct.

The sketch can be adopted easily for more button-LED combinations.

modify the pin definitions to your need:

/*
  by noiasca
  http://werner.rothschopf.net
*/

const byte ontime = 5;   // on time in seconds

struct Groups {
  const byte buttonPin;
  const byte outputPin;
  uint32_t previousMillis;
};

Groups group[] {
  {8, 2, 0},  // index 0: button Pin, outputPin, previousMillis
  {9, 3, 0}   // index 1: check if you really want to use this output pin ...
};

void tickButtonLed()
{
  for (auto & i : group)
  {
    if (digitalRead(i.buttonPin) == LOW) {
      digitalWrite(i.outputPin, HIGH);
      i.previousMillis = millis();
    }
    if (millis() - i.previousMillis > ontime * 1000UL && digitalRead(i.outputPin) == HIGH)
    {
      digitalWrite(i.outputPin, LOW);
    }
  }
}

void setup() {
  for (auto & i : group)
  {
    pinMode(i.buttonPin, INPUT_PULLUP);
    pinMode(i.outputPin, OUTPUT);
  }
}

void loop() {
  tickButtonLed();
}

if you don't mind to use google translator, you can read more about here:
https://translate.google.com/translate?sl=de&tl=en&u=https://werner.rothschopf.net/202003_arduino_retriggerbares_nachlaufrelais.htm

2 Likes