Trying to make an LED flicker (similar to a car turning light)

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();
}
}

lanskizane:
CODE: (I forgot how to insert it a better way, sorry.)

Read How to use this forum

You can edit your post, highlight the code, then click on the code tags button (<>).

The LED doesn't stop blinking because once you get into blinkingLights(), you never test the buttons again.

Also, you declared a function loop() inside of your blinkingLights() function. I would have expected a compiler error for that.

Honestly, there are a few things wrong with your code. I would definitly recommend looking up some lessons.

Firstly, you cannot call the loop function like that. The problem is twofold: You cannot call the loop function, and you cannot call a function like that. In fact, what you're doing there is defining a new function. Instead, you want to return from your blinkingLights function, so that the next iteration of loop() will occur.

Secondly, you used a while loop testing on buttonState2, but you never allow buttonState2 to change it's value. This means that the loop will run indefinitely.

Thirdly, many of your if statements are redundant. You only need to check for a press on button one, then loop untill there is a press on button 2.

Also, what is the point of having multiple 'blinks' in your loop?

// 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);

  digitalWrite(ledPin, LOW);
}

void loop() {
  // 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();
 
  }
 
  // 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);
      buttonState2 = digitalRead(buttonPin2);
    }

   return;
}