#include <MobaTools.h>
MoToStepper Step1(4096, HALFSTEP);
MoToServo Servo1;
MoToServo Servo2;
//Anschlusspins
const byte stPn[] = { 7, 9, 8, 10 }; //Anschluss Pins für den Steppermotor
const byte Neonleuchten_Pins[] = {A0, A1, A2, A3, A4, A5}; //Pins für die Neonleuchten
const byte Lichtausgaenge[] = {5, 6, 11, 12, 13};
#define DccAdresse1 1 //DCC Addresse für den Steppermotor
#define DccAdresse2 2 //DCC Adresse für die Servos
#define DccAdresse3 3 //DCC Adresse für Neonröhren
#define DccAdresse4 4 //DCC Adresse für Lichtausgänge
#define Stepperspeed 80 //Geschwindigkeit des Steppermotors
#define Servo1Pin 3 //Pin für Servo 1
#define Servo1Speed 20 //Geschwindigkeit Servo 1
#define Servo1Pos1 0 //Position 1 für Servo 1
#define Servo1Pos2 180 //Position 2 für Servo 1
#define Servo2Pin 4
#define Servo2Speed 20
#define Servo2Pos1 0
#define Servo2Pos2 180
#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 InputState = 1;
bool InputState2 = 1;
byte flackern = 0;
unsigned long prevflackermillis = 0;
unsigned long RandFlackerTime = 0;
void setup() {
Step1.attach( stPn[0], stPn[1], stPn[2], stPn[3] );
Step1.setSpeed(Stepperspeed);
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 );
Servo1.attach(Servo1Pin, true);
Servo2.attach(Servo2Pin, true);
Servo1.setSpeed(Servo1Speed);
Servo2.setSpeed(Servo2Speed);
for (int i = 0; i < 6; i++) {
pinMode(Neonleuchten_Pins[i], OUTPUT);
}
for (int i = 0; i < 5; i++) {
pinMode(Lichtausgaenge[i], OUTPUT);
}
}
void loop() {
Dcc.process();
unsigned long currentmillis = millis();
if (InputState2 == LOW) {
for (int i = 0; i < 5; i++) {
digitalWrite(Lichtausgaenge[i], HIGH);
}
}
else {
for (int i = 0; i < 5; i++) {
digitalWrite(Lichtausgaenge[i], LOW);
}
}
if (InputState == LOW && Neonleuchten_State == LOW) {
if (currentmillis - prevflackermillis > RandFlackerTime) {
if (flackern < 100) {
flackern++;
RandFlackerTime = random(5, 10);
if (random(0, 3) < 2) {
digitalWrite(Neonleuchten_Pins[random(0, 6)], HIGH);
}
else {
digitalWrite(Neonleuchten_Pins[random(0, 6)], LOW);
}
}
else {
Neonleuchten_State = HIGH;
for (int i = 0; i < 6; i++) {
digitalWrite(Neonleuchten_Pins[i], HIGH);
}
flackern = 0;
}
prevflackermillis = currentmillis;
}
}
if (InputState == HIGH) {
Neonleuchten_State = LOW;
for (int i = 0; i < 6; i++) {
digitalWrite(Neonleuchten_Pins[i], 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 == DccAdresse1) {
if ( OutputAddr & 0x1 ) {
Step1.rotate(1);
} else {
Step1.stop();
}
}
if ( wAddr == DccAdresse2) {
if ( OutputAddr & 0x1 ) {
Servo1.write(Servo1Pos1);
Servo2.write(Servo2Pos1);
} else {
Servo1.write(Servo1Pos2);
Servo2.write(Servo2Pos2);
}
}
if ( wAddr == DccAdresse3) {
if ( OutputAddr & 0x1 ) {
InputState = LOW;
} else {
InputState = HIGH;
}
}
if ( wAddr == DccAdresse4) {
if ( OutputAddr & 0x1 ) {
InputState2 = LOW;
} else {
InputState2 = HIGH;
}
}
}