When i press a button to subtract 1 from 5 it skips to 3, it's the same when I add 1 to -5 it skips displaying - 4 and goes straight to -3.
This is the code that i am using:
//Testing the use of addition and subtraction with the menu options.
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int lcd_key = 0;
int adc_key_in = 0;
int calc_total;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
int read_LCD_buttons(){
adc_key_in = analogRead(0);
if (adc_key_in > 1000) return btnNONE;
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 150) return btnUP;
if (adc_key_in < 300) return btnDOWN;
if (adc_key_in < 450) return btnLEFT;
if (adc_key_in < 700) return btnSELECT;
return btnNONE;
}
void setup(){
lcd.begin(16, 2);
lcd.setCursor(0,0);
lcd.print (calc_total);
}
void loop(){
lcd_key = read_LCD_buttons();
calc_total = constrain(calc_total, -4, 4);
switch (lcd_key){
case btnRIGHT:{
lcd.clear();
calc_total = calc_total - 1;
lcd.print (calc_total);
delay(700);
lcd.setCursor(0,0);
break;
}
case btnLEFT:{
lcd.clear();
calc_total = calc_total + 1;
lcd.print (calc_total);
delay(700);
lcd.setCursor(0,0);
break;
}
}
}