LyquidCristal / 10-1 = 90?

Hi there,

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

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

You need to overwrite the trailing zero you printed for 10

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.

Hi both,

Thanks a lot.

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

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

Cheers!

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)

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.

Hehe, thanks for that :slight_smile:

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!

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.

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.

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

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.

PaulS:

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!

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;  

}