@noiaska, ahh verstanden. Ich bin absoluter Neuling in C++ und hab mit Klassen noch nichts gemacht, das muss ich mich erst mal einlesen. BZw ich denke das ist noch etwas zu früh für mich.
Prinzipiell hast du aber Recht, das deine Lösung die bessere ist.
Hier ist der Code mit dem delay Befehl, der macht genau, was er soll:
/* Weichenschaltung der Gleise 1 -5 Version 2_1
Funktionen:
1. Weichenschaltung neu komplett neu aufgesetzt
2. Programm läuft immer nur, wenn Zug in SBH einfährt (LSHaupt)
3. Programmierung LS Haupt
4. V6.0
*/
// Umbenenen der STM Pins
// Lichtschranke zur Gleisbesetzmeldung LSBG1-5
const int LSBG1 = PA8; // Lichtschranke Gleisbesetzt Gleis 1
const int LSBG2 = PB15; // Lichtschranke Gleisbesetzt Gleis 2
const int LSBG3 = PB14; // Lichtschranke Gleisbesetzt Gleis 3
const int LSBG4 = PB13; // Lichtschranke Gleisbesetzt Gleis 4
const int LSBG5 = PB12; // Lichtschranke Gleisbesetzt Gleis 5
int LSBG1State = 0;
int LSBG2State = 0;
int LSBG3State = 0;
int LSBG4State = 0;
int LSBG5State = 0;
// Lichtschranke SBH Haupt
const int LSHaupt = PA9; // Lichtschranke zur Einfahrt in den SBH
int LSHauptState = 0;
// Ausgänge für Relais der Weichenstellung weiche 1-5
const int W1A = PA0; // Weiche 1 Abbiegen
const int W1G = PA1; // Weiche 1 Geradeaus
const int W2A = PA2; // Weiche 2 Abbiegen
const int W2G = PA3; // Weiche 2 Geradeaus
const int W3A = PA4; // Weiche 3 Abbiegen
const int W3G = PA5; // Weiche 3 Geradeaus
const int W4A = PA6; // Weiche 4 Abbiegen
const int W4G = PA7; // Weiche 4 Geradeaus
const int WHauptA = PB0; // Hauptweiche Abbiegen
const int WHauptG = PB1; // Hauptweiche Geradeaus
int W1AState = 0;
int W1GState = 0;
int W2AState = 0;
int W2GState = 0;
int W3AState = 0;
int W3GState = 0;
int W4AState = 0;
int W4GState = 0;
int W5AState = 0;
int W5GState = 0;
int WHauptAState = 0;
int WHauptGState = 0;
// variable definieren
// Variablen Relais Ausgaänge für Weichen
bool geschaltetW1A = false; // Variable für Weiche1 Abbiegen
bool geschaltetW1G = false; // Variable für Weiche1 Geradeaus
bool geschaltetW2A = false; // Variable für Weiche2 Abbiegen
bool geschaltetW2G = false; // Variable für Weiche2 Geradeaus
bool geschaltetW3A = false; // Variable für Weiche3 Abbiegen
bool geschaltetW3G = false; // Variable für Weiche3 Geradeaus
bool geschaltetW4A = false; // Variable für Weiche4 Abbiegen
bool geschaltetW4G = false; // Variable für Weiche4 Geradeaus
bool geschaltetW5A = false; // Variable für Weiche5 Abbiegen
bool geschaltetW5G = false; // Variable für Weiche5 Geradeaus
bool geschaltetWHauptA = false; // Variable Hauptweiche Abbiegen
bool geschaltetWHauptG = false; // Variable für Weiche3 Geradeaus
int empfindlichkeit = 200;
unsigned long RELAIS1G_timestore; // Variable Speicher für Systemzeit.Weiche1
unsigned long RELAIS1A_timestore;
unsigned long RELAIS2G_timestore; // Variable Speicher für Systemzeit.Weiche2
unsigned long RELAIS2A_timestore;
unsigned long RELAIS3G_timestore; // Variable Speicher für Systemzeit.Weiche3
unsigned long RELAIS3A_timestore;
unsigned long RELAIS4G_timestore; // Variable Speicher für Systemzeit.Weiche5
unsigned long RELAIS4A_timestore;
unsigned long RELAIS5G_timestore; // Variable Speicher für Systemzeit.Weiche5
unsigned long RELAIS5A_timestore;
unsigned long RELAISHauptG_timestore; // Variable Speicher für Systemzeit.Weiche5
unsigned long RELAISHauptA_timestore;
// Variablen für die Counter damit Befehl nur einmal ausgeführt wird. Erst nach Zustandswechsel der LSBG wieder erneut
unsigned long CounterWHauptA = 0;
unsigned long CounterWHauptA1 = 0;
unsigned long CounterWHauptG = 0;
unsigned long CounterW1A = 0;
unsigned long CounterW1Ageschaltet = 0;
unsigned long CounterW2A = 0;
unsigned long CounterW3A = 0;
unsigned long CounterW4A = 0;
unsigned long CounterW5A = 0;
// delay zeit um zwischenräume zwischen Wagons abzufangen
unsigned long LSBG1_timestore_akt = 0;
unsigned long LSBG1_timestore_alt = 0;
unsigned long LSBG1_delay (3000);
unsigned long LSBG1High_timestore_akt = 0;
unsigned long LSBG1High_timestore_alt = 0;
unsigned long LSBG2_timestore_akt = 0;
unsigned long LSBG2_timestore_alt = 0;
unsigned long LSBG2_delay (3000);
unsigned long LSBG2High_timestore_akt = 0;
unsigned long LSBG2High_timestore_alt = 0;
unsigned long LSBG3_timestore_akt = 0;
unsigned long LSBG3_timestore_alt = 0;
unsigned long LSBG3_delay (3000);
unsigned long LSBG3High_timestore_akt = 0;
unsigned long LSBG3High_timestore_alt = 0;
unsigned long LSBG4_timestore_akt = 0;
unsigned long LSBG4_timestore_alt = 0;
unsigned long LSBG4_delay (3000);
unsigned long LSBG4High_timestore_akt = 0;
unsigned long LSBG4High_timestore_alt = 0;
unsigned long LSBG5_timestore_akt = 0;
unsigned long LSBG5_timestore_alt = 0;
unsigned long LSBG5_delay (3000);
unsigned long LSBG5High_timestore_akt = 0;
unsigned long LSBG5High_timestore_alt = 0;
void setup()
{
// put your setup code here, to run once:
// Pin Mode für Lichtschranken Gleisbesetztmeldung Gleis 1 - 5
pinMode(LSBG1, INPUT);
pinMode(LSBG2, INPUT);
pinMode(LSBG3, INPUT);
pinMode(LSBG4, INPUT);
pinMode(LSBG5, INPUT);
pinMode(LSHaupt, INPUT);
// Pin Mode für Lichtschranken Gleiseinfahrt Gleis 1 - 5 rausgenommen
// Pin Mode für Gleis an schalten
pinMode(W1A, OUTPUT);
pinMode(W1G, OUTPUT);
pinMode(W2A, OUTPUT);
pinMode(W2G, OUTPUT);
pinMode(W3A, OUTPUT);
pinMode(W3G, OUTPUT);
pinMode(W4A, OUTPUT);
pinMode(W4G, OUTPUT);
pinMode(WHauptA, OUTPUT);
pinMode(WHauptG, OUTPUT);
}
void loop()
{
LSHauptState = digitalRead(LSHaupt); // Abfrage ob Zug einfäht. Lichtschranke an der Zufahrt
if (LSHauptState == HIGH)
{
LSBG1State = digitalRead(LSBG1); // abfrage ob SBH voll
LSBG2State = digitalRead(LSBG2);
LSBG3State = digitalRead(LSBG3);
LSBG4State = digitalRead(LSBG4);
LSBG5State = digitalRead(LSBG5);
if (LSBG1State == HIGH && LSBG2State == HIGH && LSBG3State == HIGH && LSBG4State == HIGH && LSBG5State == HIGH) // Abfrage ob alle Gleise besetzt sind
{
digitalWrite (WHauptA, HIGH);
delay(1000);
digitalWrite (WHauptA, LOW);
}
else
// if (LSBG1State == LOW || LSBG2State == LOW || LSBG3State == LOW || LSBG4State == LOW || LSBG5State == LOW) // Abfrage ob alle Gleise besetzt sind // nicht alle Gleise sind besetzt
{
digitalWrite (WHauptG, HIGH);
delay(1000);
digitalWrite (WHauptG, LOW);
}
LSBG1State = digitalRead(LSBG1); // Abfrage ob Zug auf Gleis 1 steht.
if (LSBG1State == HIGH)
{
digitalWrite (W1G, HIGH);
delay (1000);
digitalWrite (W1G, LOW);
}
if (LSBG1State == LOW)
{
digitalWrite (W1A, HIGH);
delay (1000);
digitalWrite (W1A, LOW);
}
LSBG2State = digitalRead(LSBG2); // Abfrage ob Zug auf Gleis 2 steht.
if (LSBG2State == HIGH)
{
digitalWrite (W2G, HIGH);
delay (1000);
digitalWrite (W2G, LOW);
}
if (LSBG2State == LOW)
{
digitalWrite (W2A, HIGH);
delay (1000);
digitalWrite (W2A, LOW);
}
LSBG3State = digitalRead(LSBG3); // Abfrage ob Zug auf Gleis 3 steht.
if (LSBG3State == HIGH)
{
digitalWrite (W3G, HIGH);
delay (1000);
digitalWrite (W3G, LOW);
}
if (LSBG3State == LOW)
{
digitalWrite (W3A, HIGH);
delay (1000);
digitalWrite (W3A, LOW);
}
LSBG4State = digitalRead(LSBG4); // Abfrage ob Zug auf Gleis 4 steht.
if (LSBG4State == HIGH)
{
digitalWrite (W4G, HIGH);
delay (1000);
digitalWrite (W4G, LOW);
}
if (LSBG4State == LOW)
{
digitalWrite (W4A, HIGH);
delay (1000);
digitalWrite (W4A, LOW);
}
LSBG5State = digitalRead(LSBG5);
delay (10000);
}
}
Damit ich das richtig verstehe, ich kann den delay Befehl nicht so einfach als Funktion durch Millies ersetzten? Da muss ich andere Bedungen schaffen?