Push button and repeat

I have this code that I am trying to make as a good working example. The code works fine, the only thing that’s missing that i haven’t been able to figure out is where and what to add to the code to make the numbers increment/decrement faster when the button is held down.

Can someone help me out here?

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 20, 4);

const int Up_buttonPin   = 2;
const int Down_buttonPin = 3;

// Variables will change:
int buttonPushCounter = 0;
int up_buttonState = 0;
int up_lastButtonState = 0;

int down_buttonState = 0;
int down_lastButtonState = 0;
bool bPress = false;

void checkUp()
{
  up_buttonState = digitalRead(Up_buttonPin);

  if(up_buttonState != up_lastButtonState) 
  {
    if(up_buttonState == LOW) 
    {
      bPress = true;
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } 
    else 
    {
      Serial.println("off");
    }
    delay(50);
  }

  up_lastButtonState = up_buttonState;
}

void checkDown()
{
  down_buttonState = digitalRead(Down_buttonPin);

  if(down_buttonState != down_lastButtonState) 
  {
    if(down_buttonState == LOW) 
    {
      bPress = true;
      buttonPushCounter--;
     
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } 
    else 
    {
      Serial.println("off");
    }
    delay(50);
  }
  down_lastButtonState = down_buttonState;
}

void setup()
{
  Serial.begin(9600);
  pinMode(Up_buttonPin , INPUT_PULLUP);
  pinMode(Down_buttonPin , INPUT_PULLUP);
 
  lcd.init();
 
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Please Select:");
  lcd.setCursor(14, 0);
  lcd.print(buttonPushCounter);
}

void loop()
{
  checkUp();
  checkDown();

  if(bPress)
  {
    bPress = false;
    lcd.setCursor(14, 0);
    lcd.print("  ");
    lcd.setCursor(14, 0);
    if(buttonPushCounter < 0)
    {
      buttonPushCounter = 0;
    }
      
    lcd.print(buttonPushCounter);
  }
}

Appreciate some help.

Thanks

What parts of this code did you write yourself?

What parts of this code do you not understand?

checkUp and checkDown are pretty much the same, so I'll deal only with checkUp.

In the checkUp function, when you test
if(up_buttonState != up_lastButtonState) you check if the button state changed and act accordingly. But you don't do anything if the state didn't change, which is what you want to catch.
So you need to add a 'else' there.

The delay(50) is ok for debounce, and you can decrease it down to 20 or 30 (to be tested). If you want to increment faster, also decrease that delay in the 'else' you'll be adding.

Oh man I am not sure what to do there. I bought this arduino kit about 7 months ago to start learning how to do some cool things with this board. I started using it finally over the weekend, but when I got to the button counter example I didn't think it was a practical example.

It doesn't make sense to have to push the button 100 times to count to 100 or to decrement back to zero. I wanted to add that missing piece in there so I could have an example that I could eventually use when I need it.

Imagine setting the time on your watch and you have to keep pressing the button until you get the time correct, I think thats how it was in the old days but not anymore.

The only piece to me thats missing in the code is to hold the button down and have it continue counting without pressing over and over.

Try this (not tested) :

void checkUp()
{
  up_buttonState = digitalRead(Up_buttonPin);
  if (up_buttonState != up_lastButtonState)
  {
    if (up_buttonState == LOW)
    {
      bPress = true;
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    }
    else
    {
      Serial.println("off");
    }
    delay(50);
  } else {
    if (up_buttonState == LOW) {
      buttonPushCounter++;
      delay(100);
    }
  }
  up_lastButtonState = up_buttonState;
}
[quote author=lesept link=msg=4002869 date=1546521694]
  } else {
    if (up_buttonState == LOW) {
      buttonPushCounter++;
      delay(100);
    }
  }
  up_lastButtonState = up_buttonState;
}

[/quote]

Lesept,

That worked pretty good. Only issue I have is when I press the button one time the numbers double up like 0,2,4,6,8,10 etc. When i hold down the button down for a while the numbers go in sequence like it should.

Am I overlooking something else?

Thanks

You may be seeing the effects of bounce. You might want to qualify the button presses with a debounce-on-change call and pass the results of that to checkUp() as a parameter.

try addingdelay(30);after line  up_lastButtonState = up_buttonState;

Added the delay in and played around with the other delay values, now it works as expected.

Thanks again for the guidance.

Good!

Using a library with the features built in is simpler:

#include <TDuino.h>
#define BUTTON_UP 3
#define BUTTON_DOWN 4

TButton buttons[2];

int counter = 0;

void button_callback(byte pin, int state)
{
  if (pin == BUTTON_UP) counter++;
  else counter--;
  Serial.print("Counter = ");
  Serial.println(counter);
}

void setup()
{
  Serial.begin(9600);
  for (byte i = 0; i < 2; i++)
  {
    buttons[i].attach(i == 0 ? BUTTON_UP : BUTTON_DOWN);
    buttons[i].setRepeat(500, 250);
    buttons[i].onPress(button_callback);
  }
}

void loop()
{
  for (byte i = 0; i < 2; i++) buttons[i].loop();
}

Danois90:
Using a library with the features built in is simpler:

Let me give this a shot, Appreciate this.

Danois90:
Using a library with the features built in is simpler:

Damn dude, I just tried it and that was SUPER easy.

JohnRandalls:
Damn dude, I just tried it and that was SUPER easy.

There's a library for pretty much anything you might want to try on an Arduino, it seems. Lucky for us, we're usually not the first people to try a thing, and some kind soul published their wisdom in a library.

Have a look (for example) in the Playground to see what folk have done previously. There's quite a lot of button-related stuff there.