Hey, new here.. I have been working on a school project, making a counter using LCD and a push button.. it is working for me but it counts slowly

by the way.. i tried changing resistors , but it still counts slowly, it feels like the button is not responding well.
i needed to hold the push button for it to count.
using this code:

LiquidCrystal_I2C lcd(0x27,16,2);
void setup() {
  // put your setup code here, to run once:
  lcd.init();
  lcd.backlight();
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT);
pinMode(8, OUTPUT);
pinMode(10, OUTPUT);
}
int num=0;
void loop() {
  // put your main code here, to run repeatedly:
  lcd.begin(16,2);

      
  if(digitalRead(2)==0){
    num=num+1;
    lcd.print(num);
     
     
  }
    else
    {
    lcd.print(num);  
    }
    
    
   
}```

according to your code, the counter will increment (+1) each time the button is pressed. EDIT: at each occurence of loop() if button is pressed.

May I propose you to hit the button quicker?

Serioulsy, could you ask with more details what you're expecting from your code, and what is actually displaying on your screen?

EDIT: your button is setup with pullup resistor. it is okay for the usage, and so you don't need resistor on your button wiring.

Great you posted the code correctly in code tags.
A pity you did not post all the code so that it would actually compile.

Should be in the setup function not the loop function, you only need to do it once.

2 Likes

In general, you should not use magic numbers. The I/O pins love to have a functional name.

the project is to make a counter with LCD and push button.
i have to hold the button for 2 to 3 seconds for the counter to increment +1.
my goal is to make it faster (increments with a quick push).

The loop function will run faster if you remove the "else" statement and you "lcd.begin" in the setup.

It worked, thank you!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.