Help about one input and one output :)

Heloo everyone, i need your help :frowning:

I cant solve next programing problem:

There is one input and one output.

Output is always HIGH ,except if input goes from HIGH to LOW, and within 2 seconds goes HIGH again.

If time is more than 2 seconds, output remains HIGH, and in every other case, just need to be low if input is HIGH within 2 seconds after switching from HIGH to LOW.

Output shoud stay LOW within that 2 seconds.

Can you suggest some idea(s)?

Please post the code you have already tried (using code tags), and explain what goes wrong.

You would only need to use 3 function calls to implement this:

digitalRead(), digitalWrite() and millis()

The pseudocode could look like this:

if input is low
   store current time
   loop while time < 2 seconds
      read input
      if input is high
         set output LOW
   end loop

What you havenā€™t specified is what condition will reset the output back to HIGH

Output need to go HIGH after expiration of that 2 sec, no matter what state of input is.

I am new to this, and i have no meaningful code right now :flushed:

Am I right in assuming that you havenā€™t written any code at all, and expect someone here to solve your problem? This sounds like a classroom example, so getting a solution here would not really be helping you at all. As already suggested, you should try to solve it yourself and then post your program once you get stuck and someone will surely assist.

If you are really stuck, copy the ā€œblink.inoā€ sketch as a starting point and modify it to blink when your input goes HIGH. Once you have that working, expand the program and add the additional logic.

Of course i am not expecting complete code for my project, i only asked for ideas from somebody with more experience.

So far with your posts and comments you gave me some guidelines, but this weekend i am without my computer, so i cant try anything.

I will try and if stuck somewhere, i will post you to see.

And if i menage to solve it, i will also post code for someone who have similar problem.

Thank you so far :+1:

I maked it this way but something is wrong :shock:

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables will change:

int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  
}


void loop()
{
  static unsigned long timer = 0;
   unsigned long interval = 2000;
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) 
  {
    
   
    if (buttonState == LOW && lastButtonState == HIGH) 
    {
      
      timer = millis();

   if (millis() - timer <= interval )
        
   if (buttonState == HIGH && lastButtonState== LOW) 

      {
       digitalWrite(ledPin, LOW);  
      }
  
    }
 else
        {
          digitalWrite(ledPin, HIGH); 
        }
   
  }
   lastButtonState = buttonState;
  
}