Model railroad crossing gates

Ok
I'm a novice at writing code and I don't have the vocabulary for this

However I know what I want my Arduino Uno to do

I am a model railroader and I want it my train to pass over a sensor and turn something on hold it on after it passes until it crosses a second one to turn it off

And I'm getting frustrated I don't know how to do it

I not sure I know how to hook it up but the code has me stumped

please be patient i have never programed anything in my life

Please HELP
John

Any help would be greatly appreciated!

Thanks in advance.

Hello johnd1024

Welcome to the worldbest Arduino forum ever.

This is a nice project to get started.

Keep it simple and stupid firstly.

Divide et impera

Follow the example code that comes with the library or
run some tutorials for the hardware selected.
If you are happy with the results of the tutorials you can merge these to your project.

Have a nice day and enjoy coding in C++.

I moved your topic to an appropriate forum category @johnd1024.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Please list what hardware you’re planning to use…

I’m guessing a small R/C servo, an AVR based Arduino (Uno, nano etc), and whichever sensors you choose..

Please list parts and links to the web pages for your products.

When You have got it working maybe You want to make it like on the real railroad?
Real gates go on in good time before the train enrers the crossing, and they go up when last part of the train has left the crossing,

I wrote some code for a model railroad crossing lights, maybe you can adapt the code for this. Wouldn't be hard, all the triggers are setup already, it's just a matter of what the triggers trigger.

const int IRPin1 = 2;
const int IRPin2 = 3;
const int LED1 = 10;
const int LED2 = 11;
const int Output = 7;

int train = 0;

unsigned long previousMillis = 0;
unsigned long prevMillis = 0;

const long interval = 7000;

boolean ledState = false;

void setup() {
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(Output, OUTPUT);
  pinMode(IRPin1, INPUT);
  pinMode(IRPin2, INPUT);
  digitalWrite (Output, HIGH);
}

void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= 500) {
    previousMillis = currentMillis;

    ledState = !ledState;
    digitalWrite(LED1, ledState);
    digitalWrite(LED2, !ledState);
  }
  if ((!digitalRead(IRPin1) == 1) || (!digitalRead(IRPin2) == 1)) {
    train = 1;
  }
  if (train == 1) {
    digitalWrite (Output, LOW);
    unsigned long currMillis = millis();
    if (currMillis - prevMillis >= interval) {
      prevMillis = currMillis;
      digitalWrite (Output, HIGH);
      train = 0;
    }
  }
  if (train == 0) {
    digitalWrite (Output, HIGH);
  }
}

I don't think that is the final code iteration, but I seem to have lost the final one in the depths of my many hard drives. But that should give you a good start!

Dr. Geoff Bunza has done an excellent series of model Railroad-related Arduino projects. They're published on the Model Railroad Hobbyist Forum. Here's a link to the index he put together of his projects. You can ask about specifics here, but you can also approach him directly by joining that forum and posting to a particular thread, or actually direct message him.
Not trying to chase you away, but Happy Reading, and good luck!

(sorry, got distracted reading, forgot to insert link!)

There is no obvious link.
Sorry, I tried to find it but couldn't.

Thank you....I'll leave this post here for future readers.

1 Like

Will this idea work? Pseudocode:

    If train triggers sensor1
         Turn on whatever

    If train triggers sensor2
        Turn off whatever

Right there in your loop.

a7

Practice your train code on Wokwi... (here's a pedestrian-facing simulation, not train-facing)

Thank you all so very very much for responding with your help and suggestions.

I found a you tube video entitled "Crossing gate Project for Arduino" and through that was able to understand the code and get the result i needed.

I also understand now through all you help in the forum to activate another pin.

The code seems to be simple and easy if you just takr the time to read it and stay with it even for a novice such as me.

like i said at the beginning this is the first time i have ever tried anything like this, this is so far out of my wheelhouse i never thought i would get it.

actually I'm having more trouble navigating thorough this forum than writing code. . .LOL. but I will get it!
Thank you all again for your support, help and patience.

John.

4 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.