-1

I am a newbie and also a noobie. Kindly assist this poor boy over here. I am looking for a way to increment a variable by one each time a button is pressed. Current approach is the following :

void buttonLoop() {
 int win = 0;
 while (win ==0) {
 display.setCursor(0, 0);
 //getButtons() function can be used to test if any button is pressed, or used like:
 //getButtons(TSButtonUpperLeft) to test a particular button, or even like:
 //getButtons(TSButtonUpperLeft|TSButtonUpperRight) to test multiple buttons
 //results are flipped as you would expect when setFlip(true)
 if (display.getButtons(TSButtonUpperLeft)) {
 display.println("Pressed!");
 score++;
 updateScore();
 } else {
 display.println(" ");
 }
 display.setCursor(0, 54);
 if (display.getButtons(TSButtonLowerLeft)) {
 display.println("Pressed!");
 score++;
 updateScore();
 } else {
 display.println(" ");
 }
 display.setCursor(95 - display.getPrintWidth("Pressed!"), 0);
 if (display.getButtons(TSButtonUpperRight)) {
 display.println("Pressed!");
 score++;
 updateScore();
 } else {
 display.println(" ");
 }
 display.setCursor(95 - display.getPrintWidth("Pressed!"), 54);
 if (display.getButtons(TSButtonLowerRight)) {
 display.println("Pressed!");
 score++;
 updateScore();
 } else {
 display.println(" ");
 }
}
}

In my case, the counter will keep increasing if I do not let go of the button. How do I make it so that the counter will only increase by 1 even if i continue holding onto the button.

Thank you for all the kind souls in this society :)

asked Nov 22, 2019 at 14:54
2
  • 1
    Look for the Bounce2 library in the IDE library manager. Commented Nov 22, 2019 at 15:47
  • check to see if the button was released before allowing the counter to increment again Commented Nov 22, 2019 at 16:35

1 Answer 1

0

See my answer in this thread:

How to connect Big Dome Button with arduino

That thread explains how to wire up a switch and write code that increments a counter each time a button is pressed.

The bits you care about:

Connect one lead of the switch to a digital input, and the other lead to ground.

Then set your switch pin to INPUT_PULLUP mode. Now your switch will read HIGH until it's pressed, then it will read LOW.

create a variable at the top of your code called buttonState:

byte buttonState = HIGH;

Also define an unsigned long variable nextReadMillis:

unsigned long nextReadMillis = 0;

and a pressCount variable:

unsigned int pressCount = 0;

And define a debounceTime:

#define debounceTime 50

In your loop, do something like this:

void loop() {
 //Get the new millis(value)
 unsigned long newMillis = millis();
 //only look at the new button value if enough time has elapsed.
 if (newMillis >= nextReadMillis) {
 byte newButtonState = digitalRead(buttonPin);
 //Only do something if the button state has changed
 if (buttonState != newButtonState) {
 buttonState = newButtonState;
 nextReadMillis = newMillis + debounceTime;
 //If the button is now in the pressed (LOW) state, increment the count
 if (buttonState == LOW) {
 pressCount++;
 }
 }
 }
}

That's pseudo-code. It likely has syntax errors. It's not meant to be copy/pasted. Use it as a guide for your project.

The code using millis() is a version of "software debouncing." Electrical switches tend to "jitter" or "bounce" between their on and off states very rapidly when they are first pressed/released. By only checking for state changes after a brief delay, you ignore the jitter in the readings.

You can adjust the value of debounceTime up or down a little. A value of 50 is 1/20th of a second, which should make your button presses quite responsive, but still debounce the switch well. If you find your count sometimes increases by more than one for a button press, you can increase the value to 100 or even 500, although at 500 (1/2 second) the button won't respond to really rapid presses

answered Nov 23, 2019 at 1:33

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.