/* CodeSchoss * * Benutzt den Drehimpulsgeber und die LCD * * LCD-Anschluesse: * LCD RS pin to digital pin 12 * LCD Enable pin to digital pin 11 * LCD D4 pin to digital pin 5 * LCD D5 pin to digital pin 4 * LCD D6 pin to digital pin 3 * LCD D7 pin to digital pin 0 (STATT 2 !!!) * LCD R/W pin to ground * * Encoder auf Pin 8 (Taster), 9 und 10 */ #include #include const int Taster = 8; // Pin für den Taster const int Led = 13; // Pin für LED Encoder myEnc(9, 10); LiquidCrystal lcd(12, 11, 5, 4, 3, 0); char Code[] = "DM8MB"; // geheimer Schlüssel char Eingabe[] = "AAAAA"; // (falsche) Vorgabe gleicher Länge int Length = sizeof(Code)-1; // Länge des Schlüssels int buttonState; // the current reading from the input pin int lastButtonState = LOW; // the previous reading from the input pin int Pos = 0; // Position auf dem LCD/in der Eingabe char c; // the following variables are long's because the time, measured in miliseconds, // will quickly become a bigger number than can be stored in an int. long lastDebounceTime = 0; // the last time the output pin was toggled long debounceDelay = 50; // the debounce time; increase if the output flickers void setup() { lcd.begin(16, 2); // set up the LCD's number of columns and rows pinMode (Taster, INPUT_PULLUP); pinMode (Led, OUTPUT); lcd.print (Code); // macht den Test einfacher lcd.setCursor(0, 1); // erstes Zeichen, zweite Zeile lcd.print (Eingabe); c = Eingabe[Pos]; lcd.setCursor(Pos, 1); lcd.cursor(); } long oldPosition = -999; // Initialwert Drehgeber void loop() { int Oeffner = (!strncmp (Eingabe, Code, Length)); digitalWrite(Led, Oeffner); // Status des Schlosses ausgeben int reading = digitalRead (Taster); if (reading != lastButtonState) { lastDebounceTime = millis(); // reset the debouncing timer } if ((millis() - lastDebounceTime) > debounceDelay) { // whatever the reading is at, it's been there for longer // than the debounce delay, so take it as the actual current state: buttonState = reading; } if ((reading == LOW) && (reading != lastButtonState)) { // Button gedrückt Eingabe[Pos] = c; // Eingabe verändern Pos = (Pos+1) % Length; // Position weiterschalten c = Eingabe[Pos]; // nächstes Zeichen lesen lcd.setCursor(Pos, 1); // Cursor neu positionieren } lastButtonState = reading; long newPosition = myEnc.read() >> 2; if (newPosition != oldPosition) { // Buchstaben verändern im Bereich 0x30-0x5f c = c + newPosition - oldPosition; if (c > 0x5f) { c = c - 0x30;} if (c < 0x30) { c = c + 0x30;} lcd.print (c); // geänderten Buchstaben anzeigen lcd.setCursor(Pos, 1); // aber Cursor-Position beibehalten oldPosition = newPosition; } }