# Push button and repeat

**URL:** <https://forum.arduino.cc/t/push-button-and-repeat/565284>\
**Category:** Programming\
**Created:** [January 2, 2019, 8:44pm UTC](https://forum.arduino.cc/t/push-button-and-repeat/565284 "2019-01-02T20:44:20Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![JohnRandalls](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@JohnRandalls](https://forum.arduino.cc/u/JohnRandalls)\
**Post date:** [January 2, 2019, 8:44pm UTC](https://forum.arduino.cc/t/push-button-and-repeat/565284/1 "2019-01-02T20:44:20Z")

</div>

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?

```cpp
#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

---

<div class="post-metadata">

**Author:** ![Perehama](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/perehama/32/256713_2.png) [@Perehama](https://forum.arduino.cc/u/Perehama)\
**Post date:** [January 2, 2019, 8:58pm UTC](https://forum.arduino.cc/t/push-button-and-repeat/565284/2 "2019-01-02T20:58:20Z")

</div>

What parts of this code did you write yourself?

What parts of this code do you not understand?

---

<div class="post-metadata">

**Author:** ![lesept](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/lesept/32/238959_2.png) [@lesept](https://forum.arduino.cc/u/lesept)\
**Post date:** [January 2, 2019, 10:25pm UTC](https://forum.arduino.cc/t/push-button-and-repeat/565284/3 "2019-01-02T22:25:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![JohnRandalls](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@JohnRandalls](https://forum.arduino.cc/u/JohnRandalls)\
**Post date:** [January 3, 2019, 1:02am UTC](https://forum.arduino.cc/t/push-button-and-repeat/565284/4 "2019-01-03T01:02:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lesept](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/lesept/32/238959_2.png) [@lesept](https://forum.arduino.cc/u/lesept)\
**Post date:** [January 3, 2019, 1:21pm UTC](https://forum.arduino.cc/t/push-button-and-repeat/565284/5 "2019-01-03T13:21:34Z")

</div>

Try this (not tested) :

```cpp
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;
}

```

---

<div class="post-metadata">

**Author:** ![JohnRandalls](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@JohnRandalls](https://forum.arduino.cc/u/JohnRandalls)\
**Post date:** [January 4, 2019, 12:50am UTC](https://forum.arduino.cc/t/push-button-and-repeat/565284/6 "2019-01-04T00:50:45Z")

</div>

```cpp
[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

---

<div class="post-metadata">

**Author:** ![Blackfin](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/blackfin/32/238871_2.png) [@Blackfin](https://forum.arduino.cc/u/Blackfin)\
**Post date:** [January 4, 2019, 2:49am UTC](https://forum.arduino.cc/t/push-button-and-repeat/565284/7 "2019-01-04T02:49:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lesept](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/lesept/32/238959_2.png) [@lesept](https://forum.arduino.cc/u/lesept)\
**Post date:** [January 4, 2019, 2:07pm UTC](https://forum.arduino.cc/t/push-button-and-repeat/565284/8 "2019-01-04T14:07:23Z")

</div>

try adding`delay(30);`after line ` up_lastButtonState = up_buttonState;`

---

<div class="post-metadata">

**Author:** ![JohnRandalls](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@JohnRandalls](https://forum.arduino.cc/u/JohnRandalls)\
**Post date:** [January 4, 2019, 3:00pm UTC](https://forum.arduino.cc/t/push-button-and-repeat/565284/9 "2019-01-04T15:00:45Z")

</div>

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

Thanks again for the guidance.

---

<div class="post-metadata">

**Author:** ![lesept](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/lesept/32/238959_2.png) [@lesept](https://forum.arduino.cc/u/lesept)\
**Post date:** [January 4, 2019, 5:18pm UTC](https://forum.arduino.cc/t/push-button-and-repeat/565284/10 "2019-01-04T17:18:01Z")

</div>

Good!

---

<div class="post-metadata">

**Author:** ![Danois90](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@Danois90](https://forum.arduino.cc/u/Danois90)\
**Post date:** [January 4, 2019, 6:17pm UTC](https://forum.arduino.cc/t/push-button-and-repeat/565284/11 "2019-01-04T18:17:12Z")

</div>

Using a [library](https://github.com/bswebdk/TDuino/) with the features built in is simpler:

```cpp
#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();
}

```

---

<div class="post-metadata">

**Author:** ![JohnRandalls](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@JohnRandalls](https://forum.arduino.cc/u/JohnRandalls)\
**Post date:** [January 5, 2019, 1:59am UTC](https://forum.arduino.cc/t/push-button-and-repeat/565284/12 "2019-01-05T01:59:35Z")

</div>

> Danois90:  
> Using a [library](https://github.com/bswebdk/TDuino/) with the features built in is simpler:

Let me give this a shot, Appreciate this.

---

<div class="post-metadata">

**Author:** ![JohnRandalls](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@JohnRandalls](https://forum.arduino.cc/u/JohnRandalls)\
**Post date:** [January 5, 2019, 2:17am UTC](https://forum.arduino.cc/t/push-button-and-repeat/565284/13 "2019-01-05T02:17:24Z")

</div>

> Danois90:  
> Using a [library](https://github.com/bswebdk/TDuino/) with the features built in is simpler:

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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [January 5, 2019, 4:17am UTC](https://forum.arduino.cc/t/push-button-and-repeat/565284/14 "2019-01-05T04:17:17Z")

</div>

> 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](http://playground.arduino.cc/)to see what folk have done previously. There's quite a lot of button-related stuff [there](https://playground.arduino.cc/Main/InterfacingWithHardware#phi_interfaces).

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 11:33pm UTC](https://forum.arduino.cc/t/push-button-and-repeat/565284/15 "2021-05-05T23:33:20Z")

</div>

Closed on May 5, 2021, 11:33pm UTC


