#define BUTTON_H 13
#define BUTTON_V 4
// 11 => Horizontal grüne LED
// 9 => Horizontal gelbe LED
// 7 => Horizontal rote LED
// 10 => Vertikale grüne LED
// 8 => Vertikale grüne LED
// 6 => Vertikale grüne LED
unsigned long startTime = 0; // Zeitparameter für den Tag auf 0 Setzen
const unsigned long dayDuration = 30000; // Zeitlänge des Tages in ms
int verticalCount = 0; // Zähler für die vertikale Straße vor dem Loop auf 0 setzen
int horizontalCount = 0; // Zähler für die horizontale Straße vor dem Loop auf 0 setzen
bool preferredDirection = true; // Horizontal ist in dem Beispiel eine Hauptstraße und soll erstmal bevorzugt werden.
void setup() {
const int LED_PINS[] = {11, 9, 7, 10, 8, 6}; // Array mit den Pin-Nummern der LEDs
const int LED_COUNT = 6; // Anzahl der LEDs
for (int i = 0; i < LED_COUNT; i++) {
pinMode(LED_PINS[i], OUTPUT); // Array wird verwendet um die PINs der LEDs auf OUTPUT zustellen
}
pinMode(BUTTON_H, INPUT);
pinMode(BUTTON_V, INPUT);
Serial.begin(115200);
digitalWrite(7, HIGH); // Vertical startet auf Rot
digitalWrite(6, HIGH); // Horizontal startet auf Rot
}
// Funktion um den Zustand der LEDs in einer Zeile zuverändern
void setLEDs(int state0, int state1, int state2, int state3, int state4, int state5) {
const int LED_PINS[] = {11, 9, 7, 10, 8, 6};
digitalWrite(LED_PINS[0], state0);
digitalWrite(LED_PINS[1], state1);
digitalWrite(LED_PINS[2], state2);
digitalWrite(LED_PINS[3], state3);
digitalWrite(LED_PINS[4], state4);
digitalWrite(LED_PINS[5], state5);
}
// Vertikale Ampel wird auf grün geschaltet
void VDrive() {
Serial.println("Vertikal fährt!");
// Gelb und Rot an
setLEDs(LOW, HIGH, HIGH, LOW, LOW, HIGH);
delay(2000);
// Grün an
setLEDs(HIGH, LOW, LOW, LOW, LOW, HIGH);
delay(2000);
}
// Vertikale Ampel wird auf Rot geschaltet
void VStop() {
// Gelb an
setLEDs(LOW, HIGH, LOW, LOW, LOW, HIGH);
delay(2000);
// Rot an
setLEDs(LOW, LOW, HIGH, LOW, LOW, HIGH);
delay(1000);
}
// Die vertikale Ampel soll Grün schalten, gegebenheiten der beiden Straßen prüfen!
void VCheck() {
while (digitalRead(10) == HIGH) { // 10 => horizontale grüne LED
HStop();
delay(2000);
}
while (digitalRead(11) == LOW) { // 11 => vertikale grüne LED
VDrive();
delay(1000);
}
}
// Horizontale Ampel soll von Rot auf Grün schalten
void HDrive() {
Serial.println("Horizontal fährt!");
// Gelb und Rot an
setLEDs(LOW, LOW, HIGH, LOW, HIGH, HIGH);
delay(2000);
// Grün an
setLEDs(LOW, LOW, HIGH, HIGH, LOW, LOW);
delay(2000);
}
// Horizontale Ampel soll von grün auf Rot schalten
void HStop() {
// Gelb an
setLEDs(LOW, LOW, HIGH, LOW, HIGH, LOW);
delay(2000);
// Rot an
setLEDs(LOW, LOW, HIGH, LOW, LOW, HIGH);
delay(1000);
}
// Die horizontale Ampel soll Grün schalten, gegebenheiten der beiden Straßen prüfen!
void HCheck() {
while (digitalRead(11) == HIGH) { // 11 => Vertikale grüne LED
VStop();
delay(2000);
}
while (digitalRead(10) == LOW) { // 10 => Horizontale grüne LED
HDrive();
delay(1000);
}
}
// Ampelprogramm
void loop() {
if (startTime == 0) {
startTime = millis(); // Timer für den Tag starten
}
byte V = digitalRead(BUTTON_V);
byte H = digitalRead(BUTTON_H);
Serial.print("Vertikaltaster = ");
Serial.println(V);
Serial.print("Horizontaltaster = ");
Serial.println(H);
delay(1000);
if (millis() - startTime < dayDuration) { // Überprüfung der Restlichen zeit des Tages
if (V == 1 && H == 0) { // Vertikale Straße soll auf Grün geschaltet werden
VCheck();
verticalCount++;
}
else if (H == 1 && V == 0) { // Horizontale Straße soll auf Grün geschaltet werden
HCheck();
horizontalCount++;
}
else if (H == 1 && V == 1) { // Beide Straßen werden befahren, schalte auf bevorzugte Straße
// Zeitabfrage einbauen!
if (preferredDirection == true) { // Horizontale Straße
for (int i = 0; i < 3; i++) { // Limitierung der Grünphase für die horizontale Straße
HCheck();
delay(1000);
}
horizontalCount++;
VCheck();
verticalCount++;
}
else { // Vertikale Straße
for (int i = 0; i < 3; i++) { // Limtierung der Grünphase für die vertikale Straße
VCheck();
delay(1000);
}
verticalCount++;
HCheck();
horizontalCount++;
}
}
else { // Niemand steht an der Ampel, bevorzugte Straße auf Grün schalten
if (preferredDirection == true) { // Horizontale Straße
HCheck();
}
else { // Vertikale Straße
VCheck();
}
}
}
else {
Serial.println("Tag beendet!");
if (horizontalCount > verticalCount) {
preferredDirection = true; // Horizontale Straße wird am nächsten Tag bevorzugt
}
else if (horizontalCount < verticalCount) {
preferredDirection = false; // Verticale Straße wird als nächstes Tag bevorzugt
}
else {
preferredDirection = true; // Horizontale Straße (Hauptstraße) bleibt die bevorzugte Straße
}
Serial.print("Vertikaler Anzahl =");
Serial.println(verticalCount);
Serial.print("Horizontale Anzahl =");
Serial.println(horizontalCount);
verticalCount = 0;
horizontalCount = 0;
startTime = 0;
}
}