Go Down

Topic: make each click go through one loop (Read 130 times) previous topic - next topic

ziyangs1

Aug 22, 2019, 03:43 pm Last Edit: Aug 22, 2019, 04:32 pm by ziyangs1
I have an LCD shield with buttons: LEFT, RIGHT, DOWN, UP, and SELECT.
Then I have the code:

Code: [Select]

uint8_t x = 0;
void loop(){

      buttons = lcd.readButtons();
      if (buttons){
             if (buttons & BUTTON_RIGHT){
                      x = x + 1;
                      lcd.print(x);
             }
      }

}

What I want to do is to add 1 to x and print it on the LCD each time I press the button. But the problem is if I push a button for longer, x will increase more. Are there any methods to use click to control the loop?
             










septillion

#1
Aug 22, 2019, 03:50 pm Last Edit: Aug 22, 2019, 09:12 pm by septillion
Hi, welcome! Please first have a look at "How to use the forum" before responding.

Next, have a look at the "State change detection" example in the IDE. Key is to understand the difference between a key being pressed (= a state) and a key becoming pressed (= event).
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

ziyangs1

Thank you! I checked the example and the library. From the example you gave me, I think it's doing something like debouncing. But I also noticed that the button is auto debounced in the library. I am not sure what the problem is.

septillion

I meant to write "example in the IDE". So no idea at which library you looked. But have a look at the IDE example. Don't expect much bounce from a touchscreen library.
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

Paul__B

#4
Aug 23, 2019, 12:07 am Last Edit: Aug 23, 2019, 12:09 am by Paul__B
I meant to write "example in the IDE". So no idea at which library you looked. But have a look at the IDE example. Don't expect much bounce from a touchscreen library.
OK, so (s)he posted the code properly but failed to provide the whole code.  :smiley-roll:

Read the first post.  This is not a touchscreen.

Also need to read the warning!

Go Up