back and forth train control with reed switches

Hello, I’m Maarten and making a test track for my model n-scale train project.
The track consists of 1 track and 1 side track operated by a switch controlled by an arduino uno. That part works fine with lights and relay for switching on and of power to the tracks.

Now an other arduino uno is controlling the train speed and direction. I’ve a L298N motor controller and 3 reed sensor modules at the end of the track detecting if a locomotive is there.

Also this part works oke. Upon detection the loc stops, indication light switches on en after a few seconds the direction is reversed.

But if the loc passes the reedswitch, it stops, LED will light up, but it doesn’t change direction anymore until I push the loc back above the reedswitch???

I’ve tried many things with delays, try to keep the “HIGH” signal, but no succes yet.

I really don’t know what else I could try now to continu the reverse direction even when the reedswitch is not activated anymore after the loc passes the switch.

reed_detectie_en_motor_werkend_5.ino (4.14 KB)

See sketch:

// set pin numbers:
const int reed = {2, 3, 4}; // the number of the reed pins
const int ledPin1 = 5; // the number of ledPin
const int ledPin2 = 6; // the number of ledPin
const int ledPin3 = 7; // the number of ledPin

// connect motor controller pins to Arduino digital pins
int enA = 10;
int in1 = 9;
int in2 = 8;

// variables will change:
int reedState = 1; // variable for reading the reed status

unsigned long interval = 2500;
unsigned long startTime;
byte lastPress;

void setup() {

Serial.begin(9600);
Serial.print(“reed detectie en motor werkend 5”);

// set all the motor control pins to outputs
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);

// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);

int reedState = 1;

for (int x = 0; x < 3; x++)
{
pinMode(reed, INPUT);
}
}

void loop() {

// read the state of the reed value one by one (3 in total in this case):
for (int x = 0; x < 3; x++)
{
reedState = digitalRead(reed);

if (reedState == HIGH && reed == 2) {

// turn occupation LED on:
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
// turn on motor direction 1

Foreward();
delay(20);
}

if (reedState == HIGH && reed == 3) {

// turn block free LED on:
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, LOW);
// turn on motor direction 2

Reverse1();
delay(20);
}

if (reedState == HIGH && reed == 4) {

// turn block free LED on:
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, HIGH);
// turn on motor direction 2

Reverse2();
delay(20);
}
}
}

void Foreward() {

for (int x = 0; x < 3; x++)
{
reedState = digitalRead(reed);

byte pressedF = digitalRead(2);
if (pressedF)
{
if (pressedF != lastPress)
{
OFF();
startTime = millis();
}
}

lastPress = pressedF;
if (millis() - startTime >= interval)
{
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(enA, 90);
delay(50);

}
}
}
void Reverse1() {

for (int x = 0; x < 3; x++)
{
reedState = digitalRead(reed);

byte pressedR1 = digitalRead(3);
if (pressedR1)
{
if (pressedR1 != lastPress)
{
OFF();
startTime = millis();
}
}
lastPress = pressedR1;

if (millis() - startTime >= interval)
{
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(enA, 90);
delay(50);

}
}
}

void Reverse2() {

for (int x = 0; x < 3; x++)
{
reedState = digitalRead(reed);

byte pressedR2 = digitalRead(4);
if (pressedR2)
{
if (pressedR2 != lastPress)
{
OFF();
startTime = millis();
}
}
lastPress = pressedR2;

if (millis() - startTime >= interval)
{
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(enA, 110);
delay(50);

}
}
}

void OFF() {

digitalWrite(in1, LOW);
digitalWrite(in2, LOW);

}

If you post your code in code tags, like is described in the how to use this forum sticky (see #7), your code won’t be messed up by the forum software.

Comments in your code could help us understand what you are trying to do.

Also look into the finite state machine technique. This sounds like a project that can be well implemented that way.

A moden motor driver would also a big improvement. Such as the DRV8835 or TB6621FNG dual H-bridge ICs - cost roughly the same, but don't need that massive heat sink as they don't waste so much power. The L298N is best used as paperweight.