Projekt P01 - Grundlagen des LCD Keypad Shields
Arduino Uno, LCD 1602 (parallel), 5 Taster mit Widerstandsnetzwerk
Jede Taste erzeugt durch Widerstände einen anderen Spannungswert an A0!
Exakte Werte variieren je nach Shield. Bereiche sind robuster.
#include <LiquidCrystal.h>
// LCD Pin-Definitionen (Shield-Standard)
const int PIN_RS = 8;
const int PIN_EN = 9;
const int PIN_D4 = 4;
const int PIN_D5 = 5;
const int PIN_D6 = 6;
const int PIN_D7 = 7;
const int PIN_BUTTONS = A0;
// Schwellwerte für Tastenerkennung
const int THRESHOLD_RIGHT = 60;
const int THRESHOLD_UP = 200;
const int THRESHOLD_DOWN = 400;
const int THRESHOLD_LEFT = 600;
const int THRESHOLD_SELECT = 800;
// Tasten-Enum für sauberen Code
enum Button { BTN_NONE, BTN_RIGHT, BTN_UP,
BTN_DOWN, BTN_LEFT, BTN_SELECT };LiquidCrystal lcd(PIN_RS, PIN_EN, PIN_D4,
PIN_D5, PIN_D6, PIN_D7);
// Taste erkennen mit Wertebereichen
Button readButton() {
int val = analogRead(PIN_BUTTONS);
if (val < THRESHOLD_RIGHT) return BTN_RIGHT;
if (val < THRESHOLD_UP) return BTN_UP;
if (val < THRESHOLD_DOWN) return BTN_DOWN;
if (val < THRESHOLD_LEFT) return BTN_LEFT;
if (val < THRESHOLD_SELECT) return BTN_SELECT;
return BTN_NONE;
}
// Tastenname als String
const char* getButtonName(Button btn) {
switch(btn) {
case BTN_RIGHT: return "Rechts";
case BTN_UP: return "Hoch";
case BTN_DOWN: return "Runter";
case BTN_LEFT: return "Links";
case BTN_SELECT: return "Select";
default: return "Keine";
}
}void setup() {
lcd.begin(16, 2);
lcd.print("LCD Keypad Demo");
lcd.setCursor(0, 1);
lcd.print("Taste druecken!");
}
void loop() {
static Button lastButton = BTN_NONE;
Button currentButton = readButton();
// Nur bei Änderung aktualisieren
if (currentButton != lastButton) {
lastButton = currentButton;
if (currentButton != BTN_NONE) {
lcd.clear();
lcd.print("Taste:");
lcd.setCursor(0, 1);
lcd.print(getButtonName(currentButton));
lcd.print(" (");
lcd.print(analogRead(PIN_BUTTONS));
lcd.print(")");
}
}
delay(50); // Entprellen
}Funktioniert auch bei leicht abweichenden Shields oder Temperaturschwankungen!
Das LCD Keypad Shield wird aus Einzelkomponenten simuliert:
Klicke auf verschiedene Buttons und beobachte die Anzeige!
Du kennst jetzt das LCD Keypad Shield!
P02: Multiclock - Uhr, Stoppuhr, Alarm und Timer