Sind an deinem Nano Pin 5,6,7 frei?? Wenn ja dann sollte das gehen. Habe es nicht ausprobiert. Sollte es nicht gehen, dann sage bescheid was nicht geht.
Code: Alles auswählen
/*
https://www.stummiforum.de/t163493f21-Arduino-Zeitsteuerungen-ohne-Delay-ein-kleines-Tutorial-1.html
Beitrag: 27
*/
// Ampelsteuerung an einer einspurigen Strasse mit Bushaltestelle
// Die Ampelphasen werden über eine Tabelle definiert
#include <MobaTools.h>
// Definition der Ausgangspins:
const byte hRotPin = A0; // Ampel an der Hauptstrasse, rechts
const byte hGelbPin = A1;
const byte hGruenPin = A2;
const byte nRotPin = A3; // Ampel an der Hauptstrasse, links
const byte nGelbPin = A4;
const byte nGruenPin = A5;
const byte BusRotPin = 2; // Ampel an der Bushaltestelle
const byte BusGelbPin = 3;
const byte BusGruenPin = 4;
const int baudSerial = 9600;
// Tabelle für die Ampelzustände
// Ampelphase: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12
// -------------------------------------------------------------------------------------------------------
// Tabellenindex: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11
// -------------------------------------------------------------------------------------------------------
const byte zeitDauer [] = { 30 , 3 , 3 , 3 , 20 , 3 , 3 , 3 , 10 , 3 , 3 , 3 };
// -------------------------------------------------------------------------------------------------------
const bool hauptRot [] = { OFF , OFF , ON , ON , ON , ON , ON , ON , ON , ON , ON , ON };
const bool hauptGelb [] = { OFF , ON , OFF , OFF , OFF , OFF , OFF , OFF , OFF , OFF , OFF , ON };
const bool hauptGruen [] = { ON , OFF , OFF , OFF , OFF , OFF , OFF , OFF , OFF , OFF , OFF , OFF };
const bool nebenRot [] = { ON , ON , ON , ON , OFF , OFF , ON , ON , ON , ON , ON , ON };
const bool nebenGelb [] = { OFF , OFF , OFF , ON , OFF , ON , OFF , OFF , OFF , OFF , OFF , OFF };
const bool nebenGruen [] = { OFF , OFF , OFF , OFF , ON , OFF , OFF , OFF , OFF , OFF , OFF , OFF };
const bool BusRot [] = { ON , ON , ON , ON , ON , ON , ON , ON , OFF , OFF , ON , ON };
const bool BusGelb [] = { OFF , OFF , OFF , OFF , OFF , OFF , OFF , ON , OFF , ON , OFF , OFF };
const bool BusGruen [] = { OFF , OFF , OFF , OFF , OFF , OFF , OFF , OFF , ON , OFF , OFF , OFF };
const byte tabMax = sizeof(zeitDauer) - 1; // Tabellenindex startet bei 0 ( deshalb die -1 für den höchsten Index )
byte ampelTabIx; // läuft von 0 .. 7
// Einrichten der notwendigen MoBaTools Objekte:
MoToTimer AmpelTimer;
MoToSoftLed hRot;
MoToSoftLed hGelb;
MoToSoftLed hGruen;
MoToSoftLed nRot;
MoToSoftLed nGelb;
MoToSoftLed nGruen;
MoToSoftLed nBusRot;
MoToSoftLed nBusGelb;
MoToSoftLed nBusGruen;
const byte relaisH = 5;
const byte relaisN = 6;
const byte relaisB = 7;
void setup() {
// Softled Objekte initiieren
hRot.attach( hRotPin );
hRot.riseTime( 200 );
hGelb.attach( hGelbPin );
hGelb.riseTime( 200 );
hGruen.attach( hGruenPin );
hGruen.riseTime( 200 );
// --------------------------------------------------------------------------
nRot.attach( nRotPin );
nRot.riseTime( 200 );
nGelb.attach( nGelbPin );
nGelb.riseTime( 200 );
nGruen.attach( nGruenPin );
nGruen.riseTime( 200 );
// --------------------------------------------------------------------------
nBusRot.attach( BusRotPin );
nBusRot.riseTime( 200 );
nBusGelb.attach( BusGelbPin );
nBusGelb.riseTime( 200 );
nBusGruen.attach( BusGruenPin );
nBusGruen.riseTime( 200 );
// --------------------------------------------------------------------------
ampelTabIx = 2; // Das System startet mit " alle Ampeln = ROT "
//-------Relais einrichten ----------
digitalWrite (relaisH, HIGH);
pinMode(relaisH, OUTPUT);
digitalWrite (relaisN, HIGH);
pinMode(relaisN, OUTPUT);
digitalWrite (relaisB, HIGH);
pinMode(relaisB, OUTPUT);
Serial.begin(baudSerial);
}
void loop() {
// Tabellengesteuerte Ampel
if ( !AmpelTimer.running() ) {
// Zeit abgelaufen
// Ampel schalten
Serial.println(F("xxxxxxxxxxx"));
hRot.write( hauptRot[ampelTabIx] );
Serial.println(ampelTabIx);
hGelb.write( hauptGelb[ampelTabIx] );
hGruen.write( hauptGruen[ampelTabIx] );
nRot.write( nebenRot[ampelTabIx] );
nGelb.write( nebenGelb[ampelTabIx] );
nGruen.write( nebenGruen[ampelTabIx] );
nBusRot.write( BusRot[ampelTabIx] );
nBusGelb.write( BusGelb[ampelTabIx] );
nBusGruen.write( BusGruen[ampelTabIx] );
// ----- Relais Grünschaltung steuern ----
if (ampelTabIx == 0) {
digitalWrite (relaisH, LOW);
}
else { digitalWrite (relaisH, HIGH);
}
if (ampelTabIx == 4) {
digitalWrite (relaisN, LOW);
}
else { digitalWrite (relaisN, HIGH);
}
if (ampelTabIx == 8) {
digitalWrite (relaisB, LOW);
}
else { digitalWrite (relaisB, HIGH);
}
// Zeit setzen ( da der Timer in ms rechnet, müssen die Tabellenwerte mit 1000 multipliziert werden )
AmpelTimer.setTime( zeitDauer[ampelTabIx] * 1000 );
// Merker weiterrückem
if ( ampelTabIx >= tabMax ) {
// Tabellenende erreicht
ampelTabIx = 0;
} else {
ampelTabIx++;
}
}
}