0

I am a newbie with arduino. I am trying to have a counter with 3 buttons. Each Button when pressed will increase the number of counts. The counter should still count if button 2 is pressed while button 1 has not been released, and button 3 should also be able to increase the counter even if button 2 and button 1 are still pressed.

My code below counts if only one button is pressed at a time.

#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
int pbutton1 = 2;
int pbutton2 = 3;
int pbutton3 = 4;
int count = 0;

void setup() {
  lcd.begin(16, 2);
  pinMode (pbutton1, INPUT);
  pinMode (pbutton2, INPUT);
  pinMode (pbutton3, INPUT);

  lcd.print("My Counter");
}

void loop() {

  lcd.setCursor(0, 1);
  lcd.print("Total ");
  if (digitalRead(pbutton1) == HIGH) {
    lcd.print(count++);
    while (digitalRead(pbutton1) == HIGH); //monitor button to avoid continous count
  }

  if (digitalRead(pbutton2) == HIGH) {
    lcd.print(count++);
    while (digitalRead(pbutton2) == HIGH); //monitor button to avoid continous count
  }

  if (digitalRead(pbutton3) == HIGH) {
    lcd.print(count++);
    while (digitalRead(pbutton3) == HIGH); //monitor button to avoid continous count
  }
}
0

while (digitalRead(pbutton1) == HIGH); This blocks execution so nothing else can happen while the button is pressed. To avoid continous updating of the var while the button is pressed, create 3 booleans that keep track of the button state.

bool button1_is_pressed = false;
....
// If the button is pressed AND it was not previously pressed
if (digitalRead(pbutton1) == HIGH && !button1_is_pressed) {
    button1_is_pressed = true;
    lcd.print(count++);
}
// Not pressed - reset state variable
else if (digitalRead(pbutton1) == LOW) {
    button1_is_pressed = false;
}

Do this for the other buttons as well.

  • Hi! @Johnny, I have tried your code, it however gives a continuous count for as long as the button is pressed. In fact, a single push of the button already gives you like 38 on the counter, and not +1 – Kidd Oct 13 '17 at 15:11
  • @Kidd Could be a debouncing issue. I'll test it here and see what results I get... – Johnny Mopp Oct 13 '17 at 15:17
  • Thank you, I shall wait for your feedback, in the meantime. I will play around and see how it goes. – Kidd Oct 13 '17 at 15:19
  • Ok, I tested and had some minor issues with debouncing. Adding delay(10) to the end of loop() fixed it. Also, not sure how you are wired, but if you have your buttons wired directly to a pin, you should use the built in pullup resistors: pinMode (pbutton1, INPUT_PULLUP); – Johnny Mopp Oct 13 '17 at 15:28
  • I have the buttons connected to 1k ohms resistors then connected to the pins on the arduino – Kidd Oct 13 '17 at 15:46
0

I have managed to solve this using the PushButton library. If buttons are pressed simultaneously, the count still sums up when the button is released.

#include <LiquidCrystal.h>
#include <Pushbutton.h>

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
int count = 0;

#define button1 14
#define button2 15
#define button3 16

Pushbutton pbutton1(button1);
Pushbutton pbutton2(button2);
Pushbutton pbutton3(button3);

void setup() {
  lcd.begin(16, 2);
  lcd.print("My Counter");
}

void loop() {

  lcd.setCursor(0, 1);
  lcd.print("Total ");
    if (pbutton1.getSingleDebouncedPress()){
      count = count + 1; //increase the counter once the button is released
    }
    if (pbutton2.getSingleDebouncedPress()){
      count = count + 1; //increase the counter once the button is released
    }
    if (pbutton3.getSingleDebouncedPress()){
      count = count + 1; //increase the counter once the button is released
    }

  lcd.print(count);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.