Problem with push button reset: (update) reset fixed but button issues.

I know this is the most posted subject but I definitely fall in this category...
so I'm trying to make a button push counter that resets after a few presses of the button. I've been all over these forums and had my head crammed into the reference library and thought I found a couple of solutions but I think my syntax might be off a little (probably is). Now this is my first ounce of programming experience and only been messing with it a day or two so please cut me some slack.
Like I said I felt that I am close but don't know where the problem is...

const int  buttonPin = 2;    
const int led1 = 3;
const int led2 = 5;
const int led3 = 6;
const int led4 = 9;
const int led5 = 10;


// Variables will change:
int buttonPushCounter = 0;   
int buttonState = 0;         
int lastButtonState = 0;     
int buttonPushCounterMax = 6;

void setup() {
  
  pinMode(buttonPin, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);


  Serial.begin(9600);
}


void loop() {

  buttonState = digitalRead(buttonPin);

 
  if (buttonState && buttonState != lastButtonState) {
  
 for(buttonPushCounter = 0; buttonPushCounter > 6; buttonPushCounter++) ; {
     
      }
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
  }

  lastButtonState = buttonState;

  if (buttonPushCounter == 0);
  else if (buttonPushCounter == 1) {
    digitalWrite(led1, HIGH);
  }
  else if (buttonPushCounter == 2) {
    digitalWrite(led2, HIGH);
    digitalWrite(led1, LOW);
  }
   else if (buttonPushCounter == 3) {
    digitalWrite(led3, HIGH);
       digitalWrite(led2, LOW);
  }
  else if (buttonPushCounter == 4) {
    digitalWrite(led4, HIGH);
       digitalWrite(led3, LOW);
  }
   else if (buttonPushCounter == 5) {
    digitalWrite(led5, HIGH);
       digitalWrite(led4, LOW);
  }
     else (buttonPushCounter == 6); {
       digitalWrite(led5, LOW);

     }

}

And your question is?
if (buttonPushCounter == 0); <------- no ;
This isn’t very functional.

Have you learned about “switch case”

A few beginners errors.

The most important thing is to use indents correctly. There is also an 'auto' function in the menu. Use tabs or spaces, but use always the same layout. Upload your new sketch and try to have the layout without any error :roll_eyes:
I can tell you that for a beginner is it almost impossible to use indents consistant, but it is important. It will show any problems in the code much faster.

I am not sure what you want to do, so I am not sure if the sketch is okay.

You use 'buttonState' as a boolean, but it is an integer. Arduino does have a real 'boolean' variable : http://www.arduino.cc/en/Reference/BooleanVariables

Let me tell you what this line does: for(buttonPushCounter = 0; buttonPushCounter > 6; buttonPushCounter++) ; {
It says: "start by making the buttonPushCounter zero, run a loop while buttonPushCounter is larger than 6, increment buttonPushCounter in every loop, execute nothing in the loop (empty statement)".
You want to run the loop while it is lower than 6, and remove the ';' at the end of the line.

for(buttonPushCounter = 0; buttonPushCounter > 6; buttonPushCounter++) ;
Starting with buttonPushCounter at 0, while buttonPushCounter is greater than 6, do nothing (that’s what the semicolon on the end means). How many times will you actually do nothing? Why are you trying to do nothing more than once?

When that for loop ends, what will be the value in buttonPushCounter that the rest of the code relies on?

Since buttonPushCounter is NOT counting the number of times the switch was pressed, what possessed you to use that name?

like I said very very new to this. I'm actually an artist by trade, i'm good with lines not variables. I actually had it a little differently earlier, I didn't notice that the counter was stuck at 0, gimme a sec.

sorry guys here's the original code I had.

const int  buttonPin = 2;    
const int led1 = 3;
const int led2 = 5;
const int led3 = 6;
const int led4 = 9;
const int led5 = 10;


// Variables will change:
int buttonPushCounter = 0;   
int buttonState = 0;         
int lastButtonState = 0;     
int buttonPushCounterMax = 6;

void setup() {
  
  pinMode(buttonPin, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);


  Serial.begin(9600);
}


void loop() {

  buttonState = digitalRead(buttonPin);
  
 
  if (buttonState && buttonState != lastButtonState) {

    
    if (buttonState == HIGH) {
     

      buttonPushCounter++ ;
     
      }
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
  }

  lastButtonState = buttonState;

  if (buttonPushCounter == 0);
  else if (buttonPushCounter == 1) {
    digitalWrite(led1, HIGH);
  }
  else if (buttonPushCounter == 2) {
    digitalWrite(led2, HIGH);
    digitalWrite(led1, LOW);
  }
   else if (buttonPushCounter == 3) {
    digitalWrite(led3, HIGH);
       digitalWrite(led2, LOW);
  }
  else if (buttonPushCounter == 4) {
    digitalWrite(led4, HIGH);
       digitalWrite(led3, LOW);
  }
   else if (buttonPushCounter == 5) {
    digitalWrite(led5, HIGH);
       digitalWrite(led4, LOW);
  }
     else (buttonPushCounter == 6); {
       digitalWrite(led5, LOW);

     }

}

in this one the counter does count up and my leds come on accordingly but figuring out how to loop it back to 1 has been my problem.
The last one had multiple problems. I didn't notice I had posted the altered one.
And as for the project I am trying to hit a button to cycle through led settings.
LarryD- I have looked over the switch case and tried to mess with it a little yesterday but came up short, I need to try again.

If its OK, I'll just add comments to the code you posted with suggested changes. I got rid of the for loop and manipulated the buttonPuch Counter in the if statement. Let me know if this works for you.

const int  buttonPin = 2;    
const int led1 = 3;
const int led2 = 5;
const int led3 = 6;
const int led4 = 9;
const int led5 = 10;


// Variables will change:
int buttonPushCounter = 0;   
int buttonState = 0;         
int lastButtonState = 0;     
int buttonPushCounterMax = 6;

void setup() {
  
  pinMode(buttonPin, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);


  Serial.begin(9600);
}


void loop()
 {
      buttonState = digitalRead(buttonPin);  
       if (buttonState  != lastButtonState)  /* probably don' need to test ButtonState twice */
          {    
          buttonPushCounter ++;  
          Serial.print("number of button pushes:  ");
          Serial.println(buttonPushCounter);
        
          lastButtonState = buttonState;

         if (buttonPushCounter == 1)  /* it looks like this is what the origional code does */
             {
             digitalWrite(led1, HIGH);  /* if buttonPushCounter is 1 led 1 goes on and so forth */
             }
         if (buttonPushCounter == 2)
            {
            digitalWrite(led2, HIGH);
            digitalWrite(led1, LOW);
            }
         if (buttonPushCounter == 3)
            {
            digitalWrite(led3, HIGH);
            digitalWrite(led2, LOW);
            }
         if (buttonPushCounter == 4) 
            {
            digitalWrite(led4, HIGH);
            digitalWrite(led3, LOW);
            }
        if (buttonPushCounter == 5)
            {
            digitalWrite(led5, HIGH);
            digitalWrite(led4, LOW);
            }
        if (buttonPushCounter == 6)
            {         
            digitalWrite(led5, LOW);   
            buttonPushCounter = 0;  // reset to 0 after 6 pushes
            }
      }     /* end if buttonstate does not equal lastbuttonstate */
}     /* end of funtion void loop()  */

Fentrac please use code tags.
I would add:
pinMode(buttonPin, INPUT_PULLUP);
You are going to add some de-bounce code to filter out contact bounce.

Thanks Larry,
Tags added.

Frentrac-
It does loop now, appreciate it
For some reason it counts the button twice (once on press down and once on let up) I think that's why I had (buttonState && buttonState != lastButtonState) in there earlier.

After testing the (buttonState && buttonState != lastButtonState) in place of (buttonState != lastButtonState) just to see the outcome the counter only goes to 1. Man this is so silly! Any ideas?
I'll tell you what I have a new found respect for programmers, and this is making me want to learn more!

Peter_n:
A few beginners errors.

MAJOR beginners error

pay VERY close attention to item #6

so let me know if I'm understanding this right... so an INPUT_PULLUP takes use of internal resistors and inverts the signal and.. converts value of said signal?
From the example code it

dave-in-nj-
Appreciate it man, I don't mind looking a little ignorant but being an outright fool is not the way to go.

The reasons it counts on press and release is it is looking to see if they are not equal so 1 does not equal 0 and 0 does not equal 1. So, see if you can modify the code to only change the leds if they are not equal and the button state is a 0. This might work. This would assume that a button being pressed results in button grounding pin 2.

The example if statement is added below:

const int  buttonPin = 2;    
const int led1 = 3;
const int led2 = 5;
const int led3 = 6;
const int led4 = 9;
const int led5 = 10;

// Variables will change:
int buttonPushCounter = 0;   
int buttonState = 0;         
int lastButtonState = 0;     
int buttonPushCounterMax = 6;

void setup() {
  
  pinMode(buttonPin, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);

  Serial.begin(9600);
}

void loop()
 {    
      delay(50);  /* debounces if needed */
      buttonState = digitalRead(buttonPin);  
     
      if (buttonState != lastButtonState)  /* probably don' need to test ButtonState twice */
        {     
        lastButtonState = buttonState;

        if(buttonState == 0)  /* you might add this if statement here  if  pressing switch grounds pin 2 */
          {            
          buttonPushCounter ++;   
          Serial.print("number of button pushes:  ");
          Serial.println(buttonPushCounter);
          
          if (buttonPushCounter == 1)  /* it looks like this is what the origional code does */
            {
            digitalWrite(led1, HIGH);  /* if buttonPushCounter is 1 led 1 goes on and so forth */           
            }
          if (buttonPushCounter == 2)
            {
            digitalWrite(led2, HIGH);
            digitalWrite(led1, LOW);
            }
          if (buttonPushCounter == 3)
            {
            digitalWrite(led3, HIGH);
            digitalWrite(led2, LOW);
            }
          if (buttonPushCounter == 4) 
            {
            digitalWrite(led4, HIGH);
            digitalWrite(led3, LOW);
            }
         if (buttonPushCounter == 5)
            {
            digitalWrite(led5, HIGH);
            digitalWrite(led4, LOW);  
            }
         if (buttonPushCounter == 6)   
            {
            buttonPushCounter = 0;
            digitalWrite(led5, LOW); 
            }
         }  /* end if Buttonstate == 1 */
      }     /* end if buttonPress does not equal last buttonPress */
}     /* end of funtion void loop()  */

outlaw66613:
so an INPUT_PULLUP takes use of internal resistors and inverts the signal and.. converts value of said signal?

... What?

INPUT_PULLUP sets the pin as input and enables the internal resistor on the pin. That means that if nothing is connected to a pin, it will read as HIGH. If you want to connect a momentary switch to it, you connect one pin to the digital switch and the other to GND. Then, when the switch is pressed, the pin is shorted to GND and digitalRead will return LOW.

Arrch-
Gotcha I messed around with it and see what you mean, I'm going to need to figure out that function later.
Fentrac- wow weird stuff, still counts touch and let go on button, doesn't stop at 6, led 1, 3, and 5 are the only ones that light up and they didn't turn off.
that's crazy the only difference I saw in the code was: if(buttonState == 1)

Remember, switches bounce when you press and release them.

So, only some of the led's light because the " buttonPushCounter ++; " instruction is before the new if statement. It gets incremented
when the buttonstate is both 1 and 0, move it inside the new if(buttonstate == 1) brackets and it will only increment on the 1 condition. (Keep in mind that I don't know for sure whether pressing the button or releasing the button is a 1). I wired up the circuit I am using to test your sketch as a 1 is pressed (connects pin 2 to 5V) but yours could be the opposite.

LarryD:
Remember, switches bounce when you press and release them.

^^^^^^^

this may be your problem!

Switch bounce is probably not an issue with this sketch. It has a couple of strings that are being printed to the serial port at 9600 baud, giving plenty of delay before the switch is read again. I'm not getting any bounce on the test circuit. I modified the if statement on last code post to correct the issue of polarity of the button to pressed equals 0, and placement of increment buttonPushcounter and serial.print() functions.