Originally Posted by: GaryTrooper 
Originally Posted by: Holz1 
Good afternoon
As I already indicated my knowledge of programming with ARDUINo is nill.
I have made a sketch for switching a MArklin M electric turnout (double coils AC) by means of 2 infrared gates, 1 bistable HONGFA relay for indication of turnout position, and/or separate leds (on a breadboard) . I tried to use state change lines as you will see in the enclosed sketch.
When all is coupled, the IR gate1 wil switch the turnout1 and when passing ir gate 2 will switch back . After that nothong happens further.
Can you help with the missing parts
INFRARED GATES switching Marklin Turnouts 19 jan 2020.pdf (46kb) downloaded 70 time(s). Regards Henry
The Netherlands
First thing. Can you make your physical circuit work to switch the track without the Arduino? What I mean is if you apply 5 volts to where the Arduino output pins 8 or 9 would be connected, does the track switch between straight and bending and back?
It is going to take me a little while to get through your code.
I didn't go through the logic of your code but I did notice the IF statement brackets were out of alignment. I realigned the IF statement brackets and got the code to compile. See if this gets you anywhere. If not I would have to build a similar type test circuit.
/*Forum name Holz1
Author: Henry The Netherlands 19th January 2020
INFRARED GATES are switching Marklin M electric TURNOUTS (AC Double COIL) by means of a module based on Opto Couplers and Darlington NPN transistor. A separate
HONGFA //BiStable Relay 12V-DC, LEDs (RED and Green) can be turned on for indication of the turnout position.
The SKETCH is as follows:
*/
//Declaration of the 2 INFRARED Sensor Gates
const int IRS1 = 6; // de (INFRARED Gate) INFRAROOD sluis 1(IRS1) is op pin 6 PWM van de Arduino aangesloten
const int IRS2 = 7; // de (INFRARED Gate) INFRAROOD sluis 2 (IRS2)is op pin 7 PWM van de Arduino aangesloten
// Declaration of the IR sensor states
int IRsensorState1 = 0; // de huidige (present state) status van IRS 1 wordt bewaard
int IRsensorState2 = 0; //de huidige (present state)status van IRS 2 wordt bewaard
// Declaration of the LAST state of the LAST IR states
int lastSensorState1 = 0; // de (previous state) vorige status van de IRSluis 1
int lastSensorState2 = 0; // de (previous state) vorige status van de IRSluis 2
// Declaration of the HONGFA BI-STABLE relay for LED indication of turnout position: LED RED (BENDING) and LED GREEN (STRAIGHT)
#define relais1 2 // PWM pin 2 van de Arduino wordt op het relais pole 6 aangesloten
#define relais2 3 // PWM pin 3 van de Arduino wordt op het relais pole 7 aangesloten
// Separate LEDs can be installed on pin 11 (RED) en 12 (GREEN)
// The Marklin Turnouts are declared
#define Wissel_1 8 //Digitale pin 8 wordt op Märklin Wissel 1 aangesloten en is OUTPUT
#define Wissel_2 9 //Digitale pin 9 wordt op Märklin Wissel 2 aangesloten en is OUTPUT
#define RELAY_TIME_MS 150 // The time to switch is milliseconds for switching the
//MARKLIN Double AC COIL
void setup() {
// The IR gates are declared as INPUT /dit zijn de Infrarood lichtsluizen
pinMode(IRS1, INPUT); // IR sluis 1 is op input PWM pin 6 aangesloten
pinMode(IRS2, INPUT); // IR sluis 2 is op input PWM pin 7 aangesloten
// The MÄRKLIN TURNOUTS are declared as OUTPUT
pinMode(Wissel_1, OUTPUT); // Pin 8 schakelt Märklin Wissel 1 rechtdoor
pinMode(Wissel_2, OUTPUT); // Pin 9 schakelt Märklin Wissel 2 afbuigend
// The HONGFA relay is declared as OUTPUT and switches Green LED or RED LED /dit is bistabliele relais voor wisselstand terugmelding
pinMode(relais1, OUTPUT); // Wanneer IRS1 wordt gepasseerd, schakelt het relais1 op rood
pinMode(relais2, OUTPUT); // Wanneer IRS2 wordt gepasseerd, schakelt het relais2 op blauw
}
void loop() {
// The signal of the IR Sensors are read / het signaal van de IR sluizen wordt gelezen
int readsen1 = digitalRead (IRS1);
int readsen2 = digitalRead (IRS2);
IRsensorState1 = digitalRead (IRS1); // The IR sensor states are read for recovery and
//comparison of the last IR states
IRsensorState2 = digitalRead (IRS2);
if (readsen1 == HIGH && readsen2 == LOW) { // If IRS1 is HIGH and the IR Gate 2 is NOT
if (IRsensorState1 != lastSensorState1) { // ????????
digitalWrite(Wissel_1, HIGH); //Turnout 1 is switched / De Märklin wissel 1 schakelt naar //afbuigen
delay(RELAY_TIME_MS);
}
// save SensorState for next comparison: //???????????
lastSensorState1 = IRsensorState1; // ????????
// LED RED (BENDING) and LED GREEN (STRAIGHT)/ deze leds kunnen via pin 11 en 12 //apart worden aangesloten
digitalWrite(11, HIGH); // The Turnout position leds are switched through the HONGFA relay
digitalWrite(12, LOW); //digitalWrite zet led aan en brandt, analogWrite blijft led uit
// When The Turnout 1 is switched the HONGFA relay will switch the RED LED light to Bending/het relais ontvangt signaal en wordt geschakeld
digitalWrite(relais1, HIGH);
// The TURNOUT will be switch to BENDING/ De Märklin wissel 1 wordt geschakeld
digitalWrite(Wissel_1, HIGH); //Turnout 1 is switched De Märklin wissel 1 schakelt naar //afbuigen
delay(RELAY_TIME_MS);
digitalWrite(relais2, LOW); //????????
delay(RELAY_TIME_MS);
}
if (readsen2 == HIGH && readsen1 == LOW ) {
if (IRsensorState2 != lastSensorState2) {
digitalWrite(Wissel_2, HIGH); //The TURNOUT 2 is switched when the IRS2 is passed/ De
//Märklin wissel 2 schakelt naar afbuigen
delay(RELAY_TIME_MS);
}
// save SensorState for next comparison: lastSensorState2 = IRsensorState2; //???????????
// LED RED (BENDING) and LED GREEN (STRAIGHT)/deze leds kunnen via pin 11 en 12
//apart worden aangesloten
digitalWrite(11, LOW); // de IR sluis niet wordt gepasseerd brandt LED baanvak vrij
digitalWrite(12, HIGH); // de bezet melding is uit
digitalWrite(relais2, HIGH); // The Turnout position leds are switched through the HONGFA
//relay/ het relais schakelt het baanvak vrij in: de groene led brandt
delay(RELAY_TIME_MS);
digitalWrite(Wissel_2, HIGH); // The Turnout is switched to STRAIGHT /De Märklin wissel
//2 schakelt naar rechtdoor
delay(RELAY_TIME_MS);
digitalWrite(Wissel_1, LOW);
delay(RELAY_TIME_MS);
digitalWrite(relais1, LOW); // The HONGFA relay is switched : GREEN LED is on! /het relais
//schakelt de bezetmelding uit: de rode led is uit
}
}