Hi all
This is my first post on this forum.
I have used several Nanos for DCC (Model Railway projects) decoders and they are easy to work with.
I decided to try the Xiao due to it’s compact size as it has more than enough connections for my project.
I use the DCC Decoder Master library (Github) and this uses pin 2 on an Arduino for the DCC signal.
I am using pins 3, 4, 5 & 6 for outputs.
The input comes through a 6N137 opto coupler, with the open collector output load to 3.3v to suit the Xiao.
This is the input at pin 2:
However I cannot get it to change the state of the output when I send a command to the Xiao from the DCC system.
This is the sketch and it works as is on a Nano:````cpp
#include <DCC_Decoder.h>
#define NUMACCESSORIES 2 // Enter the number of accessories here
// GO TO setup() TO CONFIGURE DCC ADDRESSES AND PIN NUMBERS
typedef struct DCCAccessoryData {
int address; // User Configurable DCC address
byte outputpin1; // User Configurable Arduino pin
byte outputpin2; // User Configurable Arduino pin
byte dccstate; // Internal use DCC state of accessory, 1=on, 0=off
};
DCCAccessoryData accessory[NUMACCESSORIES];
// The DCC library calls this function to set / reset accessories
void BasicAccDecoderPacket_Handler(int address, boolean activate, byte data) {
address -= 1;
address *= 4;
address += 1;
address += (data & 0x06) >> 1;
// address = address - 4; // uncomment this line for Roco Maus or z21
boolean enable = (data & 0x01) ? 1 : 0;
for (byte i=0; i<NUMACCESSORIES; i++) {
if (address == accessory[i].address) {
if (enable) accessory[i].dccstate = 1;
else accessory[i].dccstate = 0;
}
}
}
void setup() {
// CONFIGURATION OF ACCESSORIES
// Copy & Paste as many times as you have accessories
// The amount must be same as NUMACCESSORIES
// Don’t forget to increment the array index
accessory[0].address = 505; // DCC address
accessory[0].outputpin1 = 3; // Arduino pin
accessory[0].outputpin2 = 4; // Arduino pin
accessory[1].address = 506; // DCC address
accessory[1].outputpin1 = 5; // Arduino pin
accessory[1].outputpin2 = 6; // Arduino pin
// END OF CONFIGURATION OF ACCESSORIES
DCC.SetBasicAccessoryDecoderPacketHandler(BasicAccDecoderPacket_Handler, true);
DCC.SetupDecoder( 0x00, 0x00, 0 );
for(byte i=0; i<NUMACCESSORIES; i++) {
pinMode (accessory[i].outputpin1, OUTPUT);
digitalWrite(accessory[i].outputpin1, LOW);
pinMode (accessory[i].outputpin2, OUTPUT);
digitalWrite(accessory[i].outputpin2, HIGH);
}
}
void loop() {
DCC.loop(); // Call to library function that reads the DCC data
for(byte i=0; i<NUMACCESSORIES; i++) {
if (accessory[i].dccstate)
(digitalWrite(accessory[i].outputpin2, LOW), digitalWrite(accessory[i].outputpin1, HIGH));
else
(digitalWrite(accessory[i].outputpin1, LOW), digitalWrite(accessory[i].outputpin2, HIGH));
}
}
I have run a short sketch to prove that the input and outputs do actually work, so the Xiao is OK.
Any suggestions what might be wrong?
Cheers
Keith