If statement with time condition

I want to make a project where if the pin 2 get high for more than 5 seconds, pin 9 will be high and 9 pin will remain high untill i reset it. I need the programme statement. please!

When is our assignment due to be handed in?

Hint: use millis() to timestamp the transition of pin 2 from LOW to HIGH

i made this programme, actually here are some mistakes

unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 5000;
bool val;
void setup()
pinMode(9, OUTPUT);


void loop()
currentMillis = millis();

  if (digitalRead(2) == HIGH && digitalRead(3)== HIGH) 
      startMillis = millis();

      if (currentMillis - startMillis >= period)
      { digitalWrite(9,HIGH);

if (digitalRead(2) != HIGH || digitalRead(3)!= HIGH)

if (val=true && currentMillis - startMillis >= period) {
if (val=false) {


@akkas, your topic has been moved to a more suitable location on the forum.

Introductory tutorials is for tutorials that have been written, not questions. Feel free to write a tutorial one you have mastered your problem :slight_smile:


Can you also please read How to get the best out of this forum and apply what you learned about code tags to the code that you posted. Thanks.

Who will or how will be reset pin 9?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.