#include <LiquidCrystal.h>
//Definieren der Pins - Joystick
constexpr uint8_t PinX {A0};
constexpr uint8_t PinY {A1};
constexpr uint8_t PinSW {A2};
//Definieren der LCD-Pins - Display
constexpr uint8_t rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 6 ;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
//Werte von X, Y und SW - Joystick
int ValueX = 0; //Hoch und Runter
int ValueY = 0; //Links und Rechts
uint8_t ValueSW = LOW;
uint8_t lastSW = HIGH;
#define Size(x) sizeof(x)/sizeof(x[0])
constexpr uint8_t maxLen {20};
// Menue 0
const char menu0[][maxLen] PROGMEM = {"Autor", "Lied"};
const char* const m0[] PROGMEM = {menu0[0], menu0[1]};
constexpr int anzahlM0 = Size(m0);
// Menue 1
const char menu1[][maxLen] PROGMEM = {"Patricia Stegh", "18", "5BHME", "Fussball"};
const char* const m1[] PROGMEM = {menu1[0], menu1[1], menu1[2], menu1[3]};
constexpr int anzahlM1 = Size(m1);
// Menue 2
const char menu2[][maxLen] PROGMEM = {"Deep Purple", "Made in Japan", "1972", "Hardrock"};
const char* const m2[] PROGMEM = {menu2[0], menu2[1], menu2[2], menu2[3]};
constexpr int anzahlM2 = Size(m2);
// Menue 3
const char menu3[][maxLen] PROGMEM = {"Pop", "Blues", "Folksong", "Heavy Metal", "Grunge"};
const char* const m3[] PROGMEM = {menu3[0], menu3[1], menu3[2], menu3[3], menu3[4]};
constexpr int anzahlM3 = Size(m3);
constexpr int menueLen[] = {anzahlM0, anzahlM1, anzahlM2, anzahlM3};
constexpr unsigned long updateInterval {300};
int actMenu = 0;
int menuIndex = 0;
int lastIndex = -1;
char text[maxLen] = "-";
void setup()
{
Serial.begin(115200);
//LCD initialisieren - Display
lcd.begin(16, 2);
lcd.clear();
//pinMode - Joystick-Switch
pinMode(PinSW, INPUT_PULLUP);
// for (int i=0;i<maxItems;i++) Serial.println(getText(mt,maxItems,i));
}
void loop() {
joystick();
scrollen();
bestaetigen();
anzeigen();
}
void bestaetigen() {
static unsigned long lastOk = 0;
if (millis() - lastOk > updateInterval) {
lastOk = millis();
uint8_t mode = 0;
if (ValueY > 100) mode = 1;
if (ValueY >= 900) mode = 2;
switch (actMenu) {
case 0:
if (mode == 2) {
switch (menuIndex) {
case 0:
actMenu = 1;
break;
case 1:
actMenu = 2 ;
break;
}
}
case 2:
if (mode == 2) {
if (menuIndex == 3) {
actMenu = 3;
}
}
if (mode == 0) actMenu = 0;
break;
case 3:
if (mode == 0) {
actMenu = 2;
}
break;
default:
if (mode == 0) actMenu = 0;
}
}
}
// Zur Darstellung der verwendeten Umlaute auf dem
// LCD-Display sind diese wie folgt zu codieren:
// \365 ü
// \341 ä
// \357 ö
void anzeigen() {
static int lastMenu = -1;
if (actMenu != lastMenu) {
lastMenu = actMenu;
menuIndex = 0;
lastIndex = -1;
}
if (menuIndex != lastIndex ) {
lastIndex = menuIndex;
lcd.clear();
lcd.setCursor(0, 1);
switch (actMenu) {
case 0:
copyTextFromPROGMEM(m0, anzahlM0, menuIndex);
lcd.print(F(" Select>"));
break;
case 1:
copyTextFromPROGMEM(m1, anzahlM1, menuIndex);
lcd.print(F("<Zur\365ck"));
break;
case 2:
copyTextFromPROGMEM(m2, anzahlM2, menuIndex);
lcd.print(F("<Zur\365ck"));
if (menuIndex == 3) {
lcd.setCursor(9, 1);
lcd.print(F("Select>"));
}
break;
case 3:
copyTextFromPROGMEM(m3, anzahlM3, menuIndex);
lcd.print(F("<Zur\365ck"));
break;
}
lcd.setCursor(0, 0);
lcd.print(text);
}
}
void scrollen() {
static unsigned long lastScroll = 0;
if (millis() - lastScroll > updateInterval) {
lastScroll = millis();
switch (ValueX) {
case 0 ... 100:
menuIndex++;
menuIndex = menuIndex % menueLen[actMenu];;
break;
case 900 ... 1023:
menuIndex--;
menuIndex = (menuIndex < 0) ? menueLen[actMenu] - 1 : menuIndex;
break;
}
}
}
void copyTextFromPROGMEM(char* const m[], int len, int index) {
if (index < len) {
strcpy_P(text, (char*)pgm_read_word(&(m[index])));
}
}
void joystick() {
//Einlesen der Werte von X und Y
ValueX = analogRead(PinX);
ValueY = analogRead(PinY);
ValueSW = digitalRead(PinSW);
if (ValueSW != lastSW) {
lastSW = ValueSW;
if (!ValueSW) {
actMenu = 0;
menuIndex = 0;
}
delay(50);
}
}