Auto-reversing model train engine

I need help writing a program. Yes, I am new to Arduino and at 75 years old it is hard for me to understand the programming. Believe me I have tried but after a week of trying to learn it and reading Arduino for Dummies book, I realize it is something I cannot grasps.

What I am trying do do is:
Auto-reversing model train engine using Arduino Nano and two limit switches.

  1. Switch one (ST1) start train moving to the right (output 1 high, output 2 low)
  2. Train starts moving right
  3. Switch two (LSR2) Limit Switch Right trigger stops train (output 1 high, output 2 high)
  4. Delay 5 seconds,
  5. Then (output 1 low, output 2 high) starts train engine moving to the left.
  6. Switch three (LSL3) Limit Switch Left trigger stops train (output 1 high, output 2 high)
  7. Delay 5 seconds,
  8. Then (output 1 high, output 2 low), repeat step two

I will utilize an H-Bridge or two relays (for power reversing) to control the power to the track, due to the amperage required to power the engine.

Thank you for any assistance you can provide.

Brian Krupicka
trains@dbcomserv.com

Many confusing issues here, not the least of which is your age. Seems every newbie claiming to be of age is 75 years old, as if that should generate some sympathy. I'm past 60 myself and have never used it as an excuse. If in fact you are 75, well done and welcome. Regardless of your age Brian, there is little anyone can do with the description given other than ask questions. This is not a code-writing service where you table your wish list and the munchkins go to work in the back room and weave a solution for you. Give us a copy of your diagram (hand drawn is fine, pictures of the trains would be ok too), and any code, copy of code or links to other projects YOU have already Googled that are similar. We munchkins will have a look and see how we can assist.

DBRR_BrianK:
I need help writing a program. Yes, I am new to Arduino and at 75 years old it is hard for me to understand the programming. Believe me I have tried but after a week of trying to learn it and reading Arduino for Dummies book, I realize it is something I cannot grasps.

If you want someone to write a program for you please ask in the Gigs and Collaborations section of the Forum and be prepared to pay.

If you want to learn to do it yourself then this is the place. Start by telling us what part of the project you have been able to do. And if you have any program code please post it.

...R

Here is the code I have pieced together The "timed restart" does not seem to work.
Please advise, if possible.

To DKWatson
Yes I am 75 and yes MY mind is slowly deteriorating due to medical issues.
I am trying to help my grandson before I will be unable in a few years. This is no plea, it is reality.

Thank you
Brian K

int sw1, sw2, sw3;
const int st6 = 6; //Start
const int lsr4 = 4; //Limit Switch Right
const int lsl12 = 12; //Limit Switch Left
const int cw = 2; // To reversing relay 1
const int cww = 3; // To reversing relay 2

void setup() {

// put your setup code here, to run once:
pinMode (st6, INPUT_PULLUP);
pinMode (lsr4, INPUT_PULLUP);
pinMode (lsl12, INPUT_PULLUP);
pinMode (cw, OUTPUT);
pinMode (cww, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:

sw1 = digitalRead(st6);
sw2 = digitalRead(lsr4);
sw3 = digitalRead(lsl12);

if (sw1 == LOW){
do {
digitalWrite(cw, HIGH);
digitalWrite(cww, HIGH);
}while (sw1 = LOW);
}

if (sw2 == HIGH && sw3 == LOW){
do {
digitalWrite (cw, HIGH);
digitalWrite (cww, LOW);

sw2 = digitalRead(lsr4);
}while (sw2 == HIGH);
}

else
if (sw2 == LOW && sw3 == HIGH){
do {
digitalWrite (cw, LOW);
digitalWrite (cww, HIGH);

sw3 = digitalRead(lsl12);
}while (sw3 == HIGH);
}

static enum {MOVING_CW, MOVING_CWW, STOPPED_CW, STOPPED_CWW} state;
static unsigned long time_stopped;
unsigned long now = millis();

switch (state) {
case MOVING_CW:
if (digitalRead(lsr4) == LOW) { // hit switch
digitalWrite(cw, LOW); // stop
state = STOPPED_CW;
time_stopped = now;
}
break;
case MOVING_CWW:
if (digitalRead(lsl12) == LOW) { // hit switch
digitalWrite(cww, LOW); // stop
state = STOPPED_CWW;
time_stopped = now;
}
break;
case STOPPED_CW:
if (now - time_stopped >= 5000) {
digitalWrite(cww, HIGH); // restart
state = MOVING_CWW;
}
break;

case STOPPED_CWW:
if (now - time_stopped >= 5000) {
digitalWrite(cw, HIGH); // restart
state = MOVING_CW;
}
break;
}
}

Brian, Welcome !
first step:

read how to use this forum

read about how to write a subject line.

come back, on the bottom right of your post is the more/modify button

step 2, fix the code by adding "code tags" as explained in step 7 of the link

you can EDIT your post and in front of your code, put a left bracket, followed by the word code, then a right bracket.
I cannot type those as the forum sees that and uses it to show the start of actual code.

at the end of your code, do the same left bracket, then /code the slash shows the end
follow that with a right bracket and save your edit.

OP's code from Reply #3 with a little tidying up

int sw1, sw2, sw3;
const int st6 = 6; //Start
const int lsr4 = 4; //Limit Switch Right
const int lsl12 = 12;  //Limit Switch Left
const int cw = 2; // To reversing relay 1
const int cww = 3;  // To reversing relay 2

void setup() {
 
        // put your setup code here, to run once:
    pinMode (st6, INPUT_PULLUP);
    pinMode (lsr4, INPUT_PULLUP);
    pinMode (lsl12, INPUT_PULLUP);
    pinMode (cw, OUTPUT);
    pinMode (cww, OUTPUT);
}

void loop() {
        // put your main code here, to run repeatedly:

    sw1 = digitalRead(st6);
    sw2 = digitalRead(lsr4);
    sw3 = digitalRead(lsl12);

    if (sw1 == LOW){
        do {
            digitalWrite(cw, HIGH);
            digitalWrite(cww, HIGH); 
        }while (sw1 = LOW);
    }

    if (sw2 == HIGH && sw3 == LOW){
        do {
            digitalWrite (cw, HIGH);
            digitalWrite (cww, LOW);

            sw2 = digitalRead(lsr4);
        }while (sw2 == HIGH);
     }

    else if (sw2 == LOW && sw3 == HIGH){
        do {
            digitalWrite (cw, LOW);
            digitalWrite (cww, HIGH);

            sw3 = digitalRead(lsl12);
        }while (sw3 == HIGH);
    }

    static enum {MOVING_CW, MOVING_CWW, STOPPED_CW, STOPPED_CWW} state;
    static unsigned long time_stopped;
    unsigned long now = millis();

    switch (state) {
        case MOVING_CW:
            if (digitalRead(lsr4) == LOW) {  // hit switch
                digitalWrite(cw, LOW);      // stop
                state = STOPPED_CW;
                time_stopped = now;
            }
            break;
        case MOVING_CWW:
            if (digitalRead(lsl12) == LOW) {  // hit switch
                digitalWrite(cww, LOW);     // stop
                state = STOPPED_CWW;
                time_stopped = now;
            }
            break;
        case STOPPED_CW:
            if (now - time_stopped >= 5000) {
                digitalWrite(cww, HIGH);    // restart
                state = MOVING_CWW;
            }
            break;

        case STOPPED_CWW:
            if (now - time_stopped >= 5000) {
                digitalWrite(cw, HIGH);     // restart
                state = MOVING_CW;
            }
            break;
    }
}

...R

}while (sw1 = LOW); == for comparisons

You need to get rid of the DO/WHILE in all of these

if (sw1 == LOW){
 do {
 digitalWrite(cw, HIGH);
 digitalWrite(cww, HIGH); 
 }while (sw1 = LOW);
 }

The IF should be sufficient and there is nothing within the DO/WHILE that updates the value of sw1 so the WHILE will never end.

In general don't use WHILE if you want a responsive program because the WHILE blocks the Arduino from doing other things until the WHILE completes

...R

Edit, I missed the incorrect = but it does not affect my advice

Thank you all for your support and comments.
The final sketch code for the "Model Train Auto Reversing" is:

/*

  • Auto-reversing model train engine using Arduino Nano and two limit switches.
  1. Switch one (ST1) start train moving to the right_3 (output 1 high, output 2 low)
  2. Train starts moving right_3
  3. Switch two (LSR2) Limit Switch right_3 trigger stops train (output 1 high, output 2 high)
  4. Delay 5 seconds,
  5. Then (output 1 low, output 2 high) starts train engine moving to the left_2.
  6. Switch three (LSL3) Limit Switch left_2 trigger stops train (output 1 high, output 2 high)
  7. Delay 5 seconds,
    Then (output 1 high, output 2 low), repeat step two

I will utilize an H-Bridge or two relays (for power reversing) to control the power to the track, due to the amperage required to power the engine.

*/

int sw1, sw2, sw3;
const int st6 = 6; //Start
const int lsr4 = 4; //Limit Switch right
const int lsl12 = 12; //Limit Switch left
const int left_2 = 2; // To reversing relay 1
const int right_3 = 3; // To reversing relay 2

void setup() {

// put your setup code here, to run once:
pinMode (st6, INPUT_PULLUP);
pinMode (lsr4, INPUT_PULLUP);
pinMode (lsl12, INPUT_PULLUP);

pinMode (left_2, OUTPUT);
pinMode (right_3, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:

sw1 = digitalRead(st6);
sw2 = digitalRead(lsr4);
sw3 = digitalRead(lsl12);

if (sw1 == LOW){
do {
digitalWrite(left_2, HIGH);
digitalWrite(right_3, LOW);

sw1 = digitalRead(st6);
}while (sw1 = LOW);
}

if (sw2 == HIGH && sw3 == LOW){
do {

digitalWrite (right_3, LOW);
delay (3000);
digitalWrite (left_2, HIGH);
sw3 = digitalRead(lsr4);
}while (sw3 == LOW);
}

else
if (sw2 == LOW && sw3 == HIGH){
do {
digitalWrite (left_2, LOW);
delay(3000);
digitalWrite (right_3, HIGH);
sw2 = digitalRead(lsl12);
}while (sw2 == LOW);

}

}

if (sw1 == LOW){
do {
digitalWrite(left_2, HIGH);
digitalWrite(right_3, LOW);

  sw1 = digitalRead(st6);
}while (sw1 = LOW);
}

Again, == for comparison, = for assignment.

Looks like a while loop would be more appropriate, if you can cope with its blocking nature.

Please remember to use code tags when posting code

DBRR_BrianK:
Thank you all for your support and comments.
The final sketch code for the "Model Train Auto Reversing" is:

Good to hear you have it working.

...R