#include #include LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display unsigned const PIN_RIGHT = 49; unsigned const PIN_LEFT = 51; unsigned const PIN_OK = 53; int DRINKS = 3; char *drink_name[] = { "Wasser", "Limo", "Bier" }; int drink_price[] = { 100, 150, 250 }; void print_drinks() { int pos = 0; for (int i = 0; i < DRINKS; i++) { lcd.setCursor(pos, 0); lcd.print(drink_name[i]); pos += strlen(drink_name[i]) + 1; } } void print_prices() { int pos = 0; for (int i = 0; i < DRINKS; i++) { char buffer[100]; lcd.setCursor(pos, 1); sprintf(buffer, "%d.%02d", drink_price[i] / 100, drink_price[i] % 100); lcd.print(buffer); pos += strlen(drink_name[i]) + 1; } } // Schreibmarke auf Getränkename positionieren
 // drink = 0: 1. Getränk,
 // drink = 1: 2. Getränk, usw. void show_selection(int drink) { int x = 0; for (int i = 0; i < drink; i++) { x += strlen(drink_name[i]); x += strlen(" "); } lcd.setCursor(x, 0); lcd.blink(); } // Getränk auswählen und dessen Nummer zurueckgeben int choose_drink() { int current_selection = 0; unsigned long last_select = millis(); show_selection(current_selection); while (1) { if (millis() - last_select > 20) { // Rechts if (digitalRead(PIN_RIGHT) == LOW) { if (current_selection < DRINKS - 1) { current_selection++; } show_selection(current_selection); while (digitalRead(PIN_RIGHT) == LOW); last_select = millis(); } // Links if (digitalRead(PIN_LEFT) == LOW) { if (current_selection > 0) { current_selection--; } show_selection(current_selection); while (digitalRead(PIN_LEFT) == LOW); last_select = millis(); } // Drücken if (digitalRead(PIN_OK) == LOW) { lcd.noBlink(); while (digitalRead(PIN_OK) == LOW); return current_selection; } } } } void print_selection(int selection) { lcd.clear(); lcd.setCursor(0, 0); lcd.print("Sie kauften:"); lcd.setCursor(0, 1); lcd.print(drink_name[selection]); // 3 Sekunden blinken long m = millis(); while (millis() - m < 3000) { if (millis() / 200 % 3) lcd.backlight(); else lcd.noBacklight(); } lcd.backlight(); } void setup() { pinMode(PIN_RIGHT, INPUT_PULLUP); pinMode(PIN_LEFT, INPUT_PULLUP); pinMode(PIN_OK, INPUT_PULLUP); lcd.init(); lcd.backlight(); } void loop() { lcd.clear(); print_drinks(); print_prices(); int selection = choose_drink(); print_selection(selection); // Weitere Funktionen, etwa: // pay_for_drink(selection); // dispense_drink(selection); }