resetting an array sequence with a second push button

I currently have 5 LEDs blinking one after another every time I push a push button. What I would like now is to have a second push button that restarts the sequence back to the first LED. I have tried multiple attempts with different solutions but cant seem to get it to work.

Any help will be appreciated!

Soooooooooo, where is that code? Please read How to use this forum before posting it!

Here is the code I have so far for the array. Sorry for not posting it originally.

const int buttonPin = 2; // the pin number of the pushbutton input pin  
  int buttonPin2 = 1; // 2nd pin for the reset 
  int ledPins[] = {3, 4, 5, 6, 7};  
  // variables will change:  
  int buttonState = 0; // variable for reading the pushbutton status  
  int buttonPressCount = 0;  
  int numberOfLED = 5;  
 void setup() {  
  // initialize the LED pin as an output:  
   for (int i = 0; i < numberOfLED; i++) {  
    pinMode(ledPins[i], OUTPUT);  
   pinMode(buttonPin, INPUT);  
  void loop() {  
   // read the state of the pushbutton value:  
   buttonState = digitalRead(buttonPin);  
   // check if the pushbutton is pressed.  
   // if it is, the buttonState is HIGH:  
   if (buttonState == HIGH) {  
    for (int i = 0; i < numberOfLED; i++) {  
     if (buttonPressCount % numberOfLED == i) {  
      // turn LED on:  
      digitalWrite(ledPins[i], HIGH);  
     } else {  
      // turn LED off:  
      digitalWrite(ledPins[i], LOW);  

You really should use Tools + Auto Format before posting code.

But, lets take a look at what happens when you press the first switch. The switch pin's state is read, and determined to be HIGH (presumably because you have used an external resistor and wired the switch in a pulldown configuration). When that happens, the for loop is executed, which happens very quickly. Then, you stuff your head in the sand for a while.

The blinking one after another happens because loop() is called over and over. The for loop just determines the state of the LEDs.

You never actually read the second switch.

Even if you did, it is not clear that you know what you need to do to "restarts the sequence back to the first LED.". Perhaps it's as simple as:

   if(digitalRead(theSecondSwitchPin) == HIGH)
      buttonPressCount = 0;

Some tips and questions after seeing your code

Watch your indentation! Indentation is the most powerful tool to see the structure of the code in a single glance. Press Ctrl+T in the IDE and see how it looks.

Blank lines are free. Don’t overdo it but a blank line in front of a new thing line the start of a function really makes all more clear.

Nothing ever follows after a {, } or ; (with the exception of the later in a loop declaration).

All variables are small so a byte is more then big enough for every variable instead of an int.

I assume you have a external pull down resistor? Why not make things easier and connect the button straight between the pin and GND ans use pinMode(pin, INPUT_PULLUP)? Saves you from all the hassle for that resistor.

Why do you get the idea of arrays with the leds but number the button pins? But in this case the buttons don’t have a strong connection with each other aka numbering doesn’t really tell us much. It makes more sense to call them IncrementButtonPin and ResetButtonPin.

Why do you get the idea of const for buttonPin but not for the rest?

And now off to the problem, get rid of the delay(). Second chapter in every Arduino course should be “Never ever use delay() again”… In a delay() you can’t do anything. And for the button, instead of checking to see it’s HIGH, check if it became HIGH. (Read it again if you don’t see the difference.) It’s called state change.

But now you really read input you might have button bounce (Google it :wink: To come round this and the state change you can make things very easy by using a library like Bounce2.

Now move the setting of the leds out of the check for a button press on the increment button. * Setting the leds the same as last time doesn’t hurt.

And now all you have to do for the reset is to check if it became pressed and set buttonPressCount to 0 again. **

  • Alternatively, put that bit into a separate function and call that function after increasing the counter but also after resetting it.

** By doing

buttonPressCount % numberOfLED == i

you make a error which you see after many button presses. Even though 100 % 5 = 0, the value is still 100. What about when the value is 32767. The modulus of that is 2. But now you press the button. 32768 does not fit into an int… So it will roll over and become **-**32768! The modulus of that 2 again! In this instance it just looks like a button malfunction but if you have 6 leds. 32767 % 6 = 1. After increasing it, -32768 % 6 = 4. So a weird jump! So instead of checking again the modulus assign the counter the modules value again

buttonPressCount %= numberOfLED;

Or even simpler, check for a overshoot

if(buttonPressCount >= numberOfLED){
  buttonPressCount = 0;

Both of you guys helped so much! I am doing a class for this and I don't know what it is but I am really struggling! I did the ctrl t and I understand what you mean now also! thanks for that. One thing I was never taught tho was that you don't actually have to use a resistor for a push button?

You do use a resistor (kind of) but it's a switchable build in resistor. :D Because a pull up resistor is so common they just made a individual switchable build in version for you.