25869Views20Replies

Author Options:

Is there a way to count button pushes on an arduino and have it reset the count at a certain number? Answered

I would like to count the number of buttons pushed and when it reaches the number 8, I would like it to reset it to 0. Can anyone help me with an example code or a tutorial somewhere? Greatly Appreciated!

20 Replies

user
mpilchfamilyBest Answer (author)2013-07-22
Take the button sketch. Add a counting variable. If the button is pressed add 1 to that variable. If the variable is >= 8 store 0 to the counting variable.

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin
int count = 0;               // Count the button presses

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);     
  // initialize the pushbutton pin as an input:
  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) {    
    // turn LED on:   
    digitalWrite(ledPin, HIGH); 
    count++;               // add 1 to the count
    if (cout >= 8) {
      count = 0;
    } 
  }
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

Select as Best AnswerUndo Best Answer

user
AzamJ (author)mpilchfamily2016-02-22

hye mpilchfamily is there a wayto make the led blinking after fourth time push the button? is it possible using this coding???

Select as Best AnswerUndo Best Answer

user
mpilchfamily (author)AzamJ2016-02-22

Yes. Changes can be made to this code to accomplish that. Good luck with it.

Select as Best AnswerUndo Best Answer

user
AzamJ (author)mpilchfamily2016-02-22

i try to make a change but failed... can you help me?

Select as Best AnswerUndo Best Answer

user
mpilchfamily (author)AzamJ2016-02-22

Change the if count >= 8 to count >= 4. Between that and the count = 0 set led to high delay and set led low. Repeat the high low and delay as much as you want.

Select as Best AnswerUndo Best Answer

user
AzamJ (author)mpilchfamily2016-02-22

like this?

if (buttonState ==LOW) {

// turn LED on:

digitalWrite(ledPin, HIGH);

count++; // add 1 to the count

if (count >= 4) {

digitalWrite(ledPin, HIGH);

delay(500);

digitalWrite(ledPin, LOW);

count = 0;

}

Select as Best AnswerUndo Best Answer

user
mpilchfamily (author)AzamJ2016-02-22

Have you tried to run it? Try it for yourself and see what happens.

Select as Best AnswerUndo Best Answer

user
AzamJ (author)mpilchfamily2016-02-23

i have try it before actually.. and led blink when i pressed button once...not after the fouth time... (i used sound sensor as input)

Select as Best AnswerUndo Best Answer

user
mpilchfamily (author)AzamJ2016-02-23

Cause you need to check your notation. Your missing a }. So the arduino reads the if count statement and then executes the flash and count reset no matter what.

if (buttonState ==LOW) {

// turn LED on:

digitalWrite(ledPin, HIGH);

count++; // add 1 to the count

if (count >= 4) {

digitalWrite(ledPin, HIGH);

delay(500);

digitalWrite(ledPin, LOW);

count = 0;

}

}

Select as Best AnswerUndo Best Answer

user
AzamJ (author)mpilchfamily2016-02-23

sorry for that sir.. i have put the missing { on the coding.. but i dont know what the cause.... led still blink when button pressed once.. not after the fourth time...

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 == LOW) {

// turn LED on:

digitalWrite(ledPin, HIGH);

count++; // add 1 to the count

if (count >= 4) {

digitalWrite(ledPin,HIGH);

delay(500);

digitalWrite(ledPin,LOW);

}

}

else {

// turn LED off:

digitalWrite(ledPin, LOW);

}

}

Select as Best AnswerUndo Best Answer

user
PatelA23 (author)2017-01-25

how can i see ... how many time push button are pushed ????

Select as Best AnswerUndo Best Answer

user
AzamJ (author)2016-02-22

hye guys how to make the led blinking after fourth time push the button? is it possible using this coding???

Select as Best AnswerUndo Best Answer

user
steveastrouk (author)2013-07-23

if (buttonState == HIGH) {   
    // turn LED on:  
    digitalWrite(ledPin, HIGH);
    count++;               // add 1 to the count
    if (cout >= 8) {
      count = 0;
   while(buttonState==HIGH); //wait here.
    }
  }

Select as Best AnswerUndo Best Answer

user
bigjohn412 (author)2013-07-22

Ok, I understand now. Thanks!

Select as Best AnswerUndo Best Answer

user
steveastrouk (author)bigjohn4122013-07-22

Careful, that sketch has a fatal flaw.

Select as Best AnswerUndo Best Answer

user
gmoon (author)steveastrouk2013-07-22

How many iterations of loop() happen while the button is pressed?

Select as Best AnswerUndo Best Answer

user

count ++

And...

if (count >= 8 ) {

Missing a space between count and the ++. Also forgot the 'n' in count on the if statement. Should have proof read and compiled the sketch before posting.

There are many ways to structure it depending on the exact needs for the sketch. But you get the general idea of adding a means of tracking things. Create the variable for tracking, add to the variable where needed and form time to time query that variable to see if it's reached the mark you want. 

Select as Best AnswerUndo Best Answer

user

You need a state to catch the fact the button STAYS pressed. What you want to count is the state off-on-off

Select as Best AnswerUndo Best Answer