Function in Loop doesn't restart after performed

Hello - I could really use some help as I have been struggling with this for a while.

I have a button that when pressed it creates a red light to blink faster until t=0. When that happens, a vibration motor is triggered for a few seconds.

The problem is that the first time it performs as expected. When I press the button again, motor turns on only. How do I set the loop to a restart after the button is pressed each time so I can get a repeat performance of the expected effect?

Sorry- not sure how to insert the code so I did a copy and paste and attachment.

#define MOTOR 9

int btnStateR = 0;


int btnR = 5;


int RED_LED_PIN = 8;

int t=150;
int QUICKNESS=7;

void setup() {

Serial.begin(9600);               //initialize serial comm. at 9600 bits per second
 pinMode(btnR, INPUT);          // declaring output for vibration motor explosion
 pinMode(RED_LED_PIN, OUTPUT);
 pinMode(MOTOR, OUTPUT);
}

void loop() {

btnStateR = digitalRead(btnR);
   if (btnStateR == HIGH){
     Serial.println("RED");
     delay(500);
     countdownBlink();
     vibMotor();
   }
}
 


void countdownBlink() {  
   for (t; t > 0; t -= QUICKNESS) {
     digitalWrite(RED_LED_PIN, HIGH);
     delay(t);
     digitalWrite(RED_LED_PIN, LOW);
     delay(t);
   }
     if (t == 0); {
       digitalWrite(RED_LED_PIN, HIGH);
//        delay(2000);
//        digitalWrite(RED_LED_PIN, LOW);
     }
}


void vibMotor(){
     if (t == 0);{
          analogWrite(MOTOR, 255);
          delay (2500);
          analogWrite(MOTOR, 0);
  }
}

line_button_test.ino (1.02 KB)

    for (t; t > 0; t -= QUICKNESS) {

You never reset t back to the initial value. Since it's global, it remembers whatever you put into it.

Please edit your post to include code tags. Read the how-to-use-this-forum post at the top.

Thanks MorganS - Can you give me an example of how to "reset" the value? This is new to me.

The for loop

The simplest way would be something like this:

btnStateR = digitalRead(btnR);
   if (btnStateR == HIGH){
     Serial.println("RED");
     delay(500);
     countdownBlink();
     vibMotor();
     t=150;        //just add this line to your code
   }

Thank you for your responses. I knew the solution was simple but I overlooked how simple it was. I got stuck on trying to use a state change but I couldn't figure out how to apply it.
Much appreciated!

You have two places where you put a spurious ';' between an 'if' statement and what appears to be the intended body of the 'if'. The ';' is a valid (null) statement and it is treated as the only statement in the body of the 'if'. The intended body is treated as a separate, unconditional, block statement.

void countdownBlink()
{
  for (t; t > 0; t -= QUICKNESS)
  {
    digitalWrite(RED_LED_PIN, HIGH);
    delay(t);
    digitalWrite(RED_LED_PIN, LOW);
    delay(t);
  }
  if (t == 0)
    ;  
  {
    digitalWrite(RED_LED_PIN, HIGH);
    //        delay(2000);
    //        digitalWrite(RED_LED_PIN, LOW);
  }
}




void vibMotor()
{
  if (t == 0)
    ;
  {
    analogWrite(MOTOR, 255);
    delay (2500);
    analogWrite(MOTOR, 0);
  }
}