Het proefbaantje uit deel 6 is weer verder uitgebouwd. Naast de S88 onderdelen (2x Arduino Nano & 1x OKKIE8) heeft de “DCC-naar-Arduino omzetter” een plaatsje gekregen en is vervolgens aangesloten op twee Arduino Nano’s om accessoires aan te sturen: één voor het aansturen van specifiek servo’s, de andere voor het aansturen van algemene accessoires (zoals een sein, een wissel, een ontkoppelrail).
Ook is er een voeding bijgekomen voor 15V en voor 5V. Middels verdeelstrips zijn deze twee voltages op meerder plaatsen beschikbaar.
De Automatische Knipper Installatie AKI (uit deel 24 van de vorige serie) heeft een plekje gekregen en er is een breadboard geplaatst voor de nodige testjes.
Alle eerdere testjes komen hier samen. De in eerdere delen opgenomen sketches zullen verwijderd worden. Vanaf nu zullen alleen sketches gelist worden die daadwerkelijk ingezet zijn/worden in de proefbaan. Dit om verwarring (vooral voor mijzelf 🙂 ) te voorkomen.

Hieronder worden de diverse onderdelen behandeld.
Sketches
/*
S88 occupancy sensor interface to Command Station (in my case an ESU ECoS2)
Software by Ruud Boer, November 2014.
Freely distributable for private, non commercial, use.
Connections for S88 bus:
s88 pin 1 Data - Arduino pin 13 = Data_Out to Oommand Station, or to the previous Arduino in the chain
s88 pin 2 GND - Arduino GND
s88 pin 3 Clock - Arduino pin 2, interrupt 0
s88 pin 4 PS - Arduino pin 3, interrupt 1
S66 pin 5 Reset (not used here) - Arduino pin 12, used as DATA IN from previous Arduino DATA OUT
s88 pin 6 V+ - Arduino 5V
IMPORTANT: To avoid S88 signals to jitter, it is best to put DATA_in pin 12 to GND on the last Arduino in the chain.
Connections for sensors: see table in void Setup() at line 35.
REMARK1: Inputs have the internal pullup resistor active, the sensors must pull the input to GND.
REMARK2: How short a pulse is allowed from the sensors before it is not seen?
A test showed that the main loop where all sensors are read runs once every 76 microseconds.
If a train runs over the reed switch with a speed of 1m/s, which is over 300 km/hr, that translates to 1 mm/ms.
So even if the reed switch would be on only for a 1 mm travel distance, then still the Arduino
will read that info more than 10 times!
*/
int clockCounter=0;
long loopCounter=0; //used in lines 55 and 88, see there for explanation
unsigned int sensors=0;
unsigned int data=0xffff;
const byte dataIn=12; //data input from next Arduino in S88 chain
const byte dataOut=13; //data output pin=13
boolean loadSensors=false; //flag that says to load sensor bits into dataOut bits
void setup() {
pinMode(2, INPUT_PULLUP);
attachInterrupt(0,clock,RISING); //pin 2 = clock interrupt
pinMode(3, INPUT_PULLUP);
attachInterrupt(1,PS,RISING); //pin 3 = PS interrupt
pinMode(dataIn,INPUT_PULLUP); //pin 12 = data in from next Arduino S88 in chain
pinMode(dataOut, OUTPUT); //pin 13 = data out to ECoS or to previous Arduino in S88 chain
digitalWrite(dataOut, LOW); //LED off
pinMode(A0, INPUT_PULLUP); //sensor 01
pinMode(A1, INPUT_PULLUP); //sensor 02
pinMode(A2, INPUT_PULLUP); //sensor 03
pinMode(A3, INPUT_PULLUP); //sensor 04
pinMode(A4, INPUT_PULLUP); //sensor 05
pinMode(A5, INPUT_PULLUP); //sensor 06
pinMode(0, INPUT_PULLUP); //sensor 07
pinMode(1, INPUT_PULLUP); //sensor 08
pinMode(4, INPUT_PULLUP); //sensor 09
pinMode(5, INPUT_PULLUP); //sensor 10
pinMode(6, INPUT_PULLUP); //sensor 11
pinMode(7, INPUT_PULLUP); //sensor 12
pinMode(8, INPUT_PULLUP); //sensor 13
pinMode(9, INPUT_PULLUP); //sensor 14
pinMode(10, INPUT_PULLUP); //sensor 15
pinMode(11, INPUT_PULLUP); //sensor 16
}
void loop() {
if (loopCounter==600){bitSet(sensors,0);}
/*
For an unknown reason the ECoS sets the first 8 bits to 1 after startup / reset of the S88 Arduino's.
When one of the sensor inputs is changed, from there on everything goes well.
Therefore, over here we give sensor bit 0 an automatic change after 30 seconds, when the ECoS is fully started.
The 1 second is created via 'loopCounter', which increments in the PS interrupt (line 88).
There are appr0ximately 20 PS pulses per second, therefore we use 20x30=600 in the if statement.
*/
if (!digitalRead(A0)) {bitSet(sensors,0);}
if (!digitalRead(A1)) {bitSet(sensors,1);}
if (!digitalRead(A2)) {bitSet(sensors,2);}
if (!digitalRead(A3)) {bitSet(sensors,3);}
if (!digitalRead(A4)) {bitSet(sensors,4);}
if (!digitalRead(A5)) {bitSet(sensors,5);}
if (!digitalRead(0)) {bitSet(sensors,6);}
if (!digitalRead(1)) {bitSet(sensors,7);}
if (!digitalRead(4)) {bitSet(sensors,8);}
if (!digitalRead(5)) {bitSet(sensors,9);}
if (!digitalRead(6)) {bitSet(sensors,10);}
if (!digitalRead(7)) {bitSet(sensors,11);}
if (!digitalRead(8)) {bitSet(sensors,12);}
if (!digitalRead(9)) {bitSet(sensors,13);}
if (!digitalRead(10)) {bitSet(sensors,14);}
if (!digitalRead(11)) {bitSet(sensors,15);}
}
void PS() {
clockCounter=0;
data=sensors;
sensors=0;
loopCounter++; //Increment loopCounter to cretae a timer. See line 55 for explanation.
}
void clock() {
digitalWrite(dataOut,bitRead(data,clockCounter));
delayMicroseconds(16); //Delay makes reading output signal from next Arduino in chain more reliable.
bitWrite(data,clockCounter,digitalRead(dataIn));
clockCounter =(clockCounter +1) % 16;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// DCC Accessory / Function Decoder
// Author: Ruud Boer - September 2015
// This sketch turns an Arduino into a DCC decoder with max 17 function outputs.
// Output pins used: 3-19 (14-19 = A0-A5). Pin becomes LOW when accessory is switched ON
// Modes: 1-continuous, 2=oneshot, 3=flasher with 2 alternatin outputs, 4=signal with 2 inverted outputs
// The DCC signal is fed to pin 2 (=Interrupt 0).
// Optocoupler schematics for DCC to 5V conversion: www.rudysmodelrailway.wordpress.com/software
// Many thanks to www.mynabay.com for publishing their DCC monitor and -decoder code.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTANT: GOTO lines 22 and 43 to configure some data!
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <DCC_Decoder.h>
#define kDCC_INTERRUPT 0
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Fill in the number of accessories / functions you want to control
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const byte maxaccessories = 4;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
typedef struct {
int address; // User Configurable. DCC address to respond to
byte mode; // User Configurable. Mode: 1=Continuous, 2=Oneshot, 3=Flasher
byte outputPin; // User Configurable. Arduino pin where accessory is connected to
byte outputPin2; // User Configurable. 2nd pin for AlternatingFlasher (e.g. railway crossing)
int ontime; // User Configurable. Oneshot or Flasher on time in ms
int offtime; // User Configurable. Flasher off time in ms
byte onoff; // User Configurable. Initial state of accessory output: 1=on, 0=off (ON = pin LOW)
byte onoff2; // User Configurable. Initial state of 2nd output: 1=on, 0=off
byte dccstate; // Internal use. DCC state of accessory: 1=on, 0=off
byte finished; // Internal use. Memory that says the Oneshot is finished
unsigned long onMilli; // Internal use.
unsigned long offMilli; // Internal use.
}
DCCAccessoryAddress;
DCCAccessoryAddress accessory[maxaccessories];
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Fill in the attributes for every accessory / function
// COPY - PASTE as many times as you have functions. The amount must be same as in line 22 above!
//
//
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ConfigureDecoderFunctions() // The amount of accessories must be same as in line 22 above!
{
/*
accessory[0].address = 50; // DCC address
accessory[0].mode = 1; // Continuous: HIGH until DCC switches the address off again
accessory[0].outputPin = 3; // Arduino pin to which this accessory is connected
accessory[1].address = 51;
accessory[1].mode = 2; // Oneshot: HIGH for ontime ms, then LOW and stays LOW.
accessory[1].outputPin = 4;
accessory[1].ontime = 1000;
accessory[2].address = 52;
accessory[2].mode = 3; // Flasher: HIGH for ontime ms, LOW for offtime ms, repeats till DCC off
accessory[2].outputPin = 5;
accessory[2].outputPin2 = 6; // Flasher can use 2 outputs, they will flash on/off alternatively
accessory[2].ontime = 500;
accessory[2].offtime = 500;
accessory[3].address = 53; // DCC address
accessory[3].mode = 4; // Continuous: HIGH until DCC switches the address off again
accessory[3].outputPin = 7; // Green signal
accessory[3].outputPin2 = 8; // Red Signal
accessory[3].onoff2 = 1; // Initially set Red signal to ON
*/
accessory[0].address = 50; // DCC address
accessory[0].mode = 2; // Oneshot: HIGH for ontime ms, then LOW and stays LOW.
accessory[0].outputPin = 3;
accessory[0].ontime = 1000;
accessory[1].address = 51;
accessory[1].mode = 2; // Oneshot: HIGH for ontime ms, then LOW and stays LOW.
accessory[1].outputPin = 4;
accessory[1].ontime = 1000;
accessory[2].address = 52;
accessory[2].mode = 2; // Oneshot: HIGH for ontime ms, then LOW and stays LOW.
accessory[2].outputPin = 5;
accessory[2].ontime = 1000;
accessory[3].address = 53; // DCC address
accessory[3].mode = 2; // Oneshot: HIGH for ontime ms, then LOW and stays LOW.
accessory[3].outputPin = 6;
accessory[3].ontime = 1000;
} // END ConfigureDecoderFunctions
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// DCC 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<maxaccessories; i++)
{
if (address == accessory[i].address)
{
if (enable) accessory[i].dccstate = 1;
else accessory[i].dccstate = 0;
}
}
} //END BasicAccDecoderPacket_Handler
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Setup (run once)
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup()
{
ConfigureDecoderFunctions();
DCC.SetBasicAccessoryDecoderPacketHandler(BasicAccDecoderPacket_Handler, true);
DCC.SetupDecoder( 0x00, 0x00, kDCC_INTERRUPT );
pinMode(2,INPUT_PULLUP); // Interrupt 0 with internal pull up resistor (can get rid of external 10k)
for(int i=3; i<20; i++)
{
pinMode(i, OUTPUT);
digitalWrite(i, LOW); //all function outputs are set to 0 at startup - SV: LOW ipv HIGH
}
} // END setup
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Main loop (run continuous)
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop()
{
static int addr = 0;
DCC.loop(); // Loop DCC library
if( ++addr >= maxaccessories ) addr = 0; // Next address to test
if (accessory[addr].dccstate)
{
switch (accessory[addr].mode)
{
case 1: // Continuous
accessory[addr].onoff = 1;
break;
case 2: // Oneshot
if (!accessory[addr].onoff && !accessory[addr].finished)
{
accessory[addr].onoff = 1;
accessory[addr].offMilli = millis() + accessory[addr].ontime;
}
if (accessory[addr].onoff && millis() > accessory[addr].offMilli)
{
accessory[addr].onoff = 0;
accessory[addr].finished = true; //this is reset to flase below in the 'else' statement
}
break;
case 3: // Flasher, is always an 'alternating' flasher together with .outputPin2
if (!accessory[addr].onoff && millis() > accessory[addr].onMilli)
{
accessory[addr].onoff = 1;
accessory[addr].onoff2 = 0;
accessory[addr].offMilli = millis() + accessory[addr].ontime;
}
if (accessory[addr].onoff && millis() > accessory[addr].offMilli)
{
accessory[addr].onoff = 0;
accessory[addr].onoff2 = 1;
accessory[addr].onMilli = millis() + accessory[addr].offtime;
}
break;
case 4: // Signal
accessory[addr].onoff = 1;
accessory[addr].onoff2 = 0;
break;
}
}
else //accessory[addr].dccstate == 0
{
accessory[addr].onoff = 0;
if (accessory[addr].mode == 4) accessory[addr].onoff2 = 1; else accessory[addr].onoff2 = 0;
if (accessory[addr].mode == 2) accessory[addr].finished = false; // Oneshot finished by DCCstate, not by ontime
}
// activate outputpin, based on value of onoff
if (accessory[addr].onoff) digitalWrite(accessory[addr].outputPin, LOW);
else digitalWrite(accessory[addr].outputPin, HIGH);
if (accessory[addr].onoff2) digitalWrite(accessory[addr].outputPin2, LOW);
else digitalWrite(accessory[addr].outputPin2, HIGH);
} //END loop
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Arduino DCC Servo and Function Decoder
// Version: 1.4 - 2015-04-23
// Author: Ruud Boer
// This sketch turns an Arduino into a DCC decoder with max 12 servo motor outputs combined with function outputs.
// The DCC signal is optically separated and fed to pin 2 (=Interrupt 0). Schematics: www.mynabay.com
// Many thanks to www.mynabay.com for publishing their DCC monitor and -decoder code, which is used in this sketch.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTANT: GOTO lines 25 and 44 to configure some data!
// IMPORTANT: To avoid servo movement and possible high current draw at startup:
// - First start the Arduino, the software now sets the servo angle values to 'offangle'.
// - After a few seconds, switch the servo power on ... they will possibly show just a minor jitter.
// - This only works if you set all servo's to offangle before shutdown!
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <DCC_Decoder.h>
#include <Servo.h>
#define kDCC_INTERRUPT 0
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Fill in these 2 values ...
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const byte maxservos = 10; //The number of servos you have connected to this Arduino
const byte servotimer = 60; //Servo angle change timer. Lower value -> higher speed
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
unsigned long timetoupdatesetpoint = millis() + servotimer;
struct servoItem {
int address; // DCC address to respond to
byte output; // State of DCC accessory: 1=on, 0=off (ECoS: on=straight, off=turnout)
byte outputPin; // Arduino output pin for additional function (not where servo is attached to)
byte angle;
byte setpoint;
byte offangle;
byte onangle;
Servo servo;
};
servoItem servos[maxservos];
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Fill in the data for every function and servo.
// COPY - PASTE as many times as you have functions. The amount must be same as in line 22 above!
// A servo is coupled to an accessory[n]. It rotates based on accessory[n].output = 1 (CCW) or 0 (CW)
// If you have multiple servos you need to couple them to different accessories. However ...
// accessories may switch the same output pin (e.g. pin 13, which has the on board led attached)
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ConfigureFunctionsAndServos()
{
servos[0].address = 30; // DCC address for this accessory
servos[0].outputPin = 13; // Arduino pin number for additional function output (not where servo is attached to)
servos[0].servo.attach(3); //Arduino pin number where servo is connected to
servos[0].offangle = 68; //Angle for DCC=off. For ECoS turnout is DCC off, straight is DCC on.
servos[0].onangle = 126; //Angle for DCC=on. For ECoS turnout is DCC off, straight is DCC on.
servos[1].address = 31;
servos[1].outputPin = 13;
servos[1].servo.attach(4);
servos[1].offangle = 112;
servos[1].onangle = 62;
servos[2].address = 32;
servos[2].outputPin = 13;
servos[2].servo.attach(5);
servos[2].offangle = 128;
servos[2].onangle = 58;
servos[3].address = 33;
servos[3].outputPin = 13;
servos[3].servo.attach(6);
servos[3].offangle = 50;
servos[3].onangle = 128;
servos[4].address = 34;
servos[4].outputPin = 13;
servos[4].servo.attach(7);
servos[4].offangle = 60;
servos[4].onangle = 132;
servos[5].address = 35;
servos[5].outputPin = 13;
servos[5].servo.attach(8);
servos[5].offangle = 60;
servos[5].onangle = 116;
servos[6].address = 36;
servos[6].outputPin = 13;
servos[6].servo.attach(9);
servos[6].offangle = 46;
servos[6].onangle = 128;
servos[7].address = 37;
servos[7].outputPin = 13;
servos[7].servo.attach(10);
servos[7].offangle = 64;
servos[7].onangle = 124;
servos[8].address = 38;
servos[8].outputPin = 13;
servos[8].servo.attach(11);
servos[8].offangle = 50;
servos[8].onangle = 138;
servos[9].address = 39;
servos[9].outputPin = 13;
servos[9].servo.attach(12);
servos[9].offangle = 124;
servos[9].onangle = 64;
} // END ConfigureFunctionsAndServos()
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// DCC 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<maxservos; i++)
{
if(address == servos[i].address)
{
if(enable) servos[i].output = 1;
else servos[i].output = 0;
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Setup (run once)
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup()
{
DCC.SetBasicAccessoryDecoderPacketHandler(BasicAccDecoderPacket_Handler, true);
ConfigureFunctionsAndServos();
for(int i=0; i<maxservos; i++)
{
pinMode (servos[i].outputPin, OUTPUT );
digitalWrite (servos[i].outputPin, LOW);
servos[i].angle = servos[i].offangle; // Set start up angle to avoid movement at power on
}
DCC.SetupDecoder( 0x00, 0x00, kDCC_INTERRUPT );
pinMode(2,INPUT_PULLUP); //Interrupt 0 with internal pull up resistor (can get rid of external 10k)
pinMode(13,OUTPUT);
digitalWrite(13,LOW); //led off at startup
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Main loop (run continuous)
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop()
{
static int addr = 0;
DCC.loop(); // DCC library
if(++addr >= maxservos) addr = 0; // Next address to test
// Set servos output pin
if (servos[addr].output) digitalWrite(servos[addr].outputPin, HIGH);
else digitalWrite(servos[addr].outputPin, LOW);
// Every 'servotimer' ms, modify setpoints and move servos 1 step (if needed)
if (millis() > timetoupdatesetpoint)
{
timetoupdatesetpoint = millis() + servotimer;
for (int n=0; n<maxservos; n++)
{
if (servos[n].output) servos[n].setpoint=servos[n].onangle;
else servos[n].setpoint=servos[n].offangle;
if (servos[n].angle < servos[n].setpoint) servos[n].angle++;
if (servos[n].angle > servos[n].setpoint) servos[n].angle--;
servos[n].servo.write(servos[n].angle);
}
}
} //END MAIN LOOP