// Bibliotheken
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
// Pin-Definitionen
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST 7
// Objekt für das ILI9341-Display
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
// Tasten-Pins
const int buttonUpPin = 2;
const int buttonDownPin = 3;
const int buttonConfirmPin = 4;
const int buttonMainmenuPin = 5;
// Funktion zum Prüfen, ob eine Taste gedrückt wurde
bool isButtonPressed(int pin) {
return digitalRead(pin) == LOW;
}
// Variablen für das Menü
int selectedMenu = 1;
int selectedItem = 1;
int numItemsInMenu = 2;
// Timeout-Variablen
unsigned long timeoutMillis = 0;
bool timeout = false;
int maxTimeoutMillis = 10000;
bool actionRunning = false;
bool analysesRunning = false;
//pH Grenzwerte
float pHMin = 0;
float pHMax = 0;
// Setup-Funktion
void setup() {
// Pin-Modi setzen
pinMode(buttonUpPin, INPUT_PULLUP);
pinMode(buttonDownPin, INPUT_PULLUP);
pinMode(buttonConfirmPin, INPUT_PULLUP);
pinMode(buttonMainmenuPin, INPUT_PULLUP);
// TFT-Initialisierung
tft.begin();
tft.fillScreen(ILI9341_BLACK);
//tft.setRotation(3);
displayMenu();
// Timeout und Aktion zurücksetzen
analysesRunning = false; //bestimmt ob messen aktiv
timeoutMillis = millis(); //
actionRunning = false; //bestimmt ob ob eine funktion ausgeführt wird
}
// Haupt-Loop
void loop() {
handleMenuNavigation();
Timeout();
}
// Menünavigation
void handleMenuNavigation() {
// Menünavigation mit den Tasten
if (isButtonPressed(buttonUpPin) && !actionRunning) {
selectedItem = (selectedItem == 1) ? numItemsInMenu : selectedItem - 1;
displayMenu();
timeoutMillis = millis(); // Zurücksetzen des millis-Zählers
delay(200);
}
if (isButtonPressed(buttonDownPin) && !actionRunning) {
selectedItem = (selectedItem == numItemsInMenu) ? 1 : selectedItem + 1;
displayMenu();
timeoutMillis = millis(); // Zurücksetzen des millis-Zählers
delay(200);
}
// Zurück zum Hauptmenü
if (isButtonPressed(buttonMainmenuPin)) {
backToMainMenu();
timeoutMillis = millis(); // Zurücksetzen des millis-Zählers
delay(200);
}
// Bestätigung des ausgewählten Menüpunkts
if (isButtonPressed(buttonConfirmPin) && !actionRunning) {
clear_lcd();
timeoutMillis = millis(); // Zurücksetzen des millis-Zählers
actionMenu(selectedMenu, selectedItem);
}
}
// Timeout überprüfen
void Timeout() {
// Prüfen, ob timeout aktiv und 10 Sekunden vergangen sind
if (!analysesRunning && millis() - timeoutMillis >= maxTimeoutMillis) {
actionMenu(2, 1); // actionMenu21 ausführen
}
}
// Menüaktionen
void handleMenuAction(int nextMenu, int itemsInMenu) {
// Menüwechsel und Anzeige
selectedMenu = nextMenu;
selectedItem = 1;
numItemsInMenu = itemsInMenu;
displayMenu();
}
void actionMenu(int menu, int item) {
// Aktionen für jedes Menü und Menüpunkt
switch(menu) {
case 1: // Hauptmenü
switch(item) {
case 1: //Menü Messen
handleMenuAction(2, 2); // Zum Menü 2 mit 2 Menü Items wechseln
break;
case 2: //Menü Kalibrieren
handleMenuAction(3, 5); // Zum Menü 3 mit 5 Menü Items wechseln
break;
}
break;
case 2:
switch(item) {
case 1: //Funktion Messen
clear_lcd();
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.setTextSize(2);
tft.setCursor(10, 50);
tft.print("pH/ORP wird gemessen");
actionRunning = true;
analysesRunning = true;
break;
case 2: //zurück
actionRunning = false;
analysesRunning = false;
backToMainMenu();
break;
}
break;
case 3:
switch(item) {
case 1: //pH Kalibrieren
clear_lcd();
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.setTextSize(2);
tft.setCursor(10, 50);
tft.print("CAL pH");
actionRunning = true;
analysesRunning = false;
break;
case 2: //pH Grenzwerte definieren
clear_lcd();
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.setTextSize(2);
tft.setCursor(10, 50);
tft.print("pH Grenzwerte");
setpHLimits();
actionRunning = true;
analysesRunning = false;
break;
case 3: //ORP Kalibrieren
clear_lcd();
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.setTextSize(2);
tft.setCursor(10, 50);
tft.print("CAL ORP");
actionRunning = true;
analysesRunning = false;
break;
case 4: //ORP Grenzwerte definieren
clear_lcd();
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.setTextSize(2);
tft.setCursor(10, 50);
tft.print("ORP Grenzwerte");
setORPLimis();
actionRunning = true;
analysesRunning = false;
break;
case 5: //zurück
clear_lcd();
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.setTextSize(2);
tft.setCursor(10, 50);
tft.print("zurück ...");
delay(500);
actionRunning = false;
analysesRunning = false;
backToMainMenu();
break;
}
break;
}
}
void backToMainMenu() {
// Zurück zum Hauptmenü
clear_lcd();
selectedMenu = 1;
selectedItem = 1;
numItemsInMenu = 2;
displayMenu();
analysesRunning = false;
actionRunning = false;
}
void setpHLimis() {
}
void setORPLimis() {
}
// Menüanzeige
void printMenuItem(const char *item, int x, int y, int index) {
// Anzeige eines Menüpunkts
if (selectedItem == index) {
tft.setTextColor(ILI9341_RED, ILI9341_BLACK);
} else {
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
}
tft.setTextSize(2);
tft.setCursor(x, y);
tft.print(item);
}
void displayMenu() {
// Anzeige des aktuellen Menüs
if (selectedMenu == 1) {
printMenuItem("Messen", 10, 50, 1);
printMenuItem("Einstellungen", 10, 100, 2);
} else if (selectedMenu == 2) {
printMenuItem("pH/ORP Messung",10, 50, 1);
printMenuItem("zurück", 10, 100, 2);
} else if (selectedMenu == 3) {
printMenuItem("CAL pH", 10, 50, 1);
printMenuItem("pH Grenzwerte", 10, 100, 2);
printMenuItem("CAL ORP", 10, 150, 3);
printMenuItem("pH Grenzwerte", 10, 200, 4);
printMenuItem("zurück", 10, 250, 5);
}
}
// Display Löschen
void clear_line(uint16_t x, uint16_t y, uint8_t length) {
tft.setCursor(x, y);
for (uint8_t i = 0; i < length; i++) {
tft.print(' ');
}
}
void clear_lcd() {
clear_line(10, 50, 25); // Löschen der ersten Zeile
clear_line(10, 100, 15); // Löschen der zweiten Zeile
if (numItemsInMenu >= 3) {
clear_line(10, 150, 15);
clear_line(10, 200, 15);
clear_line(10, 250, 15);
}
}