Hello,
I have a problem with my Program, if I open my Arduino it’s goes only one time through the Program until i press any button in the LCD, it’s goes through the Program for the second time.
How can i make it continuous without pressing any button.
This is my code:
#include <Wire.h>
#include <LiquidCrystal.h>
#include <DallasTemperature.h>
#include <OneWire.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int Tauchsieder = 3;
int Heizplatte = 11;
int PeltierElement = 12;
String menuItems[] = {"Wasser Ist", "Zeolith Ist"};
int readKey;
int savedDistance = 0;
int menuPage = 0;
int maxMenuPages = round(((sizeof(menuItems) / sizeof(String)) / 2) + .5);
int cursorPosition = 0;
byte downArrow[8] = {
0b00100,
0b00100,
0b00100,
0b00100,
0b00100,
0b10101,
0b01110,
0b00100
};
byte upArrow[8] = {
0b00100,
0b01110,
0b10101,
0b00100,
0b00100,
0b00100,
0b00100,
0b00100
};
byte menuCursor[8] = {
B01000,
B00100,
B00010,
B00001,
B00010,
B00100,
B01000,
B00000
};
void setup() {
Serial.begin(9600);
sensors.begin();
lcd.begin(16, 2);
lcd.clear();
lcd.createChar(0, menuCursor);
lcd.createChar(1, upArrow);
lcd.createChar(2, downArrow);
pinMode(Tauchsieder, OUTPUT);
pinMode(Heizplatte, OUTPUT);
pinMode(PeltierElement, OUTPUT);
}
void loop() {
digitalWrite(Tauchsieder,LOW);
Serial.println("Tauchsieder Aus");
digitalWrite(Heizplatte,LOW);
Serial.println("Heizplatte Aus");
digitalWrite(PeltierElement,LOW);
Serial.println("Peltier-Element Aus");
delay(1000);
mainMenuDraw();
drawCursor();
operateMainMenu();}
void mainMenuDraw() {
lcd.clear();
lcd.setCursor(1, 0);
lcd.print(menuItems[menuPage]);
lcd.setCursor(1, 1);
lcd.print(menuItems[menuPage + 1]);
if (menuPage == 0) {
lcd.setCursor(15, 1);
lcd.write(byte(2));
} else if (menuPage > 0 and menuPage < maxMenuPages) {
lcd.setCursor(15, 1);
lcd.write(byte(2));
lcd.setCursor(15, 0);
lcd.write(byte(1));
} else if (menuPage == maxMenuPages) {
lcd.setCursor(15, 0);
lcd.write(byte(1));
}
}
void drawCursor() {
for (int x = 0; x < 2; x++) {
lcd.setCursor(0, x);
lcd.print(" ");
}
if (menuPage % 2 == 0) {
if (cursorPosition % 2 == 0) {
lcd.setCursor(0, 0);
lcd.write(byte(0));
}
if (cursorPosition % 2 != 0) {
lcd.setCursor(0, 1);
lcd.write(byte(0));
}
}
if (menuPage % 2 != 0) {
if (cursorPosition % 2 == 0) {
lcd.setCursor(0, 1);
lcd.write(byte(0));
}
if (cursorPosition % 2 != 0) {
lcd.setCursor(0, 0);
lcd.write(byte(0));
}
}
}
void operateMainMenu() {
int activeButton = 0;
while (activeButton == 0) {
int button;
readKey = analogRead(0);
if (readKey < 790) {
delay(100);
readKey = analogRead(0);
}
button = evaluateButton(readKey);
switch (button) {
case 0:
break;
case 1:
button = 0;
switch (cursorPosition) {
case 0:
menuItem1();
break;
case 1:
menuItem2();
break;}
activeButton = 1;
mainMenuDraw();
drawCursor();
break;
case 2:
button = 0;
if (menuPage == 0) {
cursorPosition = cursorPosition - 1;
cursorPosition = constrain(cursorPosition, 0, ((sizeof(menuItems) / sizeof(String)) - 1));
}
if (menuPage % 2 == 0 and cursorPosition % 2 == 0) {
menuPage = menuPage - 1;
menuPage = constrain(menuPage, 0, maxMenuPages);
}
if (menuPage % 2 != 0 and cursorPosition % 2 != 0) {
menuPage = menuPage - 1;
menuPage = constrain(menuPage, 0, maxMenuPages);
}
cursorPosition = cursorPosition - 1;
cursorPosition = constrain(cursorPosition, 0, ((sizeof(menuItems) / sizeof(String)) - 1));
mainMenuDraw();
drawCursor();
activeButton = 1;
break;
case 3:
button = 0;
if (menuPage % 2 == 0 and cursorPosition % 2 != 0) {
menuPage = menuPage + 1;
menuPage = constrain(menuPage, 0, maxMenuPages);
}
if (menuPage % 2 != 0 and cursorPosition % 2 == 0) {
menuPage = menuPage + 1;
menuPage = constrain(menuPage, 0, maxMenuPages);
}
cursorPosition = cursorPosition + 1;
cursorPosition = constrain(cursorPosition, 0, ((sizeof(menuItems) / sizeof(String)) - 1));
mainMenuDraw();
drawCursor();
activeButton = 1;
break;
}}}
int evaluateButton(int x) {
int result = 0;
if (x < 50) {
result = 1;
} else if (x < 195) {
result = 2;
else if (x < 380) {
result = 3;
} else if (x < 790) {
result = 4;}
return result;}
void drawInstructions() {
lcd.setCursor(0, 1); // Set cursor to the bottom line
lcd.print("Use ");
lcd.write(byte(1)); // Up arrow
lcd.print("/");
lcd.write(byte(2)); // Down arrow
lcd.print(" buttons");
}
void menuItem1() { // Function executes when you select the Yellow item from main menu
int activeButton = 0;
lcd.clear();
lcd.setCursor(3, 0);
lcd.print(sensors.getTempCByIndex(1));
while (activeButton == 0) {
int button;
readKey = analogRead(0);
if (readKey < 790) {
delay(100);
readKey = analogRead(0);
}
button = evaluateButton(readKey);
switch (button) {
case 4:
button = 0;
activeButton = 1;
break;
}}}
void menuItem2() {
int activeButton = 0;
lcd.clear();
lcd.setCursor(3, 0);
lcd.print(sensors.getTempCByIndex(2));
while (activeButton == 0) {
int button;
readKey = analogRead(0);
if (readKey < 790) {
delay(100);
readKey = analogRead(0);
}
button = evaluateButton(readKey);
switch (button) {
case 4:
button = 0;
activeButton = 1;
break;
}}}
i know it's very long but please help me, and i deleted what in the Loop becouse its very long and the problem not in it,
Thank you very much.
Alles_mit_LCD_und_Schaltern.ino (15.1 KB)