// Production 17 Function DCC Decoder
// Version 5.4 Geoff Bunza 2014,2015,2016
// NO LONGER REQUIRES modified software servo Lib
// Software restructuring mods added from Alex Shepherd and Franz-Peter
// With sincere thanks
// ******** UNLESS YOU WANT ALL CV'S RESET UPON EVERY POWER UP
// ******** AFTER THE INITIAL DECODER LOAD REMOVE THE "//" IN THE FOOLOWING LINE!!
//#define DECODER_LOADED
// ******** REMOVE THE "//" IN THE FOOLOWING LINE TO SEND DEBUGGING
// ******** INFO TO THE SERIAL MONITOR
//#define DEBUG
#include <NmraDcc.h>
#include <SoftwareServo.h>
SoftwareServo servo[4];
#define servo_start_delay 50
#define servo_init_delay 7
#define servo_slowdown 3 //servo loop counter limit
int servo_slow_counter = 0; //servo loop counter to slowdown servo transit
int tim_delay = 500;
/*int numfpins = 17;
byte fpins [] = {3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};
const int FunctionPin0 = 3;
const int FunctionPin1 = 4;
const int FunctionPin2 = 5;
const int FunctionPin3 = 6;
const int FunctionPin4 = 7;
const int FunctionPin5 = 8;
const int FunctionPin6 = 9;
const int FunctionPin7 = 10;
const int FunctionPin8 = 11;
const int FunctionPin9 = 12;
const int FunctionPin10 = 13;
const int FunctionPin11 = 14; //A0
const int FunctionPin12 = 15; //A1
const int FunctionPin13 = 16; //A2
const int FunctionPin14 = 17; //A3
const int FunctionPin15 = 18; //A4
const int FunctionPin16 = 19; //A5*/
int numfpins = 4;
byte fpins [] = {4,3,1,0};
const int FunctionPin0 = 4;
const int FunctionPin1 = 3;
const int FunctionPin2 = 1;
const int FunctionPin3 = 0;
NmraDcc Dcc ;
DCC_MSG Packet ;
int t; // temp
#define dccPIN 2
#define SET_CV_Address 24 // THIS ADDRESS IS FOR SETTING CV'S Like a Loco
#define Accessory_Address 40 // THIS ADDRESS IS THE START OF THE SWITCHES RANGE
// WHICH WILL EXTEND FOR 16 MORE SWITCH ADDRESSES
uint8_t CV_DECODER_MASTER_RESET = 120; // THIS IS THE CV ADDRESS OF THE FULL RESET
#define CV_To_Store_SET_CV_Address 121
#define CV_Accessory_Address CV_ACCESSORY_DECODER_ADDRESS_LSB
struct QUEUE
{
int inuse;
int current_position;
int increment;
int stop_value;
int start_value;
};
QUEUE *ftn_queue = new QUEUE[16];
struct CVPair
{
uint16_t CV;
uint8_t Value;
};
CVPair FactoryDefaultCVs [] =
{
{CV_ACCESSORY_DECODER_ADDRESS_LSB, Accessory_Address},
{CV_ACCESSORY_DECODER_ADDRESS_MSB, 0},
{CV_MULTIFUNCTION_EXTENDED_ADDRESS_MSB, 0},
{CV_MULTIFUNCTION_EXTENDED_ADDRESS_LSB, 0},
{CV_DECODER_MASTER_RESET, 0},
{CV_To_Store_SET_CV_Address, SET_CV_Address},
{CV_To_Store_SET_CV_Address+1, 0},
{30, 2}, //F0 Config 0=On/Off,1=Blink,2=Servo,3=DBL LED Blink,4=Pulsed,5=fade
{31, 1}, //F0 Rate Blink=Eate,PWM=Rate,Servo=Rate
{32, 28}, //F0 Start Position F0=0
{33, 140}, //F0 End Position F0=1
{34, 28}, //F0 Current Position
{35, 2}, //F1 Config 0=On/Off,1=Blink,2=Servo,3=DBL LED Blink,4=Pulsed,5=fade
{36, 1}, // Rate Blink=Eate,PWM=Rate,Servo=Rate
{37, 28}, // Start Position Fx=0
{38, 140}, // End Position Fx=1
{39, 28}, // Current Position
{40, 2}, //F2 Config 0=On/Off,1=Blink,2=Servo,3=DBL LED Blink,4=Pulsed,5=fade
{41, 1}, // Rate Blink=Eate,PWM=Rate,Servo=Rate
{42, 28}, // Start Position Fx=0
{43, 140}, // End Position Fx=1
{44, 28}, // Current Position
{45, 2}, //F3 Config 0=On/Off,1=Blink,2=Servo,3=DBL LED Blink,4=Pulsed,5=fade
{46, 1}, // Rate Blink=Eate,PWM=Rate,Servo=Rate
{47, 28}, // Start Position Fx=0
{48, 140}, // End Position Fx=1
{49, 28}, // Current Position
};
uint8_t FactoryDefaultCVIndex = sizeof(FactoryDefaultCVs)/sizeof(CVPair);
void notifyCVResetFactoryDefault()
{
// Make FactoryDefaultCVIndex non-zero and equal to num CV's to be reset
// to flag to the loop() function that a reset to Factory Defaults needs to be done
FactoryDefaultCVIndex = sizeof(FactoryDefaultCVs)/sizeof(CVPair);
};
void setup() //******************************************************
{
#ifdef DEBUG
Serial.begin(115200);
#endif
int i;
uint8_t cv_value;
// initialize the digital pins as outputs
for (int i=0; i < numfpins; i++) {
pinMode(fpins[i], OUTPUT);
digitalWrite(fpins[i], 0);
}
for (int i=0; i < numfpins; i++) {
digitalWrite(fpins[i], 1);
delay (tim_delay/10);
}
delay( tim_delay);
for (int i=0; i < numfpins; i++) {
digitalWrite(fpins[i], 0);
delay (tim_delay/10);
}
delay( tim_delay);
pinMode(2,INPUT);
// Setup which External Interrupt, the Pin it's associated with that we're using
Dcc.pin(0, 2, 0);
// Call the main DCC Init function to enable the DCC Receiver
Dcc.init( MAN_ID_DIY, 100, FLAGS_OUTPUT_ADDRESS_MODE | FLAGS_DCC_ACCESSORY_DECODER, CV_To_Store_SET_CV_Address);
delay(800);
#if defined(DECODER_LOADED)
if ( Dcc.getCV(CV_DECODER_MASTER_RESET)== CV_DECODER_MASTER_RESET )
#endif
{
for (int j=0; j < sizeof(FactoryDefaultCVs)/sizeof(CVPair); j++ )
Dcc.setCV( FactoryDefaultCVs[j].CV, FactoryDefaultCVs[j].Value);
digitalWrite(fpins[14], 1);
delay (1000);
digitalWrite(fpins[14], 0);
}
for ( i=0; i < numfpins; i++) {
cv_value = Dcc.getCV( 30+(i*5)) ;
ftn_queue[i].current_position =int (Dcc.getCV( 34+(i*5)));
ftn_queue[i].stop_value = int (Dcc.getCV( 33+(i*5)));
ftn_queue[i].start_value = int (Dcc.getCV( 32+(i*5)));
ftn_queue[i].increment = -int (char (Dcc.getCV( 31+(i*5))));
// attaches servo on pin to the servo object
servo[i].attach(fpins[i]);
servo[i].write(ftn_queue[i].start_value);
for (t=0; t<servo_start_delay; t++)
{
SoftwareServo::refresh();delay(servo_init_delay);
}
ftn_queue[i].inuse = 0;
servo[i].detach();
}
}
void loop() //**********************************************************************
{
//MUST call the NmraDcc.process() method frequently
// from the Arduino loop() function for correct library operation
Dcc.process();
SoftwareServo::refresh();
delay(4);
for (int i=0; i < numfpins; i++) {
if (ftn_queue[i].inuse==1) {
if (servo_slow_counter++ > servo_slowdown)
{
ftn_queue[i].current_position = ftn_queue[i].current_position + ftn_queue[i].increment;
if (ftn_queue[i].increment > 0) {
if (ftn_queue[i].current_position > ftn_queue[i].stop_value) {
ftn_queue[i].current_position = ftn_queue[i].stop_value;
ftn_queue[i].inuse = 0;
servo[i].detach();
}
}
if (ftn_queue[i].increment < 0) {
if (ftn_queue[i].current_position < ftn_queue[i].start_value) {
ftn_queue[i].current_position = ftn_queue[i].start_value;
ftn_queue[i].inuse = 0;
servo[i].detach();
}
}
servo[i].write(ftn_queue[i].current_position);
servo_slow_counter = 0;
}
}
}
}
extern void notifyDccAccState( uint16_t Addr, uint16_t BoardAddr, uint8_t OutputAddr, uint8_t State) {
uint16_t Current_Decoder_Addr;
uint8_t Bit_State;
Current_Decoder_Addr = Dcc.getAddr();
Bit_State = OutputAddr & 0x01;
if ( Addr >= Current_Decoder_Addr && Addr < Current_Decoder_Addr+17) { //Controls Accessory_Address+16
#ifdef DEBUG
Serial.print("Addr = ");
Serial.println(Addr);
Serial.print("BoardAddr = ");
Serial.println(BoardAddr);
Serial.print("Bit_State = ");
Serial.println(Bit_State);
#endif
exec_function(Addr-Current_Decoder_Addr, Bit_State );
}
}
void exec_function (int function, int FuncState) {
byte pin;
int servo_temp;
pin = fpins[function];
if (ftn_queue[function].inuse == 0) {
ftn_queue[function].inuse = 1;
servo[function].attach(pin);
}
if (FuncState==1) ftn_queue[function].increment = char ( Dcc.getCV( 31+(function*5)));
else ftn_queue[function].increment = - char(Dcc.getCV( 31+(function*5)));
if (FuncState==1) ftn_queue[function].stop_value = Dcc.getCV( 33+(function*5));
else ftn_queue[function].stop_value = Dcc.getCV( 32+(function*5));
}