Making buttons faster

Hello, I have an LCD-display which displays a balance, and i have four buttons which adds the balance, and when i press the button many times in a row it only adds up the balance about once a second, like if i press the +10 button 10 times in 5 seconds the arduino only adds up half of what i pressed (so total balance will be 50 even though i pressed 10 times) It seems to work fine if i press the four buttons simultaneously then it will add up everything. But there is a delay for each button. I hope you understand what my problem is!

milkshakeman21: I hope you understand what my problem is!

Sort of, but for a solution you'll need to post the code you have the problem with, and a schematic of the circuit.

I bet there are a lot of delay() in your code, but without seeing it we can only guess.

Please post your code, and use code tags </>.

#include <LiquidCrystal.h>
#include <Button.h>

Button btnAddTen(8);
Button btnAddFive(9);
Button btnAddTwo(10);
Button btnAddOne(11);

const int ten = 10;
const int five = 5;
const int two = 2;
const int one = 1;

LiquidCrystal lcd(1, 2, 4, 5, 6, 7);

int saldo = 0;

void setup() {
  // put your setup code here, to run once:
btnAddTen.begin();
btnAddFive.begin();
btnAddTwo.begin();
btnAddOne.begin();
  
  lcd.begin(16,2);
  lcd.print("Saldo: "); 
}

void loop() {
  // put your main code here, to run repeatedly:
  
  lcd.setCursor(7,0);
  lcd.print(saldo);
  lcd.print(" kr");

  if(btnAddTen.pressed()){
  saldo = saldo + ten;
  }

if(btnAddFive.pressed()){
  saldo = saldo + five;
  }

if(btnAddTwo.pressed()){
  saldo = saldo + two;
  }

  if(btnAddOne.pressed()){
  saldo = saldo + one;
  }
 
  
}

There is the code i currently have.

and here is he schematics. Hope this helps! I can try to make a video to show my problem if i got time :slight_smile:

This

  lcd.setCursor(7,0);
  lcd.print(saldo);
  lcd.print(" kr");

will slow down your loop a lot as you do it every time, and it will look really bad on the LCD.

You need to update the display only when it changes.

if (saldo != old_saldo)
{
  lcd.setCursor(7,0);
  lcd.print(saldo);
  lcd.print(" kr");
  old_saldo = saldo;
}

Off the point a bit, but does the button library do hidden pinModes with INPUT_PULLUPs? Your diagram shows that the buttons take their pins to ground but you have no pullup resistors, nor pinModes of your own in setup().

#include <Button.h>

There are several libraries with this name. Can you please provide a link to the one which you are using?

cattledog:

#include <Button.h>

There are several libraries with this name. Can you please provide a link to the one which you are using?

I am using Buttons by Michael Adams
I was following some steps in a book about Arduino and they recommended that i use that libary :slight_smile:

marco_c:
This

  lcd.setCursor(7,0);

lcd.print(saldo);
  lcd.print(" kr");



will slow down your loop a lot as you do it every time, and it will look really bad on the LCD.

You need to update the display only when it changes.


if (saldo != old_saldo)
{
  lcd.setCursor(7,0);
  lcd.print(saldo);
  lcd.print(" kr");
  old_saldo = saldo;
}

I tried that but it did not work either, here is a video about my problem, you can see when i press the first button many times it does not register every click, but if i click all 4 at the same time there is no problem, seems like there is a delay of some sort, i also noticed that the tx bulb on the arduino is blinking maybe that has something to do with it? looking forward to some answers… :slight_smile:
Video: https://www.youtube.com/watch?v=P2ZRPyDKTwA

There is a button reading lock out time (for debounce) in that library with an initial default value of 500 milliseconds. Repeated presses in that time period will not be recognized.

Find the library in libraries folder in the file with your sketches, and navigate your way to buttons.h and open it with a text editor. Find this code and change _delay(500) to _delay(10). Save the change. The button should be more responsive, but if you start seeing multiple events due to bounce, increase the time.

Button::Button(uint8_t pin)
:  _pin(pin)
,  _delay(500)
,  _state(HIGH)
,  _ignore_until(0)
,  _has_changed(false)
{
}

Depending on your computer operating system and how the library is treated you may need administrator permissions to modify the library.

I was following some steps in a book about Arduino and they recommended that i use that libary :slight_smile:

Button libraries have their place (I sometimes use Bounce2.h) but if you are a beginner I think it is better to develop the programming skills to handle simple button transitions rather than to rely on a library for simple tasks.

Here is the read() function which uses the _delay value for lock out.

bool Button::read()
{
 // ignore pin changes until after this delay time
 if (_ignore_until > millis())
 {
 // ignore any changes during this period
 }
 
 // pin has changed 
 else if (digitalRead(_pin) != _state)
 {
 _ignore_until = millis() + _delay;
 _state = !_state;
 _has_changed = true;
 }
 
 return _state;
}

cattledog: There is a button reading lock out time (for debounce) in that library with an initial default value of 500 milliseconds. Repeated presses in that time period will not be recognized.

Find the library in libraries folder in the file with your sketches, and navigate your way to buttons.h and open it with a text editor. Find this code and change _delay(500) to _delay(10). Save the change. The button should be more responsive, but if you start seeing multiple events due to bounce, increase the time.

Button::Button(uint8_t pin)
:  _pin(pin)
,  _delay(500)
,  _state(HIGH)
,  _ignore_until(0)
,  _has_changed(false)
{
}

Depending on your computer operating system and how the library is treated you may need administrator permissions to modify the library.

Button libraries have their place (I sometimes use Bounce2.h) but if you are a beginner I think it is better to develop the programming skills to handle simple button transitions rather than to rely on a library for simple tasks.

Here is the read() function which uses the _delay value for lock out.

bool Button::read()
{
 // ignore pin changes until after this delay time
 if (_ignore_until > millis())
 {
 // ignore any changes during this period
 }

// pin has changed else if (digitalRead(_pin) != _state) { _ignore_until = millis() + _delay; _state = !_state; _has_changed = true; }

return _state; }

THANK YOU SO MUCH!!! That fixed the problem!! :) i get that as a beginner you should learn that programming from scratch but that make things 10x harder imo, just reading your code made me shiver hehe ;) but thank you so much ^^

There is a button reading lock out time (for debounce) in that library with an initial default value of 500 milliseconds.

Wow, that is excessive. It does mot need to be any more that 50ms. After 200ms it becomes a noticeable delay when a user presses the switch.

For your education, read this http://www.eng.utah.edu/~cs5780/debouncing.pdf as a pdf or the same info as blog post http://www.ganssle.com/debouncing.htm. This is the best explanation of switch debouncing I have found.