//Programm zur Steuerung der Märklin-Drehscheibe 410, 7027 und 7186. //Markus Schild 2014 #include #include #include //Definition der Tastatur // const byte ROWS = 4; //four rows const byte COLS = 4; //three columns char keys[ROWS][COLS] = { {'1', '2', '3', 'A'}, {'4', '5', '6', 'B'}, {'7', '8', '9', 'C'}, {'*', '0', '#', 'D'} }; //Anschlüsse des Keyboards am Arduino. Wenn die Eingabe seltsame Ergebnisse ergibt, müssen die Keyboard Anschlüsse //um 180° gedreht werden oder die Pins angepasst werden //Connections of the keyboard. If you get strange results the pins of the keypad must be connected in reverse direction byte rowPins[ROWS] = {2, 3, 4, 5}; //connect to the row pinouts of the keypad byte colPins[COLS] = {6, 7, 8, 9}; //connect to the column pinouts of the keypad Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); int startimpuls = 14; int drehen = 15; int schritte = 0; int pos = 0; int ziel = 0; int auftrag = 0; int last = EEPROM.read(1); int reset1 = 0; int reset2 = 0; int reset3 = 0; int resetabbruch = 0; int gleis1input = 0; int gleis6input = 0; int gleisinput = 0; int lastposition = 0; BlinkLed led1(13, 50); // Wegetabelle: Die Zeilen beziehen sich auf die Startposition, in den Spalten stehen die Schritte // die den kürzesten Weg zum Ziel darstellen. Éntsprechen sich Start und Zielposition wird die Bühne um 180° gedreht. // // table of ways int wege[10][10] = { {8, 1, 2, 3, 4, 5, -2, 8, -3, -1}, { -1, 8, 1, 2, 3, 4, 5, -1, 4, -2}, { -2, -1, 8, 1, 2, 3, 4, -2, 3, 5}, { -3, -2, -1, 8, 1, 2, 3, -3, 2, 4}, { -4, -3, -2, -1, 8, 1, 2, -4, 1, 3}, { -5, -4, -3, -2, -1, 8, 1, -5, 8, 2}, {2, -5, -4, -3, -2, -1, 8, 2, -1, 1}, {8, 1, 2, 3, 4, 5, -2, 8, -3, -1}, { -5, -4, -3, -2, -1, 8, 1, -5, 8, 2}, {1, 2, -5, -4, -3, -2, -1, 1, -2, 8} }; void setup() { pinMode(startimpuls, OUTPUT); digitalWrite(startimpuls, HIGH); pinMode(drehen, OUTPUT); digitalWrite(drehen, HIGH); // pinMode(ledPin, OUTPUT); if (EEPROM.read(2) != 7){ last = 0; EEPROM.write(1, last); EEPROM.write(2, 7);} Serial.begin(9600); Serial.println("Letzte Position:"); Serial.println(last); led1.begin(); } void loop() { led1.update(); float ampere = 0; for (int i = 0; i < 20; i++) { ampere = ampere + (abs(analogRead(A2) - 512)); delay(1); } pos = last; char tastaturinput = keypad.getKey(); if (tastaturinput != NO_KEY) { int ziel = (tastaturinput - '0'); Serial.println("Ziel:"); Serial.println(ziel); if (ziel < 0) { reset3 = -1 + reset3; led1.turnOn(0); } if (reset3 < 0) { resetabbruch = reset2 + ziel; if (resetabbruch == (-12)) { reset3 = 0; Serial.println("Resetabbruch1"); led1.turnOff(); ziel = 0; goto abbruch; } if (ziel >= 0) { ziel = ziel * (-1); } reset2 = reset2 + ziel; Serial.println("Ziel nach umrechnung:"); Serial.println(ziel); } if (reset3 >= 0) { if (ziel >= 0) { if (ziel < 10) { last = ziel; Serial.println(last); EEPROM.write(1, last); schritte = wege[pos][ziel]; auftrag = schritte; if (schritte > 0) { digitalWrite(drehen, LOW); } if (schritte < 0) { digitalWrite(drehen, HIGH); schritte = schritte * (-1); } while (schritte > 0) { ampere = 0 ; for (int i = 0; i < 50; i++) { ampere = ampere + (abs(analogRead(A2) - 512)); delay(1); } if (ampere <= 250) { digitalWrite(startimpuls, LOW); delay(500); digitalWrite(startimpuls, HIGH); ampere = 101; schritte = schritte - 1; if (auftrag > 0) { pos = pos + 1; } else { pos = pos - 1; } if (pos < 0) { pos = pos + 10; } if (pos > 9) { pos = pos - 10; } } } } abbruch:; } } //ende ziel<10 if (ziel < 0) { Serial.println("reset2sub"); Serial.println(reset2); if (reset2 <= -19) { last = ((reset2 * (-1)) - 19); EEPROM.write(1, last); Serial.println("last neu festgelegt"); Serial.println(last); reset2 = 0; reset3 = 0; led1.turnOff(); } } } }