Go Down

Topic: LyquidCristal / 10-1 = 90? (Read 2028 times)previous topic - next topic

aufruf

Jan 28, 2013, 08:48 pm
Hi there,

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

Code: [Select]
`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

AWOL

#1
Jan 28, 2013, 08:52 pm
You need to overwrite the trailing zero you printed for 10
"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.
I speak for myself, not Arduino.

PaulS

#2
Jan 28, 2013, 08:52 pm
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.

aufruf

#3
Jan 28, 2013, 09:11 pm
Hi both,

Thanks a lot.

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

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

Cheers!

AWOL

#4
Jan 28, 2013, 09:13 pm
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)
"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.
I speak for myself, not Arduino.

PaulS

#5
Jan 28, 2013, 09:15 pm
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.

aufruf

#6
Jan 28, 2013, 09:30 pm
Hehe, thanks for that

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!

Chaul

#7
Jan 28, 2013, 10:05 pm
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.

PaulS

#8
Jan 28, 2013, 10:59 pm
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.

michinyon

#9
Jan 28, 2013, 11:03 pm
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.

PaulS

#10
Jan 28, 2013, 11:15 pm
Quote

There is no reason to be using interrupts to read switches pressed by humans.

aufruf

#11
Jan 29, 2013, 11:13 pm

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

Hi Paul,

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

Code: [Select]
`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;  }`

Go Up