I am working on implementing a LCD menu for a project that lets you increment a variable on each sub menu.
Arduino Uno
LCD Key Pad Shield (D1)Robot
/*
* Menu
*/
//For LCD screen functions
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
const int rightButton = 0; // 23
const int upButton = 1; // 1323
const int downButton = 2; // 3083
const int leftButton = 3; // 4813
const int selectButton = 4; // 7232
const int noButtonSelected = 5; // 1023
unsigned int buttonPressed = 0;
unsigned int mainMenuChoice = 0;
unsigned int subMenuChoice = 0;
//To hold user value choices
unsigned int chooseO3 = 0;
unsigned int chooseCycle = 0; //
// put your setup code here, to run once:
void setup()
{
lcd.begin(16,2);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("KWJ ENGINEERING");
lcd.setCursor(0,1);
lcd.print("SPEC SENSORS");
delay (1500);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("S4PPE Project");
lcd.setCursor(0,1);
lcd.print("Version 2.4");
delay(1500);
}
// put your main code here, to run repeatedly:
void loop()
{
mainMenuChoice = readButtons();
// Main Menu
switch (mainMenuChoice)
{
case noButtonSelected:
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Select o3Conc");
lcd.setCursor(0,1);
lcd.print(chooseO3);
lcd.setCursor(3,1);
lcd.print("ppm");
delay(50);
break;
}
case upButton:
{
chooseO3++;
break;
}
case downButton:
{
chooseO3--;
break;
}
}
}
int readButtons ()
{
buttonPressed = analogRead(0);
if (buttonPressed > 1000) return noButtonSelected;
//rightButton read at 0
if (buttonPressed < 50) return rightButton;
//upButton read at 144
if (buttonPressed < 250) return upButton;
//downButton read at 329
if (buttonPressed < 450) return downButton;
//leftButton read at 504
if (buttonPressed < 650) return leftButton;
//selectButton read at 741
if (buttonPressed < 850) return selectButton;
}
Here is how I am incrementing my variable chooseO3
case upButton:
{
chooseO3++;
break;
}
case downButton:
{
chooseO3--;
break;
}
OUTPUT
Select o#Conc
0 ppm.
After using up or down button though the value of chooseO3 jumps up and down in value instead of being incremented or decremented by one.
What am I missing.