Geoff Bunza geoffb

Model animation—bringing models to life—adds enormously to the interest and enjoyment of any layout or individual model! This edition covers one element for animating grade crossings—triggering and control using infrared detectors, servo motors, LEDs, and an Arduino Pro Mini for timing and control.

g%20Crew.JPG 

Geoff Bunza's Blog Index: https://mrhmag.com/blog/geoff-bunza
More Scale Model Animation videos at: https://www.youtube.com/user/DrGeoffB
Home page: http://www.scalemodelanimation.com

Reply 1
Geoff Bunza geoffb

Level Crossing or Grade Crossing Control

Here are the assumptions for this adventure:

There are 2 gates both servo motor controlled, four LEDs for warning flashers, and two infrared (IR) detectors positioned before and after the crossing.

vo_motor.jpg 

Servo Motor

Detector.jpg 

IR Detector

The servos can be found in many hobby stores and on the web, and are commonly used by Radio Control (RC) enthusiasts. The IR detector I use is a low cost ($1.37) module I found on ebay:  http://tinyurl.com/p3gvazj that has an adjustable sensitivity and whose output pin goes low when an object is sensed in front of it. It is powered by +5Volts. Paint ALL sides of both LEDs on the IR Sensor board black EXCEPT the very front of each to improve its sensitivity to ambient light.

Servos attach to Arduino Digital Pins 3 and 4, IR Detectors to Pins 5 and 6, LED flashers to 10 & 11, and 12 & 13 respectively. All powered by a 5 Volt power supply. The Arduino sketch is complete below. Copy, paste and load it into yours. The Blinkers are assumed to be LEDs with a common cathode (negative side) each LED with its own current limiting resistor (820-4700 Ohms is usually a good range). LEDs blink in pairs led1, led1 (pins 10,11) and led3 ,led4 (pins 12,13).

As a train is sensed by either sensor the flashers turn on immediately and continue for 3 seconds, when the gates come down—slowly. Flashing continues with the gates down until the other sensor is triggered and then remains untriggered for at least 1.2 seconds. Then the gates come up and the lights will go off. The train can arrive in either direction, but is must go completely through the crossing. If you need to deal with a train that enters,  stops, and backs up through the crossing this might give you a good starting point.

If you want to add some good looking flashers and gates, give this easy "kitbashing" project a try:

http://nycshs.files.wordpress.com/2013/08/nycm_2q2012.pdf

or

http://www.scalemodelanimation.com/Articles/Crossbucks_&_Crossing_Gates.pdf

‘Hope this helps enliven your crossings! [smile]   Have Fun!
Best Regards,
Geoff Bunza

Aduino Sketch for COMMON ANODE can be downloaded from here:      zip CrossingComAnode1.4.1.zip     

Aduino Sketch for COMMON CATHODE can be downloaded from here:  zip LevelCrossing1.4.zip     

Aduino Sketch for COMMON CATHODE :

// Example Level Crossing Control Driving Common Cathode LEDs
// Version 1.4  Geoff Bunza 2018
//
#include <Servo.h>
Servo gate1servo;  // create servo object to control crossing gate 1
                   //ASSUMPTION Gate is down at position 30 and up at position 120
Servo gate2servo;  // create servo object to control crossing gate 2
                   //ASSUMPTION Gate is down at position 30 and up at position 120
int sensor1 = 5;   // IR sensor pin Assumption== Pin goes LOW when train detected
int sensor2 = 6;   // IR sensor pin Assumption== Pin goes LOW when train detected
int led1 = 10;      // Led 1 pin first alternating flasher
int led2 = 11;      // Led 2 pin first alternating flasher
int led3 = 12;      // Led 3 pin second alternating flasher
int led4 = 13;     // Led 4 pin second alternating flasher
int gatelow = 30;        // variable to store the servo low gate stop position
int gatehigh = 120;    // variable to store the servo high gate stop position
int gateposition = 120;    // variable to store the servo gateposition
int entering_sensor = 5;       //this will tell which sensor was triggered first
int leaving_sensor = 6;        // this will tell which sensor shows train leaving
int gates_started = 0;         // this says if the crossing is active
int flash_state = 0;
long flash_time = 0;
long  flash_interval = 900;    // time in milliseconds between alternating flashes
int sensor_count = 0;
void setup()
{
  gate1servo.attach(3);  // attaches the servo on pin 3 to the servo object
  gate2servo.attach(4);  // attaches the servo on pin 4 to the servo object
  gate1servo.write(gateposition);  //start assuming no train
  gate2servo.write(gateposition);  //start assuming no train
  pinMode(sensor1, INPUT);  
  pinMode(sensor2, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);  
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  digitalWrite(led1, LOW);  // Start with all flashers off
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  flash_time = millis();
}
void loop()
{
  if ((digitalRead (sensor1)==LOW)&& (gates_started==0)) {
    gates_started = 1;
    leaving_sensor = sensor2;
    starting_sequence();
  }
  if ((digitalRead (sensor2)==LOW)&& (gates_started==0)) {
    gates_started = 1;
    leaving_sensor = sensor1;
    starting_sequence();
  }
  if (gates_started) flash_leds();   //gates are down continue flashing
  if ((digitalRead(leaving_sensor)==LOW)&&(gates_started==1)) {   //train is starting to leave
   
  //as long as the leaving sensor is active the train is still in the crossing
  while (gates_started==1)  {   //now check if train is REALLY gone
    sensor_count = 0;
    for (int i=1; i< 40; i++)  {
      if (digitalRead(leaving_sensor)==LOW) sensor_count++;
          delay (30);
                  flash_leds();
                  }
    if (sensor_count==0) gates_started=0;
    flash_leds();
    }
    // we only get here if the train has really left the crossing
                ending_sequence();
  }
}
void starting_sequence()  {
  long wait_time;
  flash_time = millis();
  wait_time = millis()+3000;
  while (wait_time> millis())  flash_leds();  //flash before dropping gates
  for(gateposition = gatehigh; gateposition> gatelow; gateposition-=1)  // goes from gatehigh degrees to gatelow degrees
  {                               
    gate1servo.write(gateposition);  // tell servo to go to gateposition in variable 'gateposition'
    gate2servo.write(gateposition);  // tell servo to go to gateposition in variable 'gateposition'
    flash_leds();                    // keep flashing leds
    delay(40);                       // waits 40ms to slow servo
  }
}
void ending_sequence()  {
    for(gateposition = gatelow; gateposition< gatehigh; gateposition++)   // goes from gatelow degrees to gatehigh degrees
  {                               
    gate1servo.write(gateposition);  // tell servo to go to gateposition in variable 'gateposition'
    gate2servo.write(gateposition);  // tell servo to go to gateposition in variable 'gateposition'
    flash_leds();                    // keep flashing leds
    delay(40);                       // waits 40ms to slow servo
  }
  digitalWrite(led1, LOW);  //  flashers completely off
  digitalWrite(led3, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led4, LOW); 
  delay(30000);             // 30 second delay to account for the train passing the starting entry sensor
}
void flash_leds()  {
  if (flash_time> millis()) return;
  flash_state = ~flash_state;
  digitalWrite(led1, flash_state);  // Alternate flashers
  digitalWrite(led3, flash_state);
  digitalWrite(led2, ~flash_state);
  digitalWrite(led4, ~flash_state);
  flash_time = millis()+flash_interval;
}

Geoff Bunza's Blog Index: https://mrhmag.com/blog/geoff-bunza
More Scale Model Animation videos at: https://www.youtube.com/user/DrGeoffB
Home page: http://www.scalemodelanimation.com

Reply 0
Dave K skiloff

Bell?

I assume it wouldn't be tough to add a bell sound?

Dave
Playing around in HO and N scale since 1976

Reply 0
Geoff Bunza geoffb

@Dave re: Bell?

Hi Dave,

Well, not tough at all to trigger-- anytime the flashers are on the bell would be on. To generate the sound you could use a PriCom sound board: http://www.pricom.com/Trains/DreamPlayerLITE.shtml

or a sound module like WTV020-SD-16P : http://www.ebay.com/itm/121121906573?ssPageName=STRK:MESINDXX:IT&_trksid=p3984.m1436.l2649

or a MP3 module like: http://www.ebay.com/itm/TF-Card-MP3-Voice-Module-Support-U-Disk-USB-for-Arduino-/281387900991?pt=LH_DefaultDomain_0&hash=item418406a03f

Have fun! Best regards,

Geoff Bunza

 

 

Geoff Bunza's Blog Index: https://mrhmag.com/blog/geoff-bunza
More Scale Model Animation videos at: https://www.youtube.com/user/DrGeoffB
Home page: http://www.scalemodelanimation.com

Reply 0
Dave K skiloff

Looks

like a fairly simple project for my first crack at using an Arduino.  Not that others haven't been easy, but I've been looking at buying a pre-built crossing that does the same thing.  This looks to be more fun.

If you had one on each side of the track, do you wire the lights and servos in series, or would you have two Arduino boards fed from the two IR detectors?  These may be basic questions, but I haven't jumped into any of this yet, but everytime I see one of your posts or articles, I think, "Man, I gotta start doing some of these things."

Dave
Playing around in HO and N scale since 1976

Reply 0
Geoff Bunza geoffb

@Dave re: Setup

Hi Dave,

The way this is set up, it assumes 2 gates, one on each side of the track, each powered by its own servo, and 2 crossing flashers (2 LEDs per flasher) one on each side of the track, each LED has its own pin to connect to.

Have at it!

Best Regards,

Geoff Bunza

 

Geoff Bunza's Blog Index: https://mrhmag.com/blog/geoff-bunza
More Scale Model Animation videos at: https://www.youtube.com/user/DrGeoffB
Home page: http://www.scalemodelanimation.com

Reply 0
Dave K skiloff

Ah

I see now.  I should have looked at the code more closely to understand.  Just reading your description, I interpreted it as two LEDs with positive and negative for power, one on 10 and 11 and the other on 12 and 13.  I think I get it.  Might go online shopping tonight and have a project I can do in camp after doing a bit of soldering at home.

Dave
Playing around in HO and N scale since 1976

Reply 0
Prof_Klyzlr

The bells, the bells....

Dear Dr Geoff, Dave,

Agreed, a DP LITE would be perfect for CD-spec crossing-bells.

However, it should be noted that the typical crossing bell is actually 2x bells ringing at dis-similar speeds to give a "phased" result. As such, a single "ding" played repeatedly would be cute, but "wouldn't sound right", even if the single "ding" was a perfectly-scaled representation of a prototype crossing bell...

The suggested solution would be:
- set the Arduino to turn an Output pin ON for as long as the bell is required.
- set the DP-LITE to "loop-play while Triggered"
(IE no matter how long the WAV file audio is, it will loop-play said-file for as long as the DP LITE Trigger IN pin is held ON by the Arduino Output pin)

now, the bit that strikes terror into the hearts of modellers everywhere...

- need to create a "loopable" WAV file
(IE a file which sonically can play Start> Finish> Start> Finish> Start> ... etc etc without obvious "click", stutter, hesitation, or other tell-tale at the joint/loop-point)

which has at-least 1x complete "phase shift" cycle of both bells.

Indeed, the WAV file need not be soo short, the DP LITE can handle hours of CD-spec WAV audio,
and thus play a bell which smoothly sonically "loops" 
(IE without actually needing to re-start playing the file again),

long after most any practical-length model train has rolled-on-by,...
(and even handle the "train gone into emergency" which has stopped and blocked the crossing for an hour or 2!!! ).

Dr Geoff, I'd ask, if one built the circuit for just "Lights and sound" and omitted the servos/gates,
would the code need to be modified, or would the Arduino simply "not care" and do-it's-thing regardless?
(I'm thinking of modern-era overhead grade-crossing flashers with no gates...)

Happy Modelling,
Aim to Improve,
Prof Klyzlr

Reply 0
Geoff Bunza geoffb

@Prof K

Hi John!

Quote:

if one built the circuit for just "Lights and sound" and omitted the servos/gates,
would the code need to be modified, or would the Arduino simply "not care" and do-it's-thing regardless?

You could either leave the servo code in as is, or delete the appropriate references to servo1 and servo2. One could leave it untouched without any headaches.

By the way, as long as the audio segment was edited for "sounds right" repetition, does the double bell issue matter?

Also, as a point of reference, there are many North American crossings with only one physical bell and one clanger.

 

Best regards,

Geoff Bunza

 

Geoff Bunza's Blog Index: https://mrhmag.com/blog/geoff-bunza
More Scale Model Animation videos at: https://www.youtube.com/user/DrGeoffB
Home page: http://www.scalemodelanimation.com

Reply 0
Dave K skiloff

Alrighty

I spent a bit of time on ebay, linking to where you pointed me and ordered up a few Arduino Pro Mini boards, the USB cable, a few IR sensors and a couple other gizmos to play around with.  I was going to order the DreamPlayer Lite, but the only shipping option essentially doubled the cost of the DP ($36).  I'll have to rethink that or try and find a few others to order at the same time and drop the cost.

Just need to find the servos now and order them and I'm good to go.

Dave
Playing around in HO and N scale since 1976

Reply 0
Prof_Klyzlr

Twin bells

Dear Dr Geoff,

I knew I'd heard twin-bells before in "contemporary" (2010-2014) era switching...

Listen thru the playlist,
(you don't have to watch if contemporary era switching isn't your thing,
it's the sound that we're listening for...),

most of the clips have at least one scene with an "non-electronic bell" grade-crossing featuring dual bells...


(This one interesting, first scene has 2x analog bells, 2nd scene appears to have mixed analog and electronic bells!?!?!?)

Happy Modelling,
Aim to Improve,
Prof Klyzlr

 

Reply 0
Dave K skiloff

Well

I found the DP on Tony's Trains and the shipping was estimated at $13, so I ordered it.  Looking forward to getting into this!  Thanks so much for your inspiration, Dr. Geoff.

Dave
Playing around in HO and N scale since 1976

Reply 0
Geoff Bunza geoffb

@Prof K re: Twin Bells

Hi John,

In at least the 2 videos with the Gen Sets you can hear the sounds of bells beating against one another. Many crossing signals were erected equipped with lights and bells as new, hence the twin bells evident in the videos, usually found on each side of the tracks. If damaged and not at a well traversed crossing some bells did not get repaired or replaced. One can also find more than 2 bells evident at a complex crossing with more streets intersecting at the crossing. We have had a crossing near me "updated in the last two years, now equipped with one electronic, synthesized bell, which while quite loud, does not have as pleasing a sound as the mechanical equivalents.

One might be able to judge timeframes of certain railroad videos based on the sights and sounds of an individual crossing.

Best regards,

Geoff Bunza

 

Geoff Bunza's Blog Index: https://mrhmag.com/blog/geoff-bunza
More Scale Model Animation videos at: https://www.youtube.com/user/DrGeoffB
Home page: http://www.scalemodelanimation.com

Reply 0
Kevin Rowbotham

Excellent as always!

A ton of good information here as usual.

Thanks Dr. Geoff for another great animation project.

 

Skiloff,

Check out your old disk drives etc. as a source of neat servo or stepper motors to complement your Arduino animation projects.

I got my Arduino and some nice servos etc. through Adafruit.

~Kevin

Appreciating Modeling In All Scales but majoring in HO!

Not everybody likes me, luckily not everybody matters.

Reply 0
casenundra

I noticed

the gate did not drop in the last video. Are gates being phased out in favor of traffic lights?

Rich S.

Home of the Here N There RR (N) (under construction)

One of these days I'll be able to run some trains!

Now on Facebook for whatever that's worth.

Reply 0
ILHO

simple sound hack

An inexpensive sound card can be hacked to work - use the arduino 5V to power and a pin to send it a button-pressed signal , there are also 5 button models - but they would take a pin each to activate the different sounds.

VDD is 5V and VSS is ground.  The second from left is the wire to hook to an Arduino pin and send a high to 'press' the button- Very short- send, delay(1), then stop sending

Link :

http://www.invitebyvoice.com/index.php/recordable-musical-sound-chip-module.html

 

BTW and also - how do you insert videos into the blog so they can be linked to UTube ? Yours can be right clicked to go to UTube, mine and others are not ... I do not (and refuse to) have a Flash player- so all i see is a blank screen and a link to get a flash player.

Thanks for another great project !

Ivan

 

Ivan Lee Herring

Mountain Valley Western

Reply 0
Geoff Bunza geoffb

@Ivan Sound Module and Video Insertion

Hi Ivan,

I'm glad you enjoyed the project. I've tried sound modules like you listed. They will work as you described. I stopped using them as the other alternatives produce better sound to my ears. I don't know whether it's the way they record or the limitations of their playback-- probably both. I used to extract the sound message modules from greeting cards which are similar to these as well. If you are an audio purist (which I'm not but can appreciate such) it's hard to beat the Pricom modules-- they are simply impressive for great sound, which also means you'll need great recordings too.

Start with what you can afford, understand, and install... and learn from there! 

All my videos are first loaded to Youtube. I have a "channel" here: https://www.youtube.com/user/DrGeoffB

You can set one up for free. Then upload your video to your youtube site. When you play the video, underneath the video window, you will see:  + Add To   < Share  ... More

Click on Share and then "Embed" appears underneath the word Share. Click on Embed

Right click on the highlighted line and copy the Embed code.

Then go back to the MRH comment editor and click on the Filmstrip on the top right menu bar next to the circled f

Paste the embed code you copied into the window that appears and click OK.

That;s it! The playable video window should appear next in line in your comment.

Thanks for the sound module info Ivan! Have Fun!

Best regards,

Geoff Bunza

 

Geoff Bunza's Blog Index: https://mrhmag.com/blog/geoff-bunza
More Scale Model Animation videos at: https://www.youtube.com/user/DrGeoffB
Home page: http://www.scalemodelanimation.com

Reply 0
Prof_Klyzlr

So we have playback hardware, what sound are you using?

Dear Cross-gate curious,

Morbid curiousity prompts me to ask the rank-n-file,
the thread has already thrown up some do-able "sound module" options to add onto Dr Geoff's fantastic work,

But (you knew that was coming ),

once you have your audio playback hardware all wired up,

what Sound are you going to play thru it
(None of the units mentioned, with the exception of the 8bit 8kHz ISD-based ITTC module come "preloaded" with sound,
and if you have a particular crossing sound "in your mind's ear" the ITTC mod likely won't cut-it)

and where/how are you going to source said-sound?

Just had to ask...

Happy Modelling,
Aim to Improve,
Prof Klyzlr

http://www.freesound.org/
http://www.sounddogs.com/

Reply 0
Dave K skiloff

Been kicking that around, Prof

Haven't come up with any solutions just yet, but haven't really gone looking, either.  We have a regular fire alarm test coming up at work, that I thought I might try and record, but not sure how well that would work.

Dave
Playing around in HO and N scale since 1976

Reply 0
Kevin Rowbotham

Sound Bytes

Myself, being a CP Rail fan, I would probably just "suck" the audio out of

, throw it into an audio editor, edit out the train, make a loop of the bells, clean up any noise, encode to mp3 and load on the player.  But then, I am a hacker at heart.

Alternately, I would visit a rail crossing with my video camera.  I know of two places near yards where I could record crossing gates if I really needed to.

I think this would work, not?

~Kevin

Appreciating Modeling In All Scales but majoring in HO!

Not everybody likes me, luckily not everybody matters.

Reply 0
Dave K skiloff

Tough

to isolate the bell with a roaring engine in the background blowing a horn long, long, short, long.  I guess it depends on how picky you are, really.  I would like a clean sound with as little background noise as possible.

Dave
Playing around in HO and N scale since 1976

Reply 0
Kevin Rowbotham

Not so tough...

Quote:

to isolate the bell with a roaring engine in the background blowing a horn long, long, short, long.  I guess it depends on how picky you are, really.  I would like a clean sound with as little background noise as possible.

Dave

Well, I am no audio pro but...if you listen to the first twenty seconds of the clip, all I hear is a little wind noise in one spot.  Twenty seconds of bell ringing is far more than I think I would need to make a loop that would sound just fine on my crossing.

Edit:  Ok.  It took me about 5 minutes including the time to strip the audio out of the video and download it.  I now have a clip that should loop OK that is two minutes and nineteen seconds long that I would be more than happy with.  Maybe not good enough for the rivet inspectors but good enough for me.  Now if I knew how to test to see if it will loop seamlessly...?  I don't have a player on hand.

It's available here if anyone wants to listen to it.

~Kevin

Appreciating Modeling In All Scales but majoring in HO!

Not everybody likes me, luckily not everybody matters.

Reply 0
barr_ceo

Audacity is your friend...

And a free, open source program that turns your computer into a recording studio. You can do everything from this little project to full fledged studio recording and overdubbing. Mac, Linux... and even Windows.

http://audacity.sourceforge.net/

It'll play them, too.

 

Reply 0
Dave K skiloff

Hehe

Never actually listened to your clip, I as referring to the area where I know a bell is close by (which I think you've photographed before).  Not sure what you are using for editing, but you should be able to loop with most audio editors.  Audacity is a free editor that can do that quite easily.  Just copy the clip and put it at the end and tighten it up if it isn't seamless.

Dave
Playing around in HO and N scale since 1976

Reply 0
Dave K skiloff

OK, watched the video

Not the bell sound I'm looking for, that one sounds electronic.  My recollection (that could be completely wrong), is a much quicker "ding" rate with a manual bell.

Dave
Playing around in HO and N scale since 1976

Reply 0
Reply