Try the code below. It might work. Use accessory decoder addresses 711 to 717.
Dave
#include <DCC_Decoder.h>
#include <AFMotor.h>
AF_Stepper motor(200, 1);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Defines and structures
//
#define kDCC_INTERRUPT 0
typedef struct
{
int address; // Address to respond to
byte output; // State of output 1=on, 0=off
int road; // Position of road on the traverser
boolean isDigital; // true=digital, false=analog. If analog must also set analogValue field
boolean isFlasher; // true=flash output, false=no time, no flash.
byte analogValue; // Value to use with analog type.
int durationMilli; // Milliseconds to leave output on for. 0 means don't auto off
unsigned long onMilli; // Used internally for timing
unsigned long offMilli; //
} DCCAccessoryAddress;
DCCAccessoryAddress gAddresses[8];
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Decoder Init
//
void ConfigureDecoder()
{
gAddresses[0].address = 711;
gAddresses[0].output = 1;
gAddresses[0].road = 0;
gAddresses[0].isDigital = true;
gAddresses[0].isFlasher = false;
gAddresses[0].analogValue = 250;
gAddresses[0].durationMilli = 0;
gAddresses[1].address = 712;
gAddresses[1].output = 1;
gAddresses[1].road = 1;
gAddresses[1].isDigital = true;
gAddresses[1].isFlasher = false;
gAddresses[1].analogValue = 0;
gAddresses[1].durationMilli = 0;
gAddresses[2].address = 713;
gAddresses[2].output = 0;
gAddresses[2].road = 2;
gAddresses[2].isDigital = true;
gAddresses[2].isFlasher = true;
gAddresses[2].analogValue = 250;
gAddresses[2].durationMilli = 500;
gAddresses[3].address = 714;
gAddresses[3].output = 0;
gAddresses[3].road = 3;
gAddresses[3].isDigital = true;
gAddresses[3].isFlasher = true;
gAddresses[3].analogValue = 0;
gAddresses[3].durationMilli = 500;
gAddresses[4].address = 715;
gAddresses[4].output = 0;
gAddresses[4].road = 4;
gAddresses[4].isDigital = true;
gAddresses[4].isFlasher = false;
gAddresses[4].analogValue = 250;
gAddresses[4].durationMilli = 0;
gAddresses[5].address = 716;
gAddresses[5].output = 0;
gAddresses[5].road = 5;
gAddresses[5].isDigital = true;
gAddresses[5].isFlasher = false;
gAddresses[5].analogValue = 0;
gAddresses[5].durationMilli = 0;
gAddresses[6].address = 717;
gAddresses[6].output = 0;
gAddresses[6].road = 6;
gAddresses[6].isDigital = true;
gAddresses[6].isFlasher = false;
gAddresses[6].analogValue = 0;
gAddresses[6].durationMilli = 0;
gAddresses[7].address = 718;
gAddresses[7].output = 0;
gAddresses[7].road = 7;
gAddresses[7].isDigital = true;
gAddresses[7].isFlasher = false;
gAddresses[7].analogValue = 0;
gAddresses[7].durationMilli = 0;
// // Setup output pins
// for(int i=0; i<(int)(sizeof(gAddresses)/sizeof(gAddresses[0])); i++)
// {
// if( gAddresses.outputPin )
// {
// pinMode( gAddresses.outputPin, OUTPUT );
// }
// gAddresses.onMilli = 0;
// gAddresses.offMilli = 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;
for(int i=0; i<(int)(sizeof(gAddresses)/sizeof(gAddresses[0])); i++)
{
if( address == gAddresses.address )
{
// Serial.print("Basic addr: ");
// Serial.print(address,DEC);
// Serial.print(" activate: ");
// Serial.println(enable,DEC);
if( enable )
{
gAddresses.output = 1;
gAddresses.onMilli = millis();
gAddresses.offMilli = 0;
}else{
gAddresses.output = 0;
gAddresses.onMilli = 0;
gAddresses.offMilli = millis();
}
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Setup
//
int posn = 0;
void setup()
{
// DCC setup
Serial.begin(9600); // set up Serial library at 9600 bps
DCC.SetBasicAccessoryDecoderPacketHandler(BasicAccDecoderPacket_Handler, true);
ConfigureDecoder();
DCC.SetupDecoder( 0x00, 0x00, kDCC_INTERRUPT );
// Stepper setup
motor.setSpeed(90); // 90 rpm
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Main loop
//
void loop()
{
static int addr = 0;
////////////////////////////////////////////////////////////////
// Loop DCC library
DCC.loop();
////////////////////////////////////////////////////////////////
// Bump to next address to test
if( ++addr >= (int)(sizeof(gAddresses)/sizeof(gAddresses[0])) )
{
addr = 0;
}
////////////////////////////////////////////////////////////////
// Turn off output?
// if( gAddresses[addr].offMilli && gAddresses[addr].offMilli<millis() )
// {
// // Clear off time
// gAddresses[addr].offMilli = 0;
//
// // Disable output
// if( gAddresses[addr].isDigital )
// {
// digitalWrite( gAddresses[addr].outputPin, LOW);
// }else{
// analogWrite( gAddresses[addr].outputPin, 0);
// }
//
// // If still enabled and a flash type, set on time
// if( gAddresses[addr].output && gAddresses[addr].isFlasher)
// {
// gAddresses[addr].onMilli = millis() + gAddresses[addr].durationMilli;
// }else{
// gAddresses[addr].output = 0;
// }
//
// return;
// }
////////////////////////////////////////////////////////////////
// Turn on output?
if( gAddresses[addr].onMilli && gAddresses[addr].onMilli<=millis() )
{
// Clear off time
gAddresses[addr].onMilli = 0;
// Enable output
if(gAddresses[addr].road-posn == 0)
{
Serial.print("Position: ");
Serial.println(posn, DEC);
Serial.print("Moving to: ");
Serial.println(gAddresses[addr].road, DEC);
Serial.println("Moving: 0");
posn = gAddresses[addr].road;
Serial.print("New position: ");
Serial.println(posn, DEC);
Serial.println(" ");
delay (200);
}
else if(gAddresses[addr].road-posn == -1)
{
Serial.print("Position: ");
Serial.println(posn, DEC);
Serial.print("Moving to: ");
Serial.println(gAddresses[addr].road, DEC);
Serial.println("Moving: -1");
posn = gAddresses[addr].road;
motor.step(250, BACKWARD, DOUBLE);
Serial.print("New position: ");
Serial.println(posn, DEC);
Serial.println(" ");
delay (200);
}
else if(gAddresses[addr].road-posn == -2)
{
Serial.print("Position: ");
Serial.println(posn, DEC);
Serial.print("Moving to: ");
Serial.println(gAddresses[addr].road, DEC);
Serial.println("Moving: -2");
posn = gAddresses[addr].road;
motor.step(500, BACKWARD, DOUBLE);
Serial.print("New position: ");
Serial.println(posn, DEC);
Serial.println(" ");
delay (200);
}
else if(gAddresses[addr].road-posn == -3)
{
Serial.print("Position: ");
Serial.println(posn, DEC);
Serial.print("Moving to: ");
Serial.println(gAddresses[addr].road, DEC);
Serial.println("Moving: -3");
posn = gAddresses[addr].road;
motor.step(750, BACKWARD, DOUBLE);
Serial.print("New position: ");
Serial.println(posn, DEC);
Serial.println(" ");
delay (200);
}
else if(gAddresses[addr].road-posn == -4)
{
Serial.print("Position: ");
Serial.println(posn, DEC);
Serial.print("Moving to: ");
Serial.println(gAddresses[addr].road, DEC);
Serial.println("Moving: -4");
posn = gAddresses[addr].road;
motor.step(1000, BACKWARD, DOUBLE);
Serial.print("New position: ");
Serial.println(posn, DEC);
Serial.println(" ");
delay (200);
}
else if(gAddresses[addr].road-posn == -5)
{
Serial.print("Position: ");
Serial.println(posn, DEC);
Serial.print("Moving to: ");
Serial.println(gAddresses[addr].road, DEC);
Serial.println("Moving: -5");
posn = gAddresses[addr].road;
motor.step(1250, BACKWARD, DOUBLE);
Serial.print("New position: ");
Serial.println(posn, DEC);
Serial.println(" ");
delay (200);
}
else if(gAddresses[addr].road-posn == -6)
{
Serial.print("Position: ");
Serial.println(posn, DEC);
Serial.print("Moving to: ");
Serial.println(gAddresses[addr].road, DEC);
Serial.println("Moving: -6");
posn = gAddresses[addr].road;
motor.step(1500, BACKWARD, DOUBLE);
Serial.print("New position: ");
Serial.println(posn, DEC);
Serial.println(" ");
delay (200);
}
else if(gAddresses[addr].road-posn == -7)
{
Serial.print("Position: ");
Serial.println(posn, DEC);
Serial.print("Moving to: ");
Serial.println(gAddresses[addr].road, DEC);
Serial.println("Moving: -7");
posn = gAddresses[addr].road;
motor.step(1750, BACKWARD, DOUBLE);
Serial.print("New position: ");
Serial.println(posn, DEC);
Serial.println(" ");
delay (200);
}
else if(gAddresses[addr].road-posn == 1)
{
Serial.print("Position: ");
Serial.println(posn, DEC);
Serial.print("Moving to: ");
Serial.println(gAddresses[addr].road, DEC);
Serial.println("Moving: +1");
posn = gAddresses[addr].road;
motor.step(250, FORWARD, DOUBLE);
Serial.print("New position: ");
Serial.println(posn, DEC);
Serial.println(" ");
delay (200);
}
else if(gAddresses[addr].road-posn == 2)
{
Serial.print("Position: ");
Serial.println(posn, DEC);
Serial.print("Moving to: ");
Serial.println(gAddresses[addr].road, DEC);
Serial.println("Moving: +2");
posn = gAddresses[addr].road;
motor.step(500, FORWARD, DOUBLE);
Serial.print("New position: ");
Serial.println(posn, DEC);
Serial.println(" ");
delay (200);
}
else if(gAddresses[addr].road-posn == 3)
{
Serial.print("Position: ");
Serial.println(posn, DEC);
Serial.print("Moving to: ");
Serial.println(gAddresses[addr].road, DEC);
Serial.println("Moving: +3");
posn = gAddresses[addr].road;
motor.step(750, FORWARD, DOUBLE);
Serial.print("New position: ");
Serial.println(posn, DEC);
Serial.println(" ");
delay (200);
}
else if(gAddresses[addr].road-posn == 4)
{
Serial.print("Position: ");
Serial.println(posn, DEC);
Serial.print("Moving to: ");
Serial.println(gAddresses[addr].road, DEC);
Serial.println("Moving: +4");
posn = gAddresses[addr].road;
motor.step(1000, FORWARD, DOUBLE);
Serial.print("New position: ");
Serial.println(posn, DEC);
Serial.println(" ");
delay (200);
}
else if(gAddresses[addr].road-posn == 5)
{
Serial.print("Position: ");
Serial.println(posn, DEC);
Serial.print("Moving to: ");
Serial.println(gAddresses[addr].road, DEC);
Serial.println("Moving: +5");
posn = gAddresses[addr].road;
motor.step(1250, FORWARD, DOUBLE);
Serial.print("New position: ");
Serial.println(posn, DEC);
Serial.println(" ");
delay (200);
}
else if(gAddresses[addr].road-posn == 6)
{
Serial.print("Position: ");
Serial.println(posn, DEC);
Serial.print("Moving to: ");
Serial.println(gAddresses[addr].road, DEC);
Serial.println("Moving: +6");
posn = gAddresses[addr].road;
motor.step(1500, FORWARD, DOUBLE);
Serial.print("New position: ");
Serial.println(posn, DEC);
Serial.println(" ");
delay (200);
}
else if(gAddresses[addr].road-posn == 7)
{
Serial.print("Position: ");
Serial.println(posn, DEC);
Serial.print("Moving to: ");
Serial.println(gAddresses[addr].road, DEC);
Serial.println("Moving: +7");
posn = gAddresses[addr].road;
motor.step(1750, FORWARD, DOUBLE);
Serial.print("New position: ");
Serial.println(posn, DEC);
Serial.println(" ");
delay (200);
}
else
{
return;
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////