Pages: [1]   Go Down
Author Topic: LyquidCristal / 10-1 = 90?  (Read 396 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi there,

I am running this little code to add / substract each time I press a button:

Code:
void setup() {
lcd.begin(16, 2);
lcd.setCursor(0,0);

lcd.print("Press a Button");
}
 
void loop() {

lcd.setCursor(0,1); // column 1, row 2

lcd.print(value);

lcd_key = read_LCD_buttons();
 
  switch (lcd_key) {
case btnUP:
value = value+1;
delay(300);
break;
case btnDOWN:
value = value-1;
delay(300);
break;
}
}

After I have pressed 10 times the value is calculated correctly being 10. If I then substract by pressing once it displays 90 instead of 9. Where is the error or what do I need to add / change?

Many thanks
aufruf
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 236
Posts: 24268
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You need to overwrite the trailing zero you printed for 10
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45935
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

15
14
13
12
11
10
9
8
7
Do you see why, when you don't overwrite the zero, 10 - 1 = 90?

Print a space after the value, and the problem will go away.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi both,

Thanks a lot.

I was going to write a lot of if statements - so thanks for the help  smiley-red

Would it be better style to call a function doing the calculation or is the Switch statement fine?

Cheers!
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 236
Posts: 24268
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'd go for right-justification.
Before you print your value, test to see if it is less than 10, and if so, print a space.
Then print the value.
(May need extra work for negative values)
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45935
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Would it be better style to call a function doing the calculation or is the Switch statement fine?
Neither? switch statements are generally preferred when there are 4 or more cases. With only two cases, if statements are faster. Of course, that just means that you get to the delay() sooner, so, never mind.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hehe, thanks for that  smiley

I have put the delay in there so that I don't get a very high number although I just pressed once. Assuming from your comment that there are better ways to realize this?

Thanks!
Logged

Finland
Offline Offline
Jr. Member
**
Karma: 1
Posts: 84
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No need for the delay there. First, you can read the button state once, then another time to make sure it's really down, called debouncing. Mark the time with millis() to a variable. Then at action time you can check has it been too soon since the last action, and do nothing if so. If it has been long enough, do the action, addition or subtraction in your case.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45935
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
No need for the delay there. First, you can read the button state once, then another time to make sure it's really down, called debouncing.
That long delay there is not to avoid bouncing. It's there because OP is not detecting the state change. Therefore, one press and release takes a while.

OP: http://arduino.cc/en/Tutorial/ButtonStateChange might be of interest.
Logged

Offline Offline
Edison Member
*
Karma: 27
Posts: 2033
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So your current code will only work if you press the button for less than 300 ms.      And it also won't
work if you do two presses in a short time,  because the second one will be during the delay.  So if you
want this counter to be reliable,  well it won't be.

For your next lesson,  read up on debouncing  and on interrupts 
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45935
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
For your next lesson,  read up on debouncing  and on interrupts 
There is no reason to be using interrupts to read switches pressed by humans.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote

Hi Paul,

thanks a lot for the tip. This is what my code now looks like and works like a charm. Many thanks!

Code:
void loop() {
lcd.setCursor(0,1);

lcd.print(value);
lcd.print(" "); // Overwriting Zero

buttonState = read_LCD_buttons();

// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
         // if the state has changed, perform action
         if (buttonState == btnUP) {
value = value+1;
         }
         if (buttonState == btnDOWN) {
value = value-1;
         }
}

lastButtonState = buttonState; 
}
Logged

Pages: [1]   Go Up
Jump to: