Hello all
I need some help debugging my code as it seems to work to a point. The code is some basic lcd screens that have different functions, a couple of the screens have intigers that can be changed and saved to eeprom.
Currently, it works fine when you use the up and down buttons to scroll through the options in the menu and will options. Once in a sub menu, when I use the up and down buttons to adjust the integers I’m finding that I suddenly get kicked out of the sub menu and back to the main menu before I can save anything. When I then try to enter back into one of the sub menus the LCD screen then goes blank. Any ideas on why this might be happening?
#include <EEPROM.h>
#include <LiquidCrystal_I2C.h>
const int up=6;
const int down =7;
const int save =8;
const int test =11;
const int back =12;
int screenCounter = 0;
int testbutcounter = 0;
LiquidCrystal_I2C lcd(0x3f, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~EEPROM ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int eeAddressBoiler = 0; //EEPROM address to start reading from
int boilerTarget = 000;
int countTemp = EEPROM.get(eeAddressBoiler, boilerTarget);
int eeAddressSteam = 4;
int steamTarget = 000;
int countSteam = EEPROM.get(eeAddressSteam, steamTarget);
void memoryClear(){
lcd.setCursor(0, 0);
lcd.print("Memory Reset Press");
lcd.setCursor(0, 1);
lcd.print("Up & Save to Clear");
lcd.setCursor(0, 2);
lcd.print("OR Up & Down to Exit");
if (digitalRead(up) == LOW && digitalRead(save) == LOW){
for (int i = 0 ; i < EEPROM.length() ; i++)
EEPROM.write(i, 0);
lcd.setCursor(0, 3);
lcd.print("Memory Cleared");
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void boilertempSetting() {
lcd.setCursor(0, 0);
lcd.print("Boiler Target Temp");
lcd.setCursor(0, 1);
lcd.print("C=");
lcd.setCursor(3, 1);
lcd.print(countTemp);
lcd.setCursor(0, 2);
lcd.print("Press Save to Save ");
lcd.setCursor(0, 3);
lcd.print("To Exit press back ");
if (countTemp > 148 || countTemp < 0) {
countTemp = 0;
}
if (countTemp < 10) {
lcd.setCursor(4, 1);
lcd.print(" ");
}
if (countTemp < 100) {
lcd.setCursor(5, 1);
lcd.print(" ");
}
if (digitalRead(up) == LOW)
{
countTemp ++; // Increment Count by 1
lcd.setCursor(3, 1);
lcd.print(countTemp);
delay(400);
}
if (digitalRead(down) == LOW)
{
countTemp --; // Decrement Count by 1
if (countTemp < 0)
countTemp = 0;
lcd.setCursor(3, 1);
lcd.print(countTemp);
delay(400);
}
if (digitalRead(save) == LOW)
{
EEPROM.put(eeAddressBoiler, countTemp);
lcd.setCursor(7, 1);
lcd.print("Saved");
}
if (digitalRead(back) == LOW)
{
screenCounter = 0;
testbutcounter = 0;
}
}
void steamtempSetting() {
lcd.setCursor(0, 0);
lcd.print("Steam Target Temp");
lcd.setCursor(0, 1);
lcd.print("C=");
lcd.setCursor(3, 1);
lcd.print(countSteam);
lcd.setCursor(0, 2);
lcd.print("Press Save to Save ");
lcd.setCursor(0, 3);
lcd.print("To Exit press back ");
if (countSteam < 10) {
lcd.setCursor(4, 1);
lcd.print(" ");
}
if (countTemp < 100) {
lcd.setCursor(5, 1);
lcd.print(" ");
}
if (countSteam > 148 || countSteam < 0) {
countSteam = 0;
}
if (digitalRead(up) == LOW)
{
countSteam = countSteam + 1; // Increment Count by 1
lcd.setCursor(3, 1);
lcd.print(countSteam);
delay(400);
}
if (digitalRead(down) == LOW)
{
countSteam = countSteam - 1; // Decrement Count by 1
if (countSteam < 0)
countSteam = 0;
lcd.setCursor(3, 1);
lcd.print(countSteam);
delay(400);
}
if (digitalRead(save) == LOW)
{
EEPROM.put(eeAddressSteam, countSteam);
lcd.setCursor(7, 1);
lcd.print("Saved");
}
if (digitalRead(back) == LOW)
{
screenCounter = 0;
testbutcounter = 0;
}
}
void testScreens() {
Serial.print(screenCounter);
Serial.println(testbutcounter);
if (testbutcounter > 2 || testbutcounter < 0 ) {
testbutcounter = 0;
}
if (screenCounter > 4 || screenCounter < 0) {
screenCounter = 0;
}
if (digitalRead(up) == LOW)
{
screenCounter ++; // Increment Count by 1
delay(400);
}
if (digitalRead(down) == LOW)
{
screenCounter --; // Decrement Count by 1
if (screenCounter < 0)
countTemp = 0;
delay(400);
}
enum screens { TEST, BOILERTARG, BOILERTARGA, STEAMTARG, STEAMTARGA };
int currentScreen = -1;
if ( currentScreen != TEST && screenCounter == 0 ) {
testmenu();
currentScreen = TEST;
}
if ( currentScreen != BOILERTARG && screenCounter == 1 ) {
boiler();
currentScreen = BOILERTARG;
if (digitalRead (test) == LOW) {
testbutcounter ++;
screenCounter ++;
lcd.clear();
}
}
if (currentScreen != BOILERTARGA && screenCounter == 2 && testbutcounter == 1) {
boilerA();
currentScreen = BOILERTARGA;
}
if( currentScreen != STEAMTARG && screenCounter == 3 ) {
steam();
currentScreen = STEAMTARG;
if (digitalRead (test) == LOW) {
testbutcounter ++;
screenCounter ++;
lcd.clear();
}
}
if (currentScreen != STEAMTARGA && screenCounter == 4 && testbutcounter == 1) {
steamA();
currentScreen = STEAMTARGA;
}
}
void testmenu() {
lcd.setCursor(0, 0);
lcd.print("Cleaning & Setup ");
lcd.setCursor(0, 1);
lcd.print ("Use UP & Down ");
lcd.setCursor(0, 2);
lcd.print (" ");
lcd.setCursor(0, 3);
lcd.print("To Navigate ");
}
void boiler() {
lcd.setCursor(0, 0);
lcd.print("Boiler Target ");
lcd.setCursor(0, 1);
lcd.print("To change the target");
lcd.setCursor(0, 2);
lcd.print("temp of the boiler");
lcd.setCursor(0, 3);
lcd.print("Press test to enter");
}
void boilerA() {
boilertempSetting();
}
void steam() {
lcd.setCursor(0, 0);
lcd.print("Steam Target ");
lcd.setCursor(0, 1);
lcd.print("To change the target");
lcd.setCursor(0, 2);
lcd.print("temp of the steam ");
lcd.setCursor(0, 3);
lcd.print("Press test to enter");
}
void steamA(){
steamtempSetting();
}
void setup() {
Serial.begin(9600);
lcd.begin(20, 4); // initialize the lcd for 20 chars 4 lines
lcd.backlight(); // Turns backlight LCD on
pinMode (up,INPUT_PULLUP);
digitalWrite (up,HIGH);
pinMode (down,INPUT_PULLUP);
digitalWrite (down,HIGH);
pinMode (save,INPUT_PULLUP);
digitalWrite (save,HIGH);
pinMode (test,INPUT_PULLUP);
digitalWrite (test,HIGH);
pinMode (back,INPUT_PULLUP);
digitalWrite (back,HIGH);
}
void loop() {
testScreens();
}