Trying to use a delay for timer with reset

Hello!

I´m trying to make some code, that when i put a high signal on pin 9. I will get a 5v signal out on pin 10 and 12 for 18 seconds, and if it goes low for 5 sec then high again, i want 18 new seconds.

But when i give the signal on pin 9. the pin 10 and 12 to go high in 10 sec, and if i turn on the signal on pin 9,
then waiting 5 sec and then the signal on pin 9 get high again. Then it only gives my 5 sec.

Can someone get me in the right direction? I think i have to do something that reset the timer.

void setup() {
  //start serial connection
  Serial.begin(9600);

  pinMode(9, INPUT_PULLUP);
  pinMode(10, OUTPUT);
  pinMode(12, OUTPUT);

}

void loop() {
  //read the pushbutton value into a variable
  int sensorVal = digitalRead(9);
  Serial.println(sensorVal);


  if (sensorVal == LOW) {
    digitalWrite(12, LOW);
    digitalWrite(10, LOW); 
  }
  else {
    digitalWrite(10, HIGH);  
    digitalWrite(12, HIGH); 
    delay(18000);
  }
 
}

You don't have any code to check if the pin goes LOW and to prevent a new timing sequence until that happens.

Something like this

if (waitingForHigh == true) {
  if (sensorVal == HIGH) {
    waitingForHigh = false;
    // set pins HIGH
    // start timer
    }
else {
   if (sensorVal == LOW) {
      waitingForHigh = true;
   }
}

I hope I have got this right - my brain feels like porridge right this minute :slight_smile:

...R