Exit Void Loop, Do a Function, and Return to Void Loop with Button Press

I am trying to create a program that runs a type of LED chaser most of the time, and then when a user presses a button, it runs a function that lights a random LED. After 5 seconds, it returns to the void loop. My issue is that it runs the random_led function all of the time and never runs the main LED sequence. How can I get this to work? The code is below:

#define LED1 3 
#define LED2 4 
#define LED3 5 
#define LED4 6 
#define LED5 7
#define LED6 8
#define LED7 9
#define LED8 10
#define buzzerPin 22
#define buttonPin 2

void setup() {
  pinMode(buzzerPin, OUTPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT); 
  pinMode(LED3, OUTPUT); 
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT);
  pinMode(LED7, OUTPUT);
  pinMode(LED8, OUTPUT);

  pinMode(buttonPin, INPUT);

  digitalWrite(LED1, LOW);
  digitalWrite(LED2, LOW); 
  digitalWrite(LED3, LOW); 
  digitalWrite(LED4, LOW);
  digitalWrite(LED5, LOW);
  digitalWrite(LED6, LOW);
  digitalWrite(LED7, LOW);
  digitalWrite(LED8, LOW);

  digitalWrite(buzzerPin, HIGH);
  delay(100);
  digitalWrite(buzzerPin, LOW);
  delay(100);
  digitalWrite(buzzerPin, HIGH);
  delay(100);
  digitalWrite(buzzerPin, LOW);
  delay(100);
  digitalWrite(buzzerPin, HIGH);
  delay(100);
  digitalWrite(buzzerPin, LOW);
  delay(100);

}

void loop() {
    
  if (digitalRead(buttonPin), HIGH) {   
      random_led();
  }
  else {
    digitalWrite(LED1, LOW);
      digitalWrite(LED2, HIGH);
      digitalWrite(LED3, HIGH);
      digitalWrite(LED4, HIGH);
      digitalWrite(LED5, HIGH);
      digitalWrite(LED6, HIGH);
      digitalWrite(LED7, HIGH);
      digitalWrite(LED8, HIGH);
      delay(125);

      digitalWrite(LED1, HIGH);
      digitalWrite(LED2, LOW);
      digitalWrite(LED3, HIGH);
      digitalWrite(LED4, HIGH);
      digitalWrite(LED5, HIGH);
      digitalWrite(LED6, HIGH);
      digitalWrite(LED7, HIGH);
      digitalWrite(LED8, HIGH);
      delay(125);

      digitalWrite(LED1, HIGH);
      digitalWrite(LED2, HIGH);
      digitalWrite(LED3, LOW);
      digitalWrite(LED4, HIGH);
      digitalWrite(LED5, HIGH);
      digitalWrite(LED6, HIGH);
      digitalWrite(LED7, HIGH);
      digitalWrite(LED8, HIGH);
      delay(125);

      digitalWrite(LED1, HIGH);
      digitalWrite(LED2, HIGH);
      digitalWrite(LED3, HIGH);
      digitalWrite(LED4, LOW);
      digitalWrite(LED5, HIGH);
      digitalWrite(LED6, HIGH);
      digitalWrite(LED7, HIGH);
      digitalWrite(LED8, HIGH);
      delay(125);

      digitalWrite(LED1, HIGH);
      digitalWrite(LED2, HIGH);
      digitalWrite(LED3, HIGH);
      digitalWrite(LED4, HIGH);
      digitalWrite(LED5, LOW);
      digitalWrite(LED6, HIGH);
      digitalWrite(LED7, HIGH);
      digitalWrite(LED8, HIGH);
      delay(125);

      digitalWrite(LED1, HIGH);
      digitalWrite(LED2, HIGH);
      digitalWrite(LED3, HIGH);
      digitalWrite(LED4, HIGH);
      digitalWrite(LED5, HIGH);
      digitalWrite(LED6, LOW);
      digitalWrite(LED7, HIGH);
      digitalWrite(LED8, HIGH);
      delay(125);

      digitalWrite(LED1, HIGH);
      digitalWrite(LED2, HIGH);
      digitalWrite(LED3, HIGH);
      digitalWrite(LED4, HIGH);
      digitalWrite(LED5, HIGH);
      digitalWrite(LED6, HIGH);
      digitalWrite(LED7, LOW);
      digitalWrite(LED8, HIGH);
      delay(125);

      digitalWrite(LED1, HIGH);
      digitalWrite(LED2, HIGH);
      digitalWrite(LED3, HIGH);
      digitalWrite(LED4, HIGH);
      digitalWrite(LED5, HIGH);
      digitalWrite(LED6, HIGH);
      digitalWrite(LED7, HIGH);
      digitalWrite(LED8, LOW);
      delay(125);
  }
}

void random_led() {
  
  int random_led = random (3, 10);

  digitalWrite(LED1, LOW);
  digitalWrite(LED2, LOW); 
  digitalWrite(LED3, LOW); 
  digitalWrite(LED4, LOW);
  digitalWrite(LED5, LOW);
  digitalWrite(LED6, LOW);
  digitalWrite(LED7, LOW);
  digitalWrite(LED8, LOW);

  digitalWrite(random_led, HIGH);
  delay(5000);
  digitalWrite(random_led, LOW);
  delay(150);

}

if (digitalRead(buttonPin) == HIGH)

1 Like

Time to run through the IDE examples.
Too many bad habits starting here.

Hello confused_programmer

Welcome to the worldbest Arduino forum ever.

I´v made a small code review.
It seem to be a none human generated code.

In general - Arrays and structs are your friends.
Don't duplicate code in your sketch. Write code once - use it multiple times.

Have a nice day and enjoy coding in C++.

Hi @confused_programmer

After fixing the condition, as @LarryD suggested, you will encounter the opposite problem. The program will be in a loop all the time and will not respond to button presses.
This is due to the fact that your loop uses delays during which the button is not polled.
For everything to work, the program must be rewritten in a non-blocking style in accordance with the BlinkWithoutDelay example

1 Like

It seems like your code has a small issue in the if condition. In your loop() function, you have this line:

if (digitalRead(buttonPin), HIGH) {

The issue here is that you are using a comma , instead of == for comparison. You should change it to:

if (digitalRead(buttonPin) == HIGH) {

This way, it correctly checks if the buttonPin is in a HIGH state. With this change, your program should run the LED chaser most of the time and execute the random_led function when the button is pressed.

@codingapacalyspe
see the post #2

1 Like

I seen it. I felt like there needed to be an explanation behind it for learning purposes.

1 Like

buttons are typically connected between the pin and ground, the pin configured as INPUT_PULLUP to use the internal pullup resistor which pulls the pin HIGH and when pressed, the button pulls the pin LOW.

a button press can be recognized by detecting a change in state and becoming LOW

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.