/*
Servodekoder für 16 Servos
-Steuerung über DCC
-seperate Einstellung dieser Parameter für jeden Servo
-DCC Adresse
-Pin
-Geschwindigkeit (für jede Position einzeln)
-Endlagen
-Interval => bei einem Interval von 2 Sekunden bewegt der Servo sich alle 2 Sekunden hin und her
*/
#include <MobaTools.h>
#include <NmraDcc.h>
#define Sek *1000
#define Sec Sek
#define sek Sek
#define sec Sek
#define Min *60000
//Einstellungen Servos 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
uint16_t Dcc_Adressen [] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; //DCC Adresse
uint8_t Servo_Pins [] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, A0, A1, A2, A3, A4}; //Anschluss Pin des Servos
int8_t Servo_Speed1 [] = {30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30}; //Geschwindigkeit für die Bewegung zu Position 1
int8_t Servo_Speed2 [] = {30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30}; //Geschwindigkeit für die Bewegung zu Position 2
uint16_t Servo_Pos1 [] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; //Position 1
uint16_t Servo_Pos2 [] = {180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180}; //Position 2
uint32_t Interval [] = {2 Sek, 2 Sek, 2 Sek, 2 Sek, 2 Sek, 2 Sek, 2 Sek, 2 Sek, 2 Sek, 2 Sek, 2 Sek, 2 Sek, 2 Sek, 2 Sek, 2 Sek, 2 Sek}; //Bewegungsinterval
const byte isRoco = 0; //bei einer Zentrale von Roco (z.B. Z21) auf 4 setzen
MoToServo Servos[16];
NmraDcc Dcc;
uint8_t AnzahlServos = sizeof(Dcc_Adressen) / sizeof(Dcc_Adressen[0]);
uint32_t prevMillis [] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
bool State[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
bool Servo_Pos [] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
//Verarbeitung der DCC Kommandos
void notifyDccAccTurnoutOutput( uint16_t Addr, uint8_t Direction, uint8_t OutputPower )
{
word wAddr = Addr + isRoco;
for (int i = 0; i < AnzahlServos; i++) {
if (Dcc_Adressen[i] == wAddr) {
State[i] = Direction;
}
}
}
void setup() {
Dcc.pin(0, 2, 1);
Dcc.init( MAN_ID_DIY, 10, CV29_ACCESSORY_DECODER | CV29_OUTPUT_ADDRESS_MODE, 0 );
for (int i = 0; i < AnzahlServos; i++) {
Servos[i].attach(Servo_Pins[i]);
Servos[i].write(Servo_Pos1[i]);
}
}
void loop() {
unsigned long currentmillis = millis();
Dcc.process();
for (int i = 0; i < AnzahlServos; i++) {
if (State[i] == HIGH) {
if (currentmillis - prevMillis [i] > Interval [i]) {
prevMillis[i] = currentmillis;
if (Servo_Pos[i] == 0) {
Servos[i].setSpeed(Servo_Speed2[i]);
Servos[i].write(Servo_Pos2[i]);
Servo_Pos[i] = 1;
}
else {
Servos[i].setSpeed(Servo_Speed1[i]);
Servos[i].write(Servo_Pos1[i]);
Servo_Pos[i] = 0;
}
}
}
}
}