int BMM1;
int BMM2;
int BMM3;
int BMM4;
int BMM5;
int BMM6;
int BMM7;
int FR1 = 10;
int FR2 = 9;
int FR3 = 6;
int R1 = 12;
int R2 = 11;
int R3 = 8;
int S1 = 0;
int S2 = 0;
int S3 = 0;
int Start = 0;
int LG = 3;
int Soll = 1;
void setup() {
Serial.begin(9600);
pinMode(FR1, OUTPUT);
pinMode(FR2, OUTPUT);
pinMode(FR3, OUTPUT);
pinMode(R1, OUTPUT);
pinMode(R2, OUTPUT);
pinMode(R3, OUTPUT);
}
void loop() {
delay(5000);
BMM1 = analogRead(7);
BMM2 = analogRead(6);
BMM3 = analogRead(5);
BMM4 = analogRead(4);
BMM5 = analogRead(3);
BMM6 = analogRead(2);
BMM7 = analogRead(1);
if(BMM3 < Soll){ //Überprüfen, ob Gleis 1 belegt ist.
if(BMM5 > Soll){ //Wenn Gleis 1 belegt ist und auf Gleis 2 gafahren werden kann, tue dies...
digitalWrite(13, HIGH); //Blinksignal von Gleis 1 auf Gleis 2
delay(500);
digitalWrite(13, LOW);
delay(2000);
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13,LOW);
delay(500);
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
digitalWrite(R1, HIGH);
digitalWrite(R2, HIGH);
delay(100);
S2 = 150;
analogWrite(FR2, S2);
while(S1 < 150){ //Anfahren von Gleis 1.
analogWrite(FR1, 250);
delay(10);
S1++;
analogWrite(FR1, S1);
delay(40);
}
BMM4 = analogRead(4); //Ewig in der Schleife laufen und warten, bis der Belegtmelder von Gleis 2a anspricht.
while(BMM4 > Soll){
delay(10);
BMM4 = analogRead(4);
}
while(S2 > 100){ //PWM für Gleis 2 runterregeln und geichzeitig gucken, ob BMM5 für Gleis 2b angeschlagen hat.
BMM5 = analogRead(3);
if(BMM5 < Soll){
S2 = 101;
}
S2--;
analogWrite(FR2, S2);
delay(50);
}
S1 = 0;
analogWrite(FR1, S1);
while(BMM5 > Soll){ //Wenn Gleis 2b belegt ist, Spannung weg.
delay(10);
BMM5 = analogRead(3);
}
while(S2 > 0){
S2--;
analogWrite(FR2, S2);
delay(20);
}
}
else{ //...ansonsten fahre auf Gleis 3.
if(BMM7 > Soll){
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(2000);
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13,LOW);
delay(500);
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
digitalWrite(R1, HIGH);
digitalWrite(R3, HIGH);
delay(100);
S3 = 150;
analogWrite(FR3, S3);
while(S1 < 150){
analogWrite(FR1, 250);
delay(10);
S1++;
analogWrite(FR1, S1);
delay(40);
} //Der nachfolgende Programmteil ist ausgeklammert, da das 6. Belgtmeldemodul nicht richtig funktioniert.
/*BMM6 = analogRead(2);
Serial.println(BMM6);
while(BMM6 > Soll){
delay(10);
BMM6 = analogRead(2);
Serial.print(BMM6);
Serial.print("|");
}
Serial.println(BMM6);
Serial.println(BMM6);
while(S3 > 100){
BMM7 = analogRead(1);
if(BMM7 < Soll){
S3 = 101;
}
S3--;
analogWrite(FR3, S3);
delay(50);
Serial.print(BMM7);
Serial.print("|");
}
Serial.println(BMM7);
Serial.println(BMM7);
S1 = 0;
analogWrite(FR1, S1);*/
while(BMM7 > Soll){
delay(10);
BMM7 = analogRead(1);
}
S1 = 0;
analogWrite(FR1, S1);
while(S3 > 0){
S3--;
analogWrite(FR3, S3);
delay(5);
}
}
}
}
else{ //Wenn Gleis 1 nicht belegt ist, fahre wenn möglich von einem anderen SBF Gleis, als das zuletzt genutzte.
if(LG == 3){ //Wenn das zuletzt genutzte Gleis Gleis 3 war, Fahre von Gleis 2 zu Gleis 1...
if(BMM5 < Soll){
LG = 2;
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13,LOW);
delay(2000);
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
digitalWrite(R1, LOW);
digitalWrite(R2, LOW);
delay(100);
S1 = 150;
analogWrite(FR1, S1);
while(S2 < 150){
analogWrite(FR2, 250);
delay(10);
S2++;
analogWrite(FR2, S2);
delay(40);
}
BMM2 = analogRead(6);
while(BMM2 > Soll){
delay(10);
BMM2 = analogRead(6);
}
while(S1 > 100){
BMM3 = analogRead(5);
if(BMM3 < Soll){
S1 = 101;
}
S1--;
analogWrite(FR1, S1);
delay(60);
}
S2 = 0;
analogWrite(FR2, S2);
BMM3 = analogRead(5);
while(BMM3 > Soll){
delay(10);
BMM3 = analogRead(5);
}
while(S1 > 0){
S1--;
analogWrite(FR1, S1);
delay(20);
}
}
else{ //...wenn aber auf Gleis 2 nichts steht, fahre von Gleis 3 nach Glei 1.
if(BMM7 < Soll){
LG = 3;
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13,LOW);
delay(500);
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(2000);
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
digitalWrite(R1, LOW);
digitalWrite(R3, LOW);
delay(100);
S1 = 150;
analogWrite(FR1, S1);
while(S3 < 150){
analogWrite(FR3, 250);
delay(10);
S3++;
analogWrite(FR3, S3);
delay(40);
}
BMM2 = analogRead(6);
while(BMM2 > Soll){
delay(10);
BMM2 = analogRead(6);
}
while(S1 > 100){
BMM3 = analogRead(5);
if(BMM3 < Soll){
S1 = 101;
}
S1--;
analogWrite(FR1, S1);
delay(60);
}
S3 = 0;
analogWrite(FR3, S3);
BMM3 = analogRead(5);
while(BMM3 > Soll){
delay(10);
BMM3 = analogRead(5);
}
while(S1 > 0){
S1--;
analogWrite(FR1, S1);
delay(20);
}
}
}
}
else{ //Wenn das zuletzt genutzte Gleis Gleis 2 war, fahre von Gleis 3 nach Gleis 1...
if(BMM7 < Soll){
LG = 3;
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13,LOW);
delay(500);
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(2000);
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
digitalWrite(R1, LOW);
digitalWrite(R3, LOW);
delay(100);
S1 = 150;
analogWrite(FR1, S1);
while(S3 < 150){
analogWrite(FR3, 250);
delay(10);
S3++;
analogWrite(FR3, S3);
delay(40);
}
BMM2 = analogRead(6);
while(BMM2 > Soll){
delay(10);
BMM2 = analogRead(6);
}
while(S1 > 100){
BMM3 = analogRead(5);
if(BMM3 < Soll){
S1 = 101;
}
S1--;
analogWrite(FR1, S1);
delay(60);
}
S3 = 0;
analogWrite(FR3, S3);
BMM3 = analogRead(5);
while(BMM3 > Soll){
delay(10);
BMM3 = analogRead(5);
}
while(S1 > 0){
S1--;
analogWrite(FR1, S1);
delay(20);
}
}
else{
if(BMM5 < Soll){ //...wenn aber auf Gleis 3 nichts steht, fahre von Gleis 2 nach Gleis 1.
LG = 2;
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13,LOW);
delay(2000);
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
digitalWrite(R1, LOW);
digitalWrite(R2, LOW);
delay(100);
S1 = 150;
analogWrite(FR1, S1);
while(S2 < 150){
analogWrite(FR2, 250);
delay(10);
S2++;
analogWrite(FR2, S2);
delay(40);
}
BMM2 = analogRead(6);
while(BMM2 > Soll){
delay(10);
BMM2 = analogRead(6);
}
while(S1 > 100){
BMM3 = analogRead(5);
if(BMM3 < Soll){
S1 = 101;
}
S1--;
analogWrite(FR1, S1);
delay(60);
}
S2 = 0;
analogWrite(FR2, S2);
BMM3 = analogRead(5);
while(BMM3 > Soll){
delay(10);
BMM3 = analogRead(5);
}
while(S1 > 0){
S1--;
analogWrite(FR1, S1);
delay(20);
}
}
}
}
}
}