Simple Train Shuttle

Hi
Newbie here so greetings to all.

Electrician by Trade but sadly not a programmer.

I am at a dead end with regards to this one, so a bit of help would be appreciated .

I am trying to program a simple train shuttle on a straight piece of track

so, the train Travels from left to right stops at the end of the track for 10 seconds

then travels from right to left and stops for a further 10 seconds.

I am using 2 relays to reverse the polarity

and one relay for killing the power Needed to stop the train at the end of its travel

there is 1 inferred sensor mounted at each end of the track.

I can't seem to get out of the if and else statement

The shabby code so far not finished

Thanks in advance

Laurence

//shuttle

int powerrelay = 1; // relay 1
int posrelay = 2;
int negrelay = 3;
int leftsensorinput = 10;
int rightsensorinput = 11;
int ls = 0; //this variable will read the value from the left sensor
int rs = 0; //this variable will read the value from the right sensor

void setup()

{

pinMode(powerrelay, OUTPUT); //declare the power relay as output
pinMode(posrelay, OUTPUT); //declare the pos relay as output
pinMode(negrelay, OUTPUT); //declare the neg relay as output
pinMode(leftsensorinput, INPUT); //declare infrared sensor as input
pinMode(rightsensorinput, INPUT); //declare infrared sensor as input

}
void loop(){

ls = digitalRead(leftsensorinput); // read input value from the left sensor
if (ls == HIGH) { //check if the input is HIGH
digitalWrite(powerrelay,LOW); // LOW = ON
digitalWrite(posrelay, HIGH); // HIGH = OFF
digitalWrite(negrelay, HIGH); // HIGH = OFF

}

else {
digitalWrite(powerrelay, HIGH); // HIGH = OFF
digitalWrite(posrelay, HIGH); // relay POS turned off
digitalWrite(negrelay, HIGH); // relay NEG turned off

}
{

delay(10000); // wait time for 10 seconds
digitalWrite(powerrelay,LOW); //
digitalWrite(posrelay, LOW); //
digitalWrite(negrelay, LOW); //

}
}

Use the state change example in the IDE to check for when the sensor state changes, not simply when it is one or other state.

To make your code easier to read, you can do things like

#define RELAY_ON LOW
#define RELAY_OFF HIGH
...
...
digitalWrite (powerrelay, RELAY_ON);

etc.

Please remember to use code tags when posting code.

int powerrelay = 1; Don't use pins 0 or 1 for simple I/O - these pins have much more important uses, like serial debugging.

You should need only two relays - one reverser and one on/off.

Thanks
I will look into the comments you made
the relays are single pole - common no/nc

Some results on a site search of 'train reverse'.

2020laurence:
Thanks
I will look into the comments you made
the relays are single pole - common no/nc

If you use two of the SPDT relays to reverse the power, then energize just one to turn the power off.

Paul

Hi Paul
Yep a SPDT would be ideal but only have the SPST on an 8 pack
the reason for this is to switch other things as well at a later stage (signals etc )

How do you reverse the polarity using only 2 SPST switches? Maybe I need a coffee? :slight_smile:

Schematic.
Post an image

Sorry typo
SPDT

Has anyone got a code for this type of scenario for me to view and try to understand the process

Thanks

Have searched the internet high and low but to no avail
PWM seems to be the future but need to switch with relays

ideal scenario

And how is it currently performing to match your expectations?

Its Not

Totally Stuck on this one

Go back to reply #5 before you continue. I think you missed something important there.

Thanks aarg
I understand where Paul is coming from.
Both my SPDT relays will be switched at the same time to reverse the polarity

2020laurence:
Thanks aarg
I understand where Paul is coming from.
Both my SPDT relays will be switched at the same time to reverse the polarity

...and to apply/remove power, right? To drive in a point, you only need 2 relays, not 3. Just making sure you understand.

2 SPDT.
mot-rly.png
I would use 2 DPST, but...

mot-rly.png

If you simply reverse polarity, it starts running in the other direction.

Whereas if you switch just one relay, you have both side of the motor at the same voltage, and it stops (or at least stops powering the motor)

dochawk:
If you simply reverse polarity, it starts running in the other direction.

Whereas if you switch just one relay, you have both side of the motor at the same voltage, and it stops (or at least stops powering the motor)

Are you referring to the diagram in post #16? There, neither relay is energized, and the result is that the motor is turned off.

Hi Chaps
Actually found a bit of code and adapted it to my needs,I need to update the comments on the right but
works perfectly .
Many thanks to the original code writer Johnwasser

This has taken me to new directions with regards to coding

The 3 relays are all working nicely

Best regards and Thanks again


//shuttle

// Back and forth train control.
// When a reedswitch is activated the train will stop, pauze for 2 seconds and than continu in opposite direction
// Also a LED will be switched on to indicate which reedswitch is activated

// 3 seperate reed sensors on pin 2, 3 en 4 en led output op 5, 6, 7.

// connect motor controller pins to Arduino digital pins
int relpower = 4;
int rel2pos = 5;
int rel3neg = 6;
int lhssensor = 2;
int rhssensor = 3;

// variables will change:
int reedState = 0; // variable for reading the reed status
bool MovingForeward = true;

void setup()
{
Serial.begin(9600);
// print program name to serial monitor to indicate which program is running on the arduino
Serial.print("sensor train control test1");

// set all the motor control pins to outputs
pinMode(relpower, OUTPUT);
pinMode(rel2pos, OUTPUT);
pinMode(rel3neg, OUTPUT);

// Set all sensors as input
pinMode(2, INPUT); // lhs sensor
pinMode(3, INPUT); // rhs input

Foreward(); // Get started
MovingForeward = true;
}

void Foreward()
{
if (!MovingForeward)
{
OFF(); // Stop train when reedswitch is activated
delay(3000); // pauze for 2 seconds before moving in opposite direction
digitalWrite(relpower, LOW);
digitalWrite(rel2pos, HIGH); // set direction
digitalWrite(rel3neg, HIGH);
MovingForeward = true;
}
}

void Reverse()
{
if (MovingForeward)
{
OFF();
delay(3000);
digitalWrite(relpower, LOW);
digitalWrite(rel2pos, LOW);
digitalWrite(rel3neg, LOW);
MovingForeward = false;
}
}

void OFF()
{
digitalWrite(relpower, HIGH);
digitalWrite(relpower, HIGH);
digitalWrite(relpower, HIGH);
}
void loop()
{
// check if the reedswitch on pin 2 is activated (HIGH).
// if it is, set LED high and move train foreward.
if (digitalRead(2) != HIGH)
{

Foreward();
}

// check if the reedswitch on pin 3 is activated (HIGH).
// if it is, set LED high and move train reverse.
if (digitalRead(3) != HIGH)
{

Reverse();
}

}