Simple DCC Interface Sketch…

So you have an Arduino Uno/Nano, and a DCC Interface…  now lets write a simple sketch to start you on your project….

 

Using the MynaBay DCC Library, create the following sketch…..


//
// DCC Interface Simple Testing Sketch - Ian Jeffery 2017
//
#include 

#define kDCC_INTERRUPT            0

//
// Basic accessory packet handler 
//
void BasicAccDecoderPacket_Handler(int address, boolean activate, byte data)
{
	// Convert NMRA packet address format to human address
	address -= 1;
	address *= 4;
	address += 1;
	address += (data & 0x06) >> 1;
	boolean enable = (data & 0x01) ? 1 : 0;
	// The DCC Accessory Address is now stored in "address" variable
	Serial.print(F("Basic addr: "));
	Serial.println(address, DEC);
	Serial.print(F("Activate Status: "));
	Serial.println(enable, DEC);
}

void setup()
{
	Serial.begin(9600);
	Serial.println(F("DCC Interface Simple Sketch - Ian Jeffery. 2017"));
	// tell the library which method to call when an accessory packet is detected
	DCC.SetBasicAccessoryDecoderPacketHandler(BasicAccDecoderPacket_Handler, true);
	// set up our DCC decoder
	DCC.SetupDecoder(0x00, 0x00, kDCC_INTERRUPT);

}

void loop()
{
	// Loop DCC library
	DCC.loop();
}

The MynaBay DCC Library reads the DCC Input via the D2 ( Digital Pin 2 ) external interrupt pin on the Arduino – if you are using the DCC Interface standalone board – connect the “ARD” pin to D2 as shown here…

As you operate accessory addresses on your DCC handset, you will see the addresses and the enabled status ( point open or closed ) appear on the serial monitor ( Open Serial Monitor from Tools ).

The next step would be to act on certain addresses, effectively turn the Arduino in to an Accessory Decoder – as we will see in the next page…