Mohtadeen Mastoor
Mohtadeen Mastoor

Reputation: 77

Disable push button for 30 sec after press arduino

I am working with Arduino as I am new need your help please Thanks! I am turning on the LED light via push-button for 30 seconds all working fine but there is one issue after pushing the button light on start from 0 sec but if I push the button after 15 sec it will start again from 0 sec so, is there any way can I disable the push button also for 30 sec so it will run only 30 sec even I will push the button and button will work only when the light of after 30 sec.

    int BUTTON = 2;
    int BUTTONstate = 0;
    int LED = 8;

    void setup() {
    pinMode(BUTTON, INPUT);
    pinMode(LED, OUTPUT); 
    }


    void loop() {
    static unsigned long startTime = 0;

    BUTTONstate = digitalRead(BUTTON);
    if (BUTTONstate == HIGH){
    if (millis() - startTime >= 30000)
    digitalWrite(LED, LOW);        
    }

    else{
    digitalWrite(LED, HIGH);
    startTime = millis();
    }
    }

Upvotes: 1

Views: 790

Answers (1)

Mohtadeen Mastoor
Mohtadeen Mastoor

Reputation: 77

I got the correct code working fine:

    int pin1 = 13;
    int LED = 8;
    void setup()
    {
      pinMode(pin1, INPUT_PULLUP);
      pinMode(LED, OUTPUT);
    }

    long offAt = 0;
    void loop()
    {
      if ((digitalRead(LED) == LOW ) && (digitalRead(pin1) == LOW) ) //if LED is off  and button is pressed [low because it has pullup resistor]
      { 
        digitalWrite(LED, HIGH);
        offAt = millis() + 30000; //store var of now + 5 seconds
      }

      if (digitalRead(LED) == HIGH) //if led is on
      {
        if (millis() >= offAt) //see if it's time to turn off LED
        {
          digitalWrite(LED, LOW); //it's time. this also re-enables the button
        }
      }
    }

Upvotes: 2

Related Questions