I am wondering if anyone is interested or kind enough to help me compress and simplify the code for a DCC Accessory Decoder to control multiple building lights.
I have attached the code below. There are 8 LEDs connected to pins 3-10 on an Arduino.
Ideally the code would have a simple way at the beginning to allow the user to assign a DCC accessory address to each of the LEDs.
I am sure there will be a more compact way of assigning each LED to an output pin as well as configuring each pin as an output, for example using a for() loop.
However, I am not fluent enough in c++ and am not (yet) familiar enough with the different variables to find an obvious way to do this.
Thank you in advance for your help!
#include "NmraDcc.h"
NmraDcc Dcc ;
const int DCC_Pin = 2; //define DCC input pin - a DCC interface is required andshould be attached to pin 2
const int LED1Pin = 3; //define DCC arduino pin that controls LED1
const int LED2Pin = 4; //define DCC arduino pin that controls LED2
const int LED3Pin = 5; //define DCC arduino pin that controls LED3
const int LED4Pin = 6; //define DCC arduino pin that controls LED4
const int LED5Pin = 7; //define DCC arduino pin that controls LED5
const int LED6Pin = 8; //define DCC arduino pin that controls LED6
const int LED7Pin = 9; //define DCC arduino pin that controls LED7
const int LED8Pin = 10; //define DCC arduino pin that controls LED8
void notifyDccAccTurnoutOutput( uint16_t Addr, uint8_t Direction, uint8_t OutputPower )
{
switch(Addr){
case 301: //DCC Accessory Decoder Address for LED1
if(Direction == 1){
digitalWrite (LED1Pin,HIGH);
}
else{
digitalWrite (LED1Pin,LOW);
}
break;
case 302: //DCC Accessory Decoder Address for LED2
if(Direction == 1){
digitalWrite (LED2Pin,HIGH);
}
else{
digitalWrite (LED2Pin,LOW);
}
break;
case 303: //DCC Accessory Decoder Address for LED3
if(Direction == 1){
digitalWrite (LED3Pin,HIGH);
}
else{
digitalWrite (LED3Pin,LOW);
}
break;
case 304: //DCC Accessory Decoder Address for LED4
if(Direction == 1){
digitalWrite (LED4Pin,HIGH);
}
else{
digitalWrite (LED4Pin,LOW);
}
break;
case 305: //DCC Accessory Decoder Address for LED5
if(Direction == 1){
digitalWrite (LED5Pin,HIGH);
}
else{
digitalWrite (LED5Pin,LOW);
}
break;
case 306: //DCC Accessory Decoder Address for LED6
if(Direction == 1){
digitalWrite (LED6Pin,HIGH);
}
else{
digitalWrite (LED6Pin,LOW);
}
break;
case 307: //DCC Accessory Decoder Address for LED7
if(Direction == 1){
digitalWrite (LED7Pin,HIGH);
}
else{
digitalWrite (LED7Pin,LOW);
}
break;
case 308: //DCC Accessory Decoder Address for LED8
if(Direction == 1){
digitalWrite (LED8Pin,HIGH);
}
else{
digitalWrite (LED8Pin,LOW);
}
break;
default:
break;
}
}
void setup()
{
Dcc.pin(digitalPinToInterrupt(DCC_Pin), DCC_Pin, false);
Dcc.init(MAN_ID_DIY, 1, FLAGS_DCC_ACCESSORY_DECODER | FLAGS_OUTPUT_ADDRESS_MODE, 0);
pinMode(LED1Pin, OUTPUT);
pinMode(LED2Pin, OUTPUT);
pinMode(LED3Pin, OUTPUT);
pinMode(LED4Pin, OUTPUT);
pinMode(LED5Pin, OUTPUT);
pinMode(LED6Pin, OUTPUT);
pinMode(LED7Pin, OUTPUT);
pinMode(LED8Pin, OUTPUT);
}
void loop()
{
Dcc.process();
}