Hi everyone,
In this program, I am trying to make it so that once I press a button (buttonState, buttonPin, etc.), it will cause an LED to flicker continuously until a second button is pressed (buttonState2, buttonPin2, etc.) and then the blinking will stop. For some reason, whenever I press the second button, the LEDs will not stop blinking. Any ideas why?
CODE: (I forgot how to insert it a better way, sorry.)
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
const int buttonPin2 = 4;
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int buttonState2 = 0;
void setup() {
Serial.begin(9600);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
}
void loop() {
digitalWrite(ledPin, LOW);
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
buttonState2 = digitalRead(buttonPin2);
// Show the state of pushbutton on serial monitor
Serial.println(buttonState2);
Serial.println(buttonState);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
blinkingLights();
}
if (buttonState2 == HIGH) {
// turn LED on:
digitalWrite(ledPin, LOW);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
// Added the delay so that we can see the output of button
delay(100);
}
void blinkingLights() {
while(buttonState2 == 0){
digitalWrite(ledPin, HIGH);
delay(250);
digitalWrite(ledPin, LOW);
delay(250);
digitalWrite(ledPin, HIGH);
delay(250);
digitalWrite(ledPin, LOW);
delay(250);
digitalWrite(ledPin, HIGH);
delay(250);
digitalWrite(ledPin, LOW);
delay(250);
}
if(buttonState2 == 1)
{
void loop();
}
}