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)