// Bibliotheken
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Variablen
const int buttonZiel = 6;
const int buttonZielB = 9;
const int buttonZugTyp = 7;
const int buttonZugTypB = 8;
const int buttonLiniennm1 = 5;
const int buttonLiniennm1B = 10;
const int buttonLiniennm2 = 4;
const int buttonLiniennm2B = 11;
int ZugTyp = 0; // Variable aktueller Zugtyp
int Ziel = 0; // Variable aktuelles Ziel
int Liniennm1 = 0; //Variable aktuelle Liniennummer erste Position
int Liniennm2 = 0; //Variable aktuelle Liniennummer zweite Position
int Sonderziel = 0; // Variable um Buttons bei Sonderziel auszuschalten
// OLED Größe
#define SCREEN_WIDTH 128 // Breite in Pixel
#define SCREEN_HEIGHT 32 // Höhe in Pixel
// OLED Bibliothek Einstellung
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Setup
void setup() {
// Pin Modus Setzen
pinMode(buttonZiel, INPUT_PULLUP);
pinMode(buttonZugTyp, INPUT_PULLUP);
pinMode(buttonLiniennm1, INPUT_PULLUP);
pinMode(buttonLiniennm2, INPUT_PULLUP);
pinMode(buttonZielB, INPUT_PULLUP);
pinMode(buttonZugTypB, INPUT_PULLUP);
pinMode(buttonLiniennm1B, INPUT_PULLUP);
pinMode(buttonLiniennm2B, INPUT_PULLUP);
// OLED Startsequenz
Serial.begin(115200);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(2);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(22, 10);
display.println("CALIWAY");
display.invertDisplay(true);
display.display();
delay(1000);
display.invertDisplay(false);
display.display();
delay(1000);
display.clearDisplay();
display.display();
}
// Loop
void loop() {
//delay(100);
if (Sonderziel == 0){ //Button Zug Typ
if (digitalRead(buttonZugTyp) == LOW) { // Knopf gedrückt? Low = AN weil an GND
display.clearDisplay(); // Display leeren
ZugTyp++; // Variable +1
Serial.println("Typ+");
delay(250);
}}
if (Sonderziel == 0){ //Button Zug Typ Zurück
if (digitalRead(buttonZugTypB) == LOW) { // Knopf gedrückt? Low = AN weil an GND
display.clearDisplay(); // Display leeren
ZugTyp--; // Variable +1
Serial.println("Typ-");
delay(250);
}}
if (Sonderziel == 0){ // Button Liniennummer Position 1
if (digitalRead(buttonLiniennm1) == LOW) { // Knopf gedrückt? Low = AN weil an GND
display.clearDisplay(); // Display leeren
Liniennm1++; // Variable +1
Serial.println("Nummer 1+");
delay(250);
}}
if (Sonderziel == 0){ // Button Liniennummer Position 1
if (digitalRead(buttonLiniennm1B) == LOW) { // Knopf gedrückt? Low = AN weil an GND
display.clearDisplay(); // Display leeren
Liniennm1--; // Variable +1
Serial.println("Nummer 1-");
delay(250);
}}
if (Sonderziel == 0){ // Button Liniennummer Position 1
if (digitalRead(buttonLiniennm2) == LOW) { // Knopf gedrückt? Low = AN weil an GND
display.clearDisplay(); // Display leeren
Liniennm2++; // Variable +1
Serial.println("Nummer 2+");
delay(250);
}}
if (Sonderziel == 0){ // Button Liniennummer Position 1
if (digitalRead(buttonLiniennm2B) == LOW) { // Knopf gedrückt? Low = AN weil an GND
display.clearDisplay(); // Display leeren
Liniennm2--; // Variable +1
Serial.println("Nummer 2-");
delay(250);
}}
if (digitalRead(buttonZiel) == LOW) { // Knopf gedrückt? Low = AN weil an GND // Button Ziel
display.clearDisplay(); // Display leeren
Ziel++; // Variable +1
Serial.println("Ziel+");
delay(250);
}
if (digitalRead(buttonZielB) == LOW) { // Knopf gedrückt? Low = AN weil an GND // Button Ziel
display.clearDisplay(); // Display leeren
Ziel--; // Variable +1
Serial.println("Ziel-");
delay(250);
}
// Zug Typ Variable /////////////////////
if(ZugTyp== -1){ // Wenn Variable Z = -1 [ZURÜCKSETZER > Minimalwert]
ZugTyp=3;} // dann Variable auf 3 setzen
if(ZugTyp==0){ // Wenn Variable Z = 1
ZugTyp0();} // dann gehe zu void Ziel0
if(ZugTyp==1){ // Wenn Variable Z = 1
ZugTyp1();} // dann gehe zu void Ziel1
if(ZugTyp==2){ // Wenn Variable Z = 2
ZugTyp2();} // dann gehe zu void Ziel
if(ZugTyp==3){ // Wenn Variable Z = 3
ZugTyp3();} // dann gehe zu void Ziel
if(ZugTyp==4){ // Wenn Variable Z = 4 [ZURÜCKSETZER > Maximalwert]
ZugTyp=0;} // dann Variable auf 0 setzen
// Zug Ziel Variable /////////////////////
if(Ziel== -1){ // Wenn Variable Z = -1 [ZURÜCKSETZER > Minimalwert]
Ziel=2;} // dann Variable auf 3 setzen
if(Ziel==0){ // Wenn Variable Z = 1
Ziel0();} // dann gehe zu void Ziel0
if(Ziel==1){ // Wenn Variable Z = 1
Ziel1();} // dann gehe zu void Ziel0
if(Ziel==2){ // Wenn Variable Z = 1
Ziel2();} // dann gehe zu void Ziel0
if(Ziel==3){ // Wenn Variable Z = 4 [ZURÜCKSETZER > Maximalwert]
Ziel=0;} // dann Variable auf 1 setzen
// Zug Nummer Variable Position 1
if(Liniennm1== -1){ // Wenn Variable Z = -1 [ZURÜCKSETZER > Minimalwert]
Liniennm1=2;} // dann Variable auf 3 setzen
if(Liniennm1==0){ // Wenn Variable Z = 1
Liniennm10();} // dann gehe zu void Ziel0
if(Liniennm1==1){ // Wenn Variable Z = 1
Liniennm11();} // dann gehe zu void Ziel0
if(Liniennm1==2){ // Wenn Variable Z = 1
Liniennm12();} // dann gehe zu void Ziel0
if(Liniennm1==3){ // Wenn Variable Z = 4 [ZURÜCKSETZER > Maximalwert]
Liniennm1=0;} // dann Variable auf 1 setzen
// Zug Nummer Variable Position 2
if(Liniennm2== -1){ // Wenn Variable Z = -1 [ZURÜCKSETZER > Minimalwert]
Liniennm2=1;} // dann Variable auf 3 setzen
if(Liniennm2==0){ // Wenn Variable Z = 1
Liniennm20();} // dann gehe zu void Ziel0
if(Liniennm2==1){ // Wenn Variable Z = 1
Liniennm21();} // dann gehe zu void Ziel0
if(Liniennm2==2){ // Wenn Variable Z = 4 [ZURÜCKSETZER > Maximalwert]
Liniennm2=0;} // dann Variable auf 1 setzen
} // Ende void loop
// Zugtypen /////////////////////
void ZugTyp0(){
//Serial.println("Ziel0");
display.setCursor(0, 10);
display.setTextSize(2);
display.println("");
display.display();
}
void ZugTyp1(){
//Serial.println("Ziel1");
display.setCursor(0, 10);
display.setTextSize(2);
display.println("IRE");
display.display();
}
void ZugTyp2(){
//Serial.println("Ziel2");
display.setCursor(0, 10);
display.setTextSize(2);
display.println("MEX");
display.display();
}
void ZugTyp3(){
//Serial.println("Ziel3");
display.setCursor(0, 10);
display.setTextSize(2);
display.println("RE");
display.display();
}
// Zug Ziele /////////////////////
void Ziel0(){
//Serial.println("Ziel3");
display.setCursor(40, 10);
display.setTextSize(2);
display.println("");
display.display();
}
void Ziel1(){
//Serial.println("Ziel3");
ZugTyp = 0;
Liniennm1 = 0;
Liniennm2 = 0;
Sonderziel = 1; // Sonderziel?
display.clearDisplay();
display.setCursor(10, 10);
display.setTextSize(2);
display.println("Sonderzug");
display.display();
}
void Ziel2(){
//Serial.println("Ziel3");
display.setCursor(40, 10);
display.setTextSize(2);
display.println("Wismar");
display.display();
Sonderziel = 0; // Sonderziel?
}
// Liniennummer Position 1 /////////////////////
void Liniennm10(){
//Serial.println("Ziel3");
display.setCursor(30, 10);
display.setTextSize(2);
display.println("");
display.display();
}
void Liniennm11(){
//Serial.println("Ziel3");
display.setCursor(30, 10);
display.setTextSize(2);
display.println("1");
display.display();
}
void Liniennm12(){
//Serial.println("Ziel3");
display.setCursor(30, 10);
display.setTextSize(2);
display.println("2");
display.display();
}
// Liniennummer Position 2 /////////////////////
void Liniennm20(){
//Serial.println("Ziel3");
display.setCursor(30, 10);
display.setTextSize(2);
display.println("");
display.display();
}
void Liniennm21(){
//Serial.println("Ziel3");
display.setCursor(35, 10);
display.setTextSize(2);
display.println("1");
display.display();
}