# Making pushbutton count return to zero at set number of counts???

I am trying to make a single button press sequence light 4 LEDs in total.

The first 3 LED’s light one at a time with each press of the push button. Only one is lit at the time.
This is my first ever project with either Arduino or coding so forgive me for any simple errors.

The code I have compiled so far lights the 3 LEDs in the way that I want but I cannot work out how to make the loop return back to LED no.1 when it reaches the end (4th button press).

The 4th LED I haven’t got around to attempting the code yet as I want to finalise the 3 LEDs operation first then add it on, but basically I want to be able to long press the button and it output to LED 4. I’ll get to that later.

Here is the code:

``````/*
Button

Pushing a button will turn on LED1, then on the 2nd press light LED2 whilst
turning off LED1. On the 3rd press it will light LED3 whilst turning off LED2.
A 4th press will return the loop back to the start.
pins 11, 12 & 13 are connected each to an LED. The pushbutton is attached to pin 2.
A 4th LED is connected to pin 8 and will flash if a long press of the pushbutton
is detected.

The circuit:
* LED1 attached from pin 11 to ground
* LED2 attached from pin 12 to ground
* LED3 attached from pin 13 to ground
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground
* LED4 attached from pin 8 to +5V rail on breadboard to the Anode, Cathode to Ground.

created 2016
JK
*/

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;      // the number of the pushbutton pin
const int ledPin1 =  11;      // the number of the LED 1 pin
const int ledPin2 =  12;      // the number of the LED 2 pin
const int ledPin3 =  13;      // the number of the LED 3 pin
const int ledPin4 =  8;       // the number of the LED 4 pin Used for a long press indication

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

void loop() {
// read the state of the pushbutton value:

// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// went from off to on:
buttonPushCounter++;
} else {
// if the current state is LOW then the button
// went from on to off:
}
// Add a delay to debounce
delay(50);
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;

// turns on the LED every one button push and
// Lights each LED in turn one at a time turning
// off the preceeding LED as well
if (buttonPushCounter  == 1) {
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin1, HIGH);

}
if (buttonPushCounter == 2) {
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
}
if (buttonPushCounter == 3) {
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, HIGH);
}
if (buttonPushCounter == 4) {
buttonState = 0;
}

}
``````
``````    if (buttonState == HIGH) {
// if the current state is HIGH then the button
// went from off to on:
buttonPushCounter++;
if (buttonPushCounter == 5){ buttonPushCounter = 0;  // reset the count
}
}
``````

Add a check to see if you've gone past 4, and reset if you did.

I tried the code but it does not have the desired effect. For some reason when pressing the pushbutton the other LED's light before moving on to the next one in the sequence.

Perhaps I did not fully understand your reply but I have just changed the code to the following and it has worked exactly as I needed it to.

if (buttonPushCounter == 3){ buttonPushCounter = 0; // reset the count

Thanks again.

Just need to work out how to add in a long button press for LED No.4.

TableLeg: Just need to work out how to add in a long button press for LED No.4.

That's going to involve looking for State Change on the pin and learning to use millis to figure out how long it took for something to happen.

Delta_G: That's going to involve looking for State Change on the pin and learning to use millis to figure out how long it took for something to happen.

Thanks,

Yes I figured that was the case having seen it in similar Sketches however I am unsure at this time exactly how to implement it and where in the sketch. Would it replace any or all of the current 'buttonState' code or is it additional to it?

I would really appreciate any pointers you can give.

Thanks again.

TableLeg: Thanks,

Yes I figured that was the case having seen it in similar Sketches however I am unsure at this time exactly how to implement it and where in the sketch. Would it replace any or all of the current 'buttonState' code or is it additional to it?

I would really appreciate any pointers you can give.

Thanks again.

Those words I put in bold? Try putting those into google. Maybe add the word arduino to narrow the search a little.

Delta_G: Those words I put in bold? Try putting those into google. Maybe add the word arduino to narrow the search a little.

Thanks again.

Yes I tried that and have looked at several sketches but I don't understand fully understand the coding and how to integrate it into my sketch. Having only started reading basic code this last week I hoped I'd get some more direct assistance.

I'd be more interested to know how it integrates than actually have somebody just tell me. Perhaps you can offer an insight into how a sketch with it works?

Would this code copied from another post on here work?

{
time = millis();
delay(200); //debounce

// check if the switch is pressed for longer than 1 second.
if(digitalRead(SWITCH_PIN) == LOW && time - millis() >1000)

{
varSWITCH_PIN++; //add 1 Step to next Mode in setup
if(varSWITCH_PIN==8){varSWITCH_PIN=0;} //switch back to 0 after the required modes

// if it is a short press <1000
} else
do something

}

This code may be too complicated for you at this stage, but it is worth a try.

I have a library called MD_KeySwitch (link in the signature block below) that implements press, long press, double press and auto repeat for a digital input connected to a switch. The code there shows one way that this can be implemented if you are interested to follow the link.

It should not be so difficult for you to integrate code that keeps track of the time between state changes into your program. When this happens

`````` // save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
``````

you begin looking for a state change. When this happens

`````` if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
``````

you have detected a state change. If you mark the times, using millis(), that both of these happen the difference will be the time it took for the state to change. I suppose you would have to initialize the state start time to millis() at the end of setup() in order to measure the first change.

Many thanks to both Marco and Blue Eyes for the replies.