#include <MobaTools.h>
MoToStepper Step1(2048, FULLSTEP);
//Anschlusspins
const byte stPn[] = { 7, 9, 8, 10 }; //Anschluss Pins für den Steppermotor
const byte Neonleuchten_Pins[] = {A0, A1, A2, A3, A4}; //Pins für die Neonleuchten
#define Gaslampe1 11 //Pin für die Gaslampe
#define Gaslampe2 3
#define Gaslampe3 5
#define Gaslampe4 6
#define DccAdresse 1 //DCC Addresse
#define Stepperspeed 80 //Geschwindigkeit des Steppermotors
#include <NmraDcc.h>
NmraDcc Dcc;
DCC_MSG Packet ;
const byte isRoco = 4; //Offset für Weichenadressen
unsigned long previousmillis = 0;
bool Neonleuchten_State = 0;
bool Stepperstate = 0;
int RandStepperTime = 0;
bool InputState = 1;
bool OldStepperState = 0;
void setup() {
Step1.attach( stPn[0], stPn[1], stPn[2], stPn[3] );
Step1.setSpeed(Stepperspeed);
for (int i = 0; i < 5; i++) {
pinMode(Neonleuchten_Pins[i], OUTPUT);
}
pinMode(Gaslampe1, OUTPUT);
pinMode(Gaslampe2, OUTPUT);
pinMode(Gaslampe3, OUTPUT);
pinMode(Gaslampe4, OUTPUT);
randomSeed(millis());
Dcc.pin(0, 2, 1); // Dcc-Signal an Pin2 ( = Int0 );
Dcc.init( MAN_ID_DIY, 15, FLAGS_OUTPUT_ADDRESS_MODE | FLAGS_DCC_ACCESSORY_DECODER, 0 );
}
void loop() {
Dcc.process();
unsigned long currentmillis = millis();
if (InputState == LOW) {
if (currentmillis - previousmillis > RandStepperTime) {
Stepperstate = !Stepperstate;
RandStepperTime = random(5000, 10000);
previousmillis = currentmillis;
}
if (Stepperstate == HIGH&&OldStepperState == LOW) {
Step1.rotate(1);
OldStepperState = HIGH;
}
else if (Stepperstate == LOW){
Step1.stop();
OldStepperState = LOW;
}
}
else {
Step1.stop();
}
if (InputState == LOW && Neonleuchten_State == LOW) {
for (int i = 0; i < 100; i++) {
if (random(0, 3) < 2) {
digitalWrite(Neonleuchten_Pins[random(0, 5)], HIGH);
}
else {
digitalWrite(Neonleuchten_Pins[random(0, 5)], LOW);
}
delay(random(5, 20));
}
for (int i = 0; i < 5; i++) {
digitalWrite(Neonleuchten_Pins[i], HIGH);
}
Neonleuchten_State = HIGH;
//Gaslampe
analogWrite(Gaslampe1, 50);
delay(50);
analogWrite(Gaslampe1, 0);
delay(10);
for (int i = 0; i < 150; i++) {
analogWrite(Gaslampe1, i);
delay(30);
}
digitalWrite(Gaslampe1, HIGH);
analogWrite(Gaslampe2, 50);
delay(50);
analogWrite(Gaslampe2, 0);
delay(10);
for (int i = 0; i < 150; i++) {
analogWrite(Gaslampe2, i);
delay(30);
}
digitalWrite(Gaslampe2, HIGH);
analogWrite(Gaslampe3, 50);
delay(50);
analogWrite(Gaslampe3, 0);
delay(10);
for (int i = 0; i < 150; i++) {
analogWrite(Gaslampe3, i);
delay(30);
}
digitalWrite(Gaslampe3, HIGH);
analogWrite(Gaslampe4, 50);
delay(50);
analogWrite(Gaslampe4, 0);
delay(10);
for (int i = 0; i < 150; i++) {
analogWrite(Gaslampe4, i);
delay(30);
}
digitalWrite(Gaslampe4, HIGH);
}
if (InputState == HIGH) {
Neonleuchten_State = LOW;
for (int i = 0; i < 5; i++) {
digitalWrite(Neonleuchten_Pins[i], LOW);
}
delay(500);
digitalWrite(Gaslampe1, LOW);
delay(100);
digitalWrite(Gaslampe2, LOW);
delay(150);
digitalWrite(Gaslampe3, LOW);
delay(300);
digitalWrite(Gaslampe4, LOW);
}
}
void notifyDccAccState( uint16_t Addr, uint16_t BoardAddr, uint8_t OutputAddr, uint8_t State ) {
word wAddr = Addr + isRoco; //Offset für Roco wird eingerechnet, da Roco die Weichenadressen anders zählt
if ( wAddr == DccAdresse) {
if ( OutputAddr & 0x1 ) {
InputState = LOW;
} else {
InputState = HIGH;
}
}
}