# Want to Implement Button in a counter

I wish to implement a counter whenever a button is press, if the button is pressed, the counter will start if the sensor is detecting something, and if the button is not pressed, the counter will then restart to 0. but somehow, in my coding the counter will count regardless if the button is pressed or not. I hope you can guide me on solving this problem

the following is my code

int val = 0;
int counter = 0;
int currentState = 0;
int previousState = 0;
int sensorVal = 2;
const int buttonPin = 4 ;
int buttonState = 0;

void setup() {
pinMode(sensorVal,INPUT_PULLUP);
pinMode(buttonPin , INPUT);
Serial.begin(9600);
}

void loop(){

if (buttonState == HIGH)
{

if (val == HIGH)
{

currentState = 1;
}
else
{

currentState = 0;
}
if(currentState != previousState)
{
if(currentState == 1)
{
counter = counter + 1;
Serial.println(counter);
}
}
previousState = currentState;
delay(250);
}
else if (buttonState == LOW)
{
previousState = 0;
currentState = 0;
counter = 0;
}
}
int sensorVal = 2;
pinMode(sensorVal,INPUT_PULLUP);

So far, so good.

? ? ?

Rather than checking if the button is HIGH

if (buttonState == HIGH)

you need to check for when it changes from LOW to HIGH. Something like this

prevButtonState = buttonState;
if (buttonState == HIGH and prevButtonState == LOW) {
// now you can increment the count if appropritate

Note that best practice is to wire a switch so it connects the button pin to GND when pressed. Then use pinMode(buttonPin, INPUT_PULLUP); and when the button is pressed the Arduino will see a LOW. If you want the button press to produce a HIGH then you should use an external pull-down resistor to prevent the pin from floating.

...R

hey Robin2, I done what you did and it works perfectly but now I wanted to do is if you press the switch/button once, it will become toggle and during that time the if the sensor detects anything the counter will go up, but in my coding right now, after I press the switch the 2nd time, only then the counter will go up. is there anyway to help fix this. oh I am using boolean to make the switch act as a toggle.

int val = 2; //ir sensor
int counter = 0;
int currentState = 0;
int previousState = 0;
const int buttonPin = 4 ;
boolean buttonState = LOW;
boolean prevButtonState ;

void setup() {
pinMode(val,INPUT_PULLUP);
pinMode(buttonPin , INPUT_PULLUP);
Serial.begin(9600);
}

void loop(){

prevButtonState = buttonState;
delay (10);
if(buttonState != prevButtonState)
{
delay (10);
if (val == LOW)
{

currentState = 1;
}
else
{

currentState = 0;
}
if(currentState != previousState)
{
if(currentState == 1)
{
counter = counter + 1;
Serial.println(counter);
}
}
previousState = currentState;
delay(250);
}
}

*EDITED AT int val; to int val = 2;

pinMode(val,INPUT_PULLUP); What is connected to pin zero?

TheMemberFormerlyKnownAsAWOL:
pinMode(val,INPUT_PULLUP); What is connected to pin zero?

oops forgot to add int val = 2

ABSYR:
hey Robin2, I done what you did and it works perfectly but now I wanted to do is if you press the switch/button once, it will become toggle and during that time the if the sensor detects anything the counter will go up, but in my coding right now, after I press the switch the 2nd time, only then the counter will go up. is there anyway to help fix this. oh I am using boolean to make the switch act as a toggle.

I find that hard to figure out.

I think your code is confusing the input from the sensor with the input from the button. In your code it seems that the currentState is changed depending on the IR value. But the text I’ve quoted seems to suggest that the state should change with the button press.

I suspect if you give more meaningful names to your variables things would become clearer. For example look at these two lines

int val = 2; //ir sensor