making a time based led

Im trying to create a program with this statement.

Develop a program such that if you press a touch sensor an LED will light up once, and if the sensor is pressed again within 5 seconds it blinks twice. After the button is pressed the second time (or 5 seconds pass), the program goes back to its original condition (waiting for the first press of the sensor).

I can make it blink twice, but I cannot get the condition to go back after 5 seconds.
what am I doing wrong?

heres my current code

int btn = 7;
int count = 0;
int led = 6;
long timex;
long timez;
void setup() {
  Serial.begin(9600);
  pinMode(btn,INPUT); 
  pinMode(led,OUTPUT);
  
}

void loop() {
  
 timex = millis();
 timez = timex;
 Serial.print("timex:   ");
 Serial.println(timex);

    if(digitalRead(btn)) {
    timez = timex; 
    Serial.print("Timez:   ");
    Serial.print(timez);
    digitalWrite(led,HIGH);
    delay(100);
    digitalWrite(led,LOW); 
    delay(100);
    
    
    
    while(timez - timex < 5000)
      if(digitalRead(btn)) {
        digitalWrite(led,HIGH);
        delay(100);
        digitalWrite(led,LOW);
        delay(100);
        digitalWrite(led,HIGH);
        delay(100);
        digitalWrite(led,LOW);
        return;
        }
    } 
}

That problem statement makes this look like a class assignment. We don’t do class assignments here.

I sure hope that the btn pin is wired to a pulldown resistor and to a N.O. pushbutton switch that has its other side connected to Vcc but without a schematic, who is to know?

Read the topic pinned at the top of the forum on how to best use millis() and read about state machines

vaj4088:
That problem statement makes this look like a class assignment. We don't do class assignments here.

I sure hope that the btn pin is wired to a pulldown resistor and to a N.O. pushbutton switch that has its other side connected to Vcc but without a schematic, who is to know?

The wiring is fine, it blinks when i press the button, and then blinks twice when i press it again and it resets. However i cannot make it reset so that it doesnt blink twice after 5 seconds. I do not know how to do that, and cannot find anything in the millis() thread

Figure out how to rewrite your program so that your able to delete all instances of “delay(100);” and your well on your way to making this work.

Read about state machines (Bis)

joeman783:
I do not know how to do that, and cannot find anything in the millis() thread

You didn't find ANYTHING in that thread? Then you need to study it harder. You can find from milis() what time it was when the button was first pressed. At any later point you can compare that time to the current time to see if 5 seconds have passed. That sounds pretty useful to me.

To help you a bit - ask yourself when you do while(timez - timex < 5000) what is changing timez In the while loop…

I would not approach the problem this way though, your blinking is basically simple:

In the loop, Upon press of the button, blink twice if the previous press was less than 5 seconds ago and you did not already blink twice last time, else Blink once