// LGB Decoder by BR95009. V02, on Arduino mini Pro
// DCC reading PIN = 2 (from opto coupler)
// DCC decoder library to use
#include <DCC_Decoder.h>
// DECODER PARAMETERS:
#define DECODER_ADDRESS 2 // Decoder address as in multiMouse.
#define TURN_DELAY 110 // milleconds to wait to move turnout.
#define REVERS 0 // turn on reverse swithching if needed. - not realized yet
//Define interupt for DCC library
#define kDCC_INTERRUPT 0
//Driver control PINS for FAN8082 motor driver:
#define VIN1Pin 10
#define VIN2Pin 9
bool on_already = false; // flag to give command to turnout once but not for five consiquent similar DCC packets
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup(){
//Serial.begin(9600);
DCC.SetBasicAccessoryDecoderPacketHandler(BasicAccDecoderPacket_Handler, true);
DCC.SetupDecoder( 0x00, 0x00, kDCC_INTERRUPT );
pinMode(VIN1Pin, OUTPUT); pinMode(VIN2Pin, OUTPUT);
//Serial.println("Decoder has started. Waiting for packets...");
pinMode(13, OUTPUT); // set pin 13 as LED indicator for turnout state
digitalWrite(13, LOW); //off at the beginnning
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop(){
DCC.loop();// Loop DCC library
}
// Basic accessory packet handler
void BasicAccDecoderPacket_Handler(int address, boolean activate, byte data) {
// Convert NMRA packet address format to human address (Roco multiMouse numbering)
address -= 1; address *= 4; address += 1; address += (data & 0x06) >> 1; address += 4;
boolean enable = (data & 0x01) ? 1 : 0;
//Serial.print("Address sent: "); Serial.println(address, DEC);
if ( address == DECODER_ADDRESS) { // if adress in packet matches adress of our decoder then do the actions
//Serial.print("Basic addr: "); Serial.print(address, DEC); Serial.print(" enable(data): "); Serial.print(enable, DEC); Serial.print(" activate: "); Serial.println(enable, DEC);
if ( enable && !on_already) { // TO in pos 0
digitalWrite(VIN1Pin, HIGH); digitalWrite(VIN2Pin, LOW); digitalWrite(13, HIGH);
delay(TURN_DELAY);
digitalWrite(VIN1Pin, LOW); digitalWrite(VIN2Pin, LOW);
//Serial.println("ON");
on_already = true; //no more consequtive ONs.
}
if ( !enable && on_already) { // TO in pos 1
digitalWrite(VIN1Pin, LOW); digitalWrite(VIN2Pin, HIGH); digitalWrite(13, LOW);
delay(TURN_DELAY);
digitalWrite(VIN1Pin, LOW); digitalWrite(VIN2Pin, LOW);
//Serial.println("OFF");
on_already = false;
}
}
}//End of sketch