Page 2 of 10 FirstFirst 123456 ... LastLast
Results 21 to 40 of 197

Thread: The Arduino & Model Railroading

  1. #21
    Join Date
    Dec 2009
    Location
    Lexington, KY
    Posts
    16,788
    Blog Entries
    16
    Thanks
    4,630
    Thanked 12,866 Times in 5,375 Posts
    Mentioned
    231 Post(s)
    Tagged
    0 Thread(s)

    Default

    Quote Originally Posted by dougwood View Post
    Yes, that sounds good. Stated a little differently, if you assume that your rolling stock all have about the same length, and assume that the train does not change speed much as it goes by the detector, I would keep a running average of the time span between the detector going from off to on with every car that passes by the detector. Then if you see a time pass where the detector does not go back on again for a period of time that is that average length of time times say a factor of 2 or 1.5--then you must have just seen the end of the train pass by.

    Next I think you are going to have to get one of the ultrasonic detectors and measure the speed of the train directly. :-)
    I like this idea of comparing the car gaps to the average car length.

    There's a fundamental flow in IR detection that will have to be addressed. What do you do when you stop the train with the detector peeking between two cars? The train is there, but the detector can't see it.
    Never mistake a guy who talks a lot for a guy who has something to say...

    CH&FR Site and Blog: http://www.chfrrailroad.net and http://blog.chfrrailroad.net
    Appalachian Railroad Technology: http://www.apprailtech.com


  2. #22
    Join Date
    Jul 2012
    Location
    TX
    Posts
    1,402
    Thanks
    715
    Thanked 3,286 Times in 771 Posts
    Mentioned
    20 Post(s)
    Tagged
    0 Thread(s)

    Default

    Quote Originally Posted by TwinDad View Post
    There's a fundamental flow in IR detection that will have to be addressed. What do you do when you stop the train with the detector peeking between two cars? The train is there, but the detector can't see it.
    I just wrote up a long response, and thinking about it, I deleted the whole mess.

    I think I know how to solve this very easily.

    Let me take a nap and think about it, but if anyone else knows where I'm going please don't wait on me to answer this.

    Hint: The way I'm going to line-up the sensors, there won't be any gaps. Come to think of it, it does away with the whole timing problem!

  3. The Following User Says Thank You to Nskale For This Useful Post:


  4. #23
    Join Date
    Jan 2013
    Posts
    49
    Thanks
    35
    Thanked 18 Times in 10 Posts
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Good idea! Can you say diagonally? (if you are using beam interruption not reflection)

  5. #24
    Join Date
    Mar 2011
    Location
    High Point, NC, USA
    Posts
    328
    Thanks
    460
    Thanked 212 Times in 124 Posts
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Default

    Quote Originally Posted by dougwood View Post
    Good idea! Can you say diagonally? (if you are using beam interruption not reflection)
    Try an optical range finder on an angle like this:
    http://www.pololu.com/catalog/product/1134
    HAROLDN212

  6. #25
    Join Date
    Jan 2005
    Location
    Antelope, Calif USA
    Posts
    4,722
    Blog Entries
    6
    Thanks
    1,841
    Thanked 2,464 Times in 1,257 Posts
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)

    Default

    If you used two detectors and spaced them appropriatly, it would be hard to stop a train with both not seeing something.
    Sean McC

    "No man is a failure ...

    who has friends." -- Clarence

  7. #26
    Join Date
    Jul 2012
    Location
    TX
    Posts
    1,402
    Thanks
    715
    Thanked 3,286 Times in 771 Posts
    Mentioned
    20 Post(s)
    Tagged
    0 Thread(s)

    Default

    Quote Originally Posted by dougwood View Post
    Good idea! Can you say diagonally? (if you are using beam interruption not reflection)
    We have a winner!

    That's exactly right doug. After thinking about it, I haven't yet thought of a downside. Looking at the train in front of me, I can move my ruler at an angle greater than would be required to have no gaps, and still have the range I need, which is at 1.5" on the test bench. All I have to do is use the worst case car - like a flatcar, and make sure the TX is in line vertically with the flat car structure, and then pointed at an angle to the RX.

    But it gets better. Thinking about it, I don't need it to be real-time, as in what a microcontroller can do. I don't need to define real-time as 1 mS, 10 mS, or even 100 mS. I don't need the loop to run as fast as it can. The real time I need is only something like 0.5 sec or even 1 sec (or even longer). I mean this is a train - not a jet or rocket ship. Why does that get me so excited? Because it lets me low-pass filter the train event. What that means is that even after the careful setup of an angled beam, so that no gaps are seen, let's say a sliver of a beam gets through here and there. If I only take samples every 1 S or so, I'll very probably never even see the slivers of beam that might get through.

    Instead of this being a hard problem, it's now, I think, pretty simple.

    So here's the current Arduino code. I use a red LED for a train indication, and a green one for no train (green LED not really necessary but I like LEDs). On the bench, simulating the train with my finger, it works, even if I simulate a flicker of a gap (I have a scope hooked up and I can see the flickers). The circuit I'm using for the IR LEDs is shown in the attachment, except currenty I'm using a 180 ohm for the TX LED and a 120k for the RX LED. I'll try to get it on the layout tomorrow.

    int irtx = 11; // digital pin 11
    int irrx = A0; // analog pin A0
    int active_train = 2; //digital pin 2, red LED
    int no_train = 3; // digital pin 3, green LED
    float vin=0;

    void setup()
    {
    pinMode(irtx, OUTPUT);
    pinMode(no_train, OUTPUT);
    pinMode(active_train, OUTPUT);
    digitalWrite(irtx,HIGH); //turn on IR LED
    }

    void loop()
    {
    vin = .0049 * analogRead(A0); // get phototransistor voltage and convert 0-1024 to voltage
    delay(10); // a little settling time
    if (vin > 4.3) // train detection if greater than 4.3V
    {
    digitalWrite(active_train,HIGH);
    digitalWrite(no_train,LOW);
    }
    else // no train is present
    {
    digitalWrite(active_train,LOW);
    digitalWrite(no_train,HIGH);
    }
    delay(1000); // 1 sec delay - slow it all down - it's only a train!
    }

    Attached Images Attached Images

  8. The Following 3 Users Say Thank You to Nskale For This Useful Post:


  9. #27
    Join Date
    Mar 2011
    Location
    High Point, NC, USA
    Posts
    328
    Thanks
    460
    Thanked 212 Times in 124 Posts
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Default

    Quote Originally Posted by seanm View Post
    If you used two detectors and spaced them appropriatly, it would be hard to stop a train with both not seeing something.
    That is true but your program becomes more complicated. It has been my experience to try to keep it as simple as possible because multitasking requires good programming skills that I do not posses.
    HAROLDN212

  10. #28
    Join Date
    Feb 2013
    Location
    Sequim, WA
    Posts
    2,784
    Blog Entries
    1
    Thanks
    9,447
    Thanked 3,973 Times in 1,617 Posts
    Mentioned
    84 Post(s)
    Tagged
    0 Thread(s)

    Default

    Is there room on this site for another forum dedicated to the Arduino and N scale?

    Oh, and on my OO scale layout, I'll be using a microcontroller to run the semaphore signals. Nice thing is that Tam Valley Depot (the supplier, and no connection to me) has taken care of the programming side.

  11. #29
    Join Date
    Dec 2003
    Location
    Columbus,OH, USA
    Posts
    3,260
    Thanks
    79
    Thanked 1,571 Times in 912 Posts
    Mentioned
    44 Post(s)
    Tagged
    0 Thread(s)

    Default

    Quote Originally Posted by Nskale View Post
    It's C programming for anyone who wants to know. They call the programs "sketches", which I don't like. Why they came up with that name is a mystery. Maybe it was so as to not scare off new beginners - I don't know. But if you start off slowly C isn't so bad.
    The Arduino language is actually based on Processing and Wiring. The sketch terminology comes from the Processing, and it has to do with the fact that both languages are intended for use by artists and designers first.

    On the technical side, the Arduino environment is a wrappers around the GNU C++ compiler.

    As for the language, while much of what you do is the same as C or C++ or Java, they do have some syntax that differs from C++ ( import instead of include, different printing mechanisims, etc ).

    All that said I am actually incorporating Arduino hardware into some of my C programming courses. They are great learning tools for introducing programming to students, and they are great tools for adding interesting projects to some upper division courses.

    And then there are the model railroading related projects. My personal favorite is still my talking hotbox detector ( see
    http://nscaleintermodal.com/Arduino/...tor/index.html ).

    Paul

  12. #30
    Join Date
    Apr 2007
    Location
    Christiana, TN
    Posts
    603
    Thanks
    4
    Thanked 400 Times in 257 Posts
    Mentioned
    22 Post(s)
    Tagged
    0 Thread(s)

    Default

    I have a loop of track mounted on a loop of 3/8" plywood that for the past few years I have placed in the Christmas tree(not on the floor around it, but in it) each year. I also setup an HO scale loop on the floor. With a "motor shield", an Arduino can directly drive a DC powered locomotive, actually two, because the motor shield has two sets of outputs.

    Here's what I did: I used an Arduino with a motor shield, an infrared sensor, a universal remote, a PIR motion sensor and a few LEDs. One output from the motor shield went to the loop of N scale track in the tree and the other output went to the HO scale track on the floor. I setup two operating modes, manual and automatic, with the LEDs indicating the mode. The Arduino can read the remote commands using the infrared sensor and you could select the mode using the number buttons on the remote. In the manual mode you could control the trains with the remote's volume and channel buttons. In the automatic mode, the Arduino used the motion detector to know when somebody walked by the tree and would run the trains for a preset amount of time - one would ramp up slowly to speed, then the other one would, then they would slow down and stop in sequence.

    Next year I might try it a little differently: make it a DCC output and use sound equipped locos. If I do that, I'll have the automatic mode ring the bell and blow the horn as well.

  13. The Following User Says Thank You to CSXRobert For This Useful Post:


  14. #31
    Join Date
    Jul 2012
    Location
    TX
    Posts
    1,402
    Thanks
    715
    Thanked 3,286 Times in 771 Posts
    Mentioned
    20 Post(s)
    Tagged
    0 Thread(s)

    Default

    Quote Originally Posted by pbender View Post
    The Arduino language is actually based on Processing and Wiring. The sketch terminology comes from the Processing, and it has to do with the fact that both languages are intended for use by artists and designers first.
    Have you ever used LabView by National Instruments? I used that for years in my career. Now that's a sketch!

    All that said I am actually incorporating Arduino hardware into some of my C programming courses. They are great learning tools for introducing programming to students, and they are great tools for adding interesting projects to some upper division courses.
    Do you ever read the Arduino forum? It's funny sometimes, because you can practically pick out the students who come there wanting help (well, they really seem to want a complete solution) to their assignments. I wish some of them would try to solve the problem first, but it's way to easy to get the work done by others these days.

    And then there are the model railroading related projects. My personal favorite is still my talking hotbox detector ( see
    http://nscaleintermodal.com/Arduino/...tor/index.html ).
    Yea that's nice. You could also add sounds at a RR crossing when the train passes.

    I'm going to set up a test on my layout today. If it works I'll show people not familiar with the Arduino where in the code to add the actions they would want to do when a train is detected. it could be driving a servo, a motor, LEDs, anything. Maybe it will inspire someone to get one.

    Quote Originally Posted by CSXRobert View Post
    I have a loop of track mounted on a loop of 3/8" plywood that for the past few years I have placed in the Christmas tree(not on the floor around it, but in it) each year. I also setup an HO scale loop on the floor. With a "motor shield", an Arduino can directly drive a DC powered locomotive, actually two, because the motor shield has two sets of outputs.

    Here's what I did: I used an Arduino with a motor shield, an infrared sensor, a universal remote, a PIR motion sensor and a few LEDs ...
    That's very cool! There's so much that's possible it's unreal.

  15. The Following User Says Thank You to Nskale For This Useful Post:


  16. #32
    Join Date
    Dec 2003
    Location
    Columbus,OH, USA
    Posts
    3,260
    Thanks
    79
    Thanked 1,571 Times in 912 Posts
    Mentioned
    44 Post(s)
    Tagged
    0 Thread(s)

    Default

    Quote Originally Posted by Nskale View Post
    Have you ever used LabView by National Instruments? I used that for years in my career. Now that's a sketch!
    Maybe once or twice.

    Do you ever read the Arduino forum? It's funny sometimes, because you can practically pick out the students who come there wanting help (well, they really seem to want a complete solution) to their assignments. I wish some of them would try to solve the problem first, but it's way to easy to get the work done by others these days.
    True. But this is also true of any other environment we use. I once caught a student posting one of my assignments on a forum. He recieved zero responses.

    It is also easy to tell when someone cheats like that. They usually cannot explain what the program does to you.


    Yea that's nice. You could also add sounds at a RR crossing when the train passes.
    The voice box shield is overkill for that. You can generate bell like sounds in other ways ( even a piezo-electric buzzer works ).

    Paul

  17. The Following User Says Thank You to pbender For This Useful Post:


  18. #33
    Join Date
    Mar 2011
    Location
    Windsor, Ontario, Canada
    Posts
    1,083
    Thanks
    1,663
    Thanked 2,372 Times in 603 Posts
    Mentioned
    19 Post(s)
    Tagged
    0 Thread(s)

    Default

    Quote Originally Posted by Nskale View Post
    I wasn't sure if anyone would be interested in this at all, but it seems so. We should keep it going with out experiments related to Model Railroads.



    As Doug alluded to, yes. But, it's not simply the circuitry that can do it - it's the software you write. That's what gives it such versatility - the hardware combined with the software. I suppose all the logic could be done with hardware but it sure would be a mess.

    Yes I've already thought about the gaps between the cars. I'll do some experiments over the next few days and post the code I come up with. Basically, the program will have to have a limit set so it knows how long the time gaps between cars can be. The detector will go on and off as the cars pass. Since you "theoretically" could run a train at an arbitrarily low speed (the gaps between the cars could be 1 - 2 seconds at the lowest possible speed), this time will have to be a realistic compromise representative of the real way the trains run. So, if the time between the last detection and the most recent detection is longer than (some experimental time) then the current train has passed. So you keep measuring times between detection events as the detector goes on and off, pretty soon the time will be larger than some pre-determined value: the train has passed.

    But I'm open to other ideas. This is all going to be open source.

    I'll set up the detectors and see what I can come up with.
    To avoid the arbitrary speed, use two sensors, spaced 3-5cm (1-2inch) apart. Detect for presence in front of each. If both are detecting a presence, you have a engine or car. If only one is clear you're dealing with a gap, or beginning/end of train. If both are clear, you have seen the end of the train (ie. track is clear). The logic in your code gets slightly more complex, but the detection performance improvements is worth it.


    EDIT: Oops, didn't see SEANM's response...
    Peter

    Layout Depot (share your designs with others): www.LayoutDepot.com
    My Build Thread: www.nscale.net/forums/showthread.php?28081-Green-Valley-Railway

  19. #34
    Join Date
    Jul 2012
    Location
    TX
    Posts
    1,402
    Thanks
    715
    Thanked 3,286 Times in 771 Posts
    Mentioned
    20 Post(s)
    Tagged
    0 Thread(s)

    Default

    Quote Originally Posted by pbechard View Post
    To avoid the arbitrary speed, use two sensors, spaced 3-5cm (1-2inch) apart. Detect for presence in front of each. If both are detecting a presence, ...
    Hi pbechard - don't need to do all that now.

    I came up with a much simpler and better way. Testing is in progress as we speak. Stay tuned for a progress report ...

  20. #35
    Join Date
    Dec 2009
    Location
    Lexington, KY
    Posts
    16,788
    Blog Entries
    16
    Thanks
    4,630
    Thanked 12,866 Times in 5,375 Posts
    Mentioned
    231 Post(s)
    Tagged
    0 Thread(s)

    Default

    Quote Originally Posted by Paul Schmidt View Post
    Is there room on this site for another forum dedicated to the Arduino and N scale?
    I don't know that there's a need for an Arduino-specific forum. The Software forum is fairly quiet, and would be a good place to host most of the discussions related to programming in general, while the existing forums on train control, signlaing and so on are topical enough to hold the applications.

    I think, for example, a thread on how to use an Arduino to do train detection will get more visibility by being in the "signaling and detection" forum, than in an "Arduino" forum.

    One could always TAG threads about Arduino to make them easier to associate...
    Never mistake a guy who talks a lot for a guy who has something to say...

    CH&FR Site and Blog: http://www.chfrrailroad.net and http://blog.chfrrailroad.net
    Appalachian Railroad Technology: http://www.apprailtech.com


  21. #36
    Join Date
    Jul 2012
    Location
    TX
    Posts
    1,402
    Thanks
    715
    Thanked 3,286 Times in 771 Posts
    Mentioned
    20 Post(s)
    Tagged
    0 Thread(s)

    Default

    While I was testing just now I realized yet another way to make it work, even without angling the detectors (I think an angle works better though).

    Anyone know where I'm going with this alternative alignment?

    I will have an update in a few hours.

  22. #37
    Join Date
    May 2005
    Location
    Southern Colorado
    Posts
    3,006
    Blog Entries
    8
    Thanks
    1,263
    Thanked 1,875 Times in 890 Posts
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)

    Default

    Quote Originally Posted by Nskale View Post
    While I was testing just now I realized yet another way to make it work, even without angling the detectors (I think an angle works better though).

    Anyone know where I'm going with this alternative alignment?

    I will have an update in a few hours.
    So this is a point sensor then, right? You'll need IR detectors at each end of the block, yes?
    "Do Not Hump!?!?! Does that mean what I think it means?!?"--Michelle Blanchard

    "People saw wood and say nothing, but railroad men saw trains and say things that are better left unprinted."--Charles De Lano Hine

    Down with UP

  23. #38
    Join Date
    Jul 2012
    Location
    TX
    Posts
    1,402
    Thanks
    715
    Thanked 3,286 Times in 771 Posts
    Mentioned
    20 Post(s)
    Tagged
    0 Thread(s)

    Default

    Quote Originally Posted by ranulf View Post
    So this is a point sensor then, right?
    Yes.

    You'll need IR detectors at each end of the block, yes?
    At each end of what block?

  24. #39
    Join Date
    Jul 2012
    Location
    TX
    Posts
    1,402
    Thanks
    715
    Thanked 3,286 Times in 771 Posts
    Mentioned
    20 Post(s)
    Tagged
    0 Thread(s)

    Default

    I set up the test on the layout (see attachment). I set up a detector angle so that it would let a small sliver of light through here and there. I didn't have to do that - I could have blocked all the light between cars by increasing the angle, but I wanted to see what would happen. I used an empty flat car to set the height of the LED beam. I still had the loop delay at 1 sec.

    It worked just as I had hoped. If I ran the train through at a very low speed, it allowed the slivers of light at the few points between cars that I allowed on purpose, but the detector system still kept the "train present" condition just like it should have. If I stopped at just the right place with just the right two cars, it would untrigger, but the chance of that happening would have been very small. But as I said, I was only testing a mis-alignment. I can remove all chances of false untriggering by just making the angle larger.

    So, although I would have enjoyed solving the harder problem, it wasn't necessary.

    I commented in the code where you would want to put an event if you used this code on your layout. You could move a servo, a motor, light more LEDs, make a sound, whatever you want. You'd just have to add those physical objects/circuits.

    Here's the code -

    /* This is a simple generic IR sensing application shell.
    //
    // It uses a commonly available IR LED emmitter and IR sensing phototransistor.
    // Available many places such as here http://www.led-switch.com/I.%20R.%20LED.htm
    // Place the sensors at an angle (not 90 deg) to the passing train on either side of the tracks.
    // The angle eliminates any chance of false triggers due to gaps between cars.
    // To use this to trigger another device add the device hardware and code to the Arduino.
    //
    */

    int irtx = 11; // digital pin 11 driving the IR LED
    int irrx = A0; // analog pin A0 connected to the phototransistor
    int active_train = 2; //digital pin 2, red LED
    float vin=0; // sensed voltage

    void setup()
    {
    pinMode(irtx, OUTPUT);
    pinMode(active_train, OUTPUT);
    digitalWrite(irtx,HIGH); //turn on IR LED
    }

    void loop()
    {
    vin = .0049 * analogRead(A0); // get phototransistor voltage and convert 0-1024 to voltage
    delay(10); // a little settling time

    if (vin > 4.3) // train detection if greater than 4.3V
    {
    digitalWrite(active_train,HIGH); // this indication is not needed after testing
    /* This is where you would put more code to activate your device
    // such as a servo, motor, sounds, etc
    */
    }
    else // no train is present
    {
    digitalWrite(active_train,LOW); //this indication is not needed after testing
    /* This is where you would put more code to de-activate your device
    // such as a servo, motor, sounds, etc
    */
    }
    delay(1000); // slow it all down - it's only a train!
    }
    Attached Images Attached Images

  25. #40
    Join Date
    Mar 2011
    Location
    Windsor, Ontario, Canada
    Posts
    1,083
    Thanks
    1,663
    Thanked 2,372 Times in 603 Posts
    Mentioned
    19 Post(s)
    Tagged
    0 Thread(s)

    Default

    For reflection based sensing, I have found that these work very well:

    Free-shipping-100pcs-TCRT5000L-TCRT5000-font-b-Reflective-b-font-Optical-font-b-Sensor-b-font.jpg

    I've worked before with the two IR pieces that span the track like above, and it's often tricky to keep them aligned and sensing properly. Not sure why they move, but they do. But, when they work, they work good.

    My recent expiriment with these sensors has proven successful. Here's a pic of a trackside sensor that can tell when a car is passing by. In the second pic, you can see the screen where I display what car number I'm on (I use the gaps in between to count a car). The overall circuit is more complex than it needs to be and I'm working on slimming it down a bit. Don't let all the wires scare you away from the Arduino gadget - it's a great tool and easy to learn.

    IMG_00000054.jpgIMG_00000056.jpg
    Peter

    Layout Depot (share your designs with others): www.LayoutDepot.com
    My Build Thread: www.nscale.net/forums/showthread.php?28081-Green-Valley-Railway

Similar Threads

  1. Model Power Haunted House using Arduino Micro Controller
    By ndronet in forum Structures & Scenery
    Replies: 7
    Last Post: 12th Oct 2018, 10:24 AM
  2. 3D printing and model railroading
    By vikingservices in forum General Rail Discussion
    Replies: 0
    Last Post: 11th Feb 2013, 11:58 AM
  3. Your interest in railroading/model railroading?
    By simpdogg in forum General Rail Discussion
    Replies: 22
    Last Post: 10th Jul 2007, 11:39 PM
  4. Model Railroading subscription
    By ragnarock4 in forum General Rail Discussion
    Replies: 14
    Last Post: 28th Apr 2006, 03:38 PM
  5. Digitrax/DCC/Model Railroading on DIY
    By colonel_26 in forum DCC
    Replies: 2
    Last Post: 6th Jun 2004, 12:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •