Trying to learn to use Arduino microcontroller to create a stop, delay, startup. Using insulated rail as a detector switch, and one problem is having the switch to be ignored. Please help, but remember to make it basic. I am in the early stage of learning how to program this.
Replies sorted oldest to newest
Hi Butch -
How are you connecting the insulated rail to the Arduino?
What do you mean by having the switch ignored?
I will use a relay actuated by the train wheelset to turn on a 5V input to the Arduino. This will change an output which will actuate a relay to turn off the power to the track. So then I can put in a delay and have the Train sit for a few seconds. But when I want to restart the train, the train will be actuating that 5V input, because it is still sitting on the insulated rail-switch.
Separate your loop into two. In 1, you monitor the switch and when closed, turn off track power, and wait for whatever your stop delay is. Then in 2, you turn on track power, then monitor the switch waiting for it to open (train clears the station). When 2 completes it starts 1 again.
In practice, in part 2, you want to confirm the switch is really open before deciding the train has left the station. That is, intermittent wheel contact might cause the switch to momentarily open even while the train is on the monitored track. So in 2, you might test the switch, wait 1/4 sec, test it again to make sure, and then again for good measure. This is the classic programming task of "debouncing" a switch input.
One can also denounce the noisy track signal in hardware, though for a single input it's easier in software.
Here is a solid-state circuit I use to convert the noisy AC signal from an isolated rail into a clean 5V signal to the Arduino input:
It relies on an AC optocoupler, a capacitor/resistor to filter the signal, and a Schmitt trigger to yield a clean transition.
Attachments
$#@%!% spell-check!
Butch, I have not tested this code, but see if it works. It implements Stan's method of waiting 1/4 second after the detector is off to be sure the train has left, rather than intermittent contact with dirty track.
It assumes that the input pin goes to ground (LOW) when the relay is activated; the internal pullups on the input pin are activated to keep the input HIGH when the relay is off. Set "detector" to the # of the input pin.
Sorry the forum is not preserving the indenting so it's hard to read
const byte detector = ##; // pin connected to relay; grounded when insulated rail occupied
const unsigned long bounceTime = 250; // time (milliseconds) sensor must be off before we're sure it's unoccupied
unsigned long lastContact;
boolean trainArrived = false;
void setup() {
pinMode(detector, INPUT);
digitalWrite(detector, HIGH); // enable internal pullup
}
void loop() {
if (digitalRead(detector) == LOW) {
lastContact = millis(); // mark that detector was occupied
if (trainArrived == false) { // train has hit insulated rail for the first time
trainArrived = true;
/* turn off track power */
/* wait */
/* turn on track power */
}
}
if ((digitalRead(detector) == HIGH) && (trainArrived == true)) // train leaves, or could be poor contact?
if ((millis() - lastContact) > bounceTime) trainArrived = false; // enough time has elapsed since sensor was last on
}
I think denouncing noisy track signals is fine way of describing what's required! But debouncing is of course the accepted terminology. A key point in the Professor's circuit is the time-constant of the RC circuit. 47k x 47uF = 2.2 seconds. In other words a long time relative to the 0.05 second interval suggested in the Arduino link above. This is the key to debouncing or denouncing isolated rail detection switches. Most applications of debouncing are for push-button switches where the contacts of the switches settle very quickly (measured in milliseconds). The activity or settling times for isolated rail detection can be much slower requiring longer time-constants
I think the hardware approach also has the advantage of automatically integrating the degree of contact. The very last touch of contact with a departing train's last wheels hardly registers on the hardware, but it would trigger the full debounce delay on software. When I tried an all-software approach, I had to use really long debounce times to ensure reliability.
There are debounce algorithms that emulate the hardware approach by integrating the number of contacts registered, but I stuck with good old-fashioned capacitors!
Wow! This will take me some time to decipher.
Here is the problem I am having with trying to learn how to use the Arduino: There are many tutorials that cover basics like a switch operating a relay, but then there is a quantum leap to the level that you have sent me. I have not found the step-by-step training on many of the items you are using. Where is that?
I would like to understand what is going on here.
In the first line, you are setting up a constant called byte detector, and we are going to assign that a value. Is that correct?
How's this?
<code>
const byte detector = ##; // pin connected to relay; grounded when insulated rail occupied
const unsigned long bounceTime = 250; // time (milliseconds) sensor must be off before we're sure it's unoccupied
unsigned long lastContact;
boolean trainArrived = false;
void setup() {
pinMode(detector, INPUT);
digitalWrite(detector, HIGH); // enable internal pullup
}
void loop() {
if (digitalRead(detector) == LOW) {
lastContact = millis(); // mark that detector was occupied
if (trainArrived == false) { // train has hit insulated rail for the first time
trainArrived = true;
/* turn off track power */
/* wait */
/* turn on track power */
}
}
if ((digitalRead(detector) == HIGH) && (trainArrived == true)) // train leaves, or could be poor contact?
if ((millis() - lastContact) > bounceTime) trainArrived = false; // enough time has elapsed since sensor was last on
}
</code>
That helps me read it. Unfortunately, it does not help me understand it.
Please try to understand that you guys are writing code at a level I do not understand. I would like to understand, but need someone to explain what is being done here.
Please "dumb-it-down" for this newbie.
That helps me read it. Unfortunately, it does not help me understand it.
Please try to understand that you guys are writing code at a level I do not understand. I would like to understand, but need someone to explain what is being done here.
Please "dumb-it-down" for this newbie.
Maybe this would help?
http://www.arduino.cc/en/Reference/HomePage
Also a lot of good info on this site, tutorials also.
Some more tutorials here:
http://www.jeremyblum.com/category/arduino-tutorials/
I'm new to all this too and am finding that learning electronics (and Arduino) is part reading reference material and a LOT of fiddling with the stuff to figure out what different things do, at least that's how I am learning. For me the tinkering part is a lot more fun than reading.
Down side to this method is that you sometimes need new parts to replace the bad ones you create while tinkering. Not everything you try will work out, at least mine don't. Hopefully your failures are all with cheaper parts (mine haven't always been).
Following this forum, you can also learn a great deal and the members are very knowledgeable and helpful if you get stuck. I save all the schematics I find here and the threads that go with them (including this one). These are what got me started.
If you haven't already, my suggestion would be to look at some example beginner projects, get some test parts and start experimenting. There are starter kits available for Arduino that include parts, tutorials and other stuff needed to get going.
If you have passed that point, get the parts for your current project and set up a test bench, start hooking things up and try them out. Watch what happens and try to learn from that. If something burns up, try to figure out why, ask the folks here about it, then get another one and keep trying until you get it.
Thanks.
I tried to use the arduino site to decipher the subject code, but that did not work for me.
I will look at the jeremy blum tutorials.
I have purchased some parts and have been experimenting, but I want to put something together for a display I need now. I want to put together something basic to stop the train, and I feel I could easily do it with relays and timers, but I want to learn to use the arduino.
I don't think I have enough information to wire the correct circuit for the code from Prof. Chaos. For example, I don't see that he defined what pins he was using.
I'm afraid you are way ahead of me with the Arduino stuff, mine is still on order and hasn't yet been delivered. Perhaps Prof Chaos or gunrunnerjohn will be along to help with your Arduino questions?
Forum member Dale H has some very good info on different relay, isolated rail, etc. circuits on his blog site, hope he doesn't mind me posting it. I have learned a lot by studying the info he has posted here.
Entire blog is here:
http://www.jcstudiosinc.com/Bl...tegoryMain?catId=426
Dale H's isolated rail/relay info:
Hi Butch -
Sorry, I thought you were just looking for advice on how to handle the problem of the train still occupying the detector when it starts up. I don't know how your hardware is set up, so I didn't specify the input pin (and didn't include the code to turn the track power on and off).
A lot of useful information is on the Arduino Reference page. It has information about all the basic concepts and functions used in Arduino programming.
Your project is very doable, but I always start by developing and testing small parts of a project first. I am going to suggest that you first build and test the detection system. So if you don't mind me giving you an "exercise," I suggest you start here:
Hardware:
Connect a relay so that when the insulated rail is occupied, an Arduino input pin is connected to the Arduino's GND. (Do not connect the input pin to the track power supply).
Software:
Read the state of the input pin.
Print "1" to the serial monitor if the pin is HIGH, or "0" if the pin is LOW.
Wait 1 millisecond (to avoid overloading serial communication)
Repeat indefinitely.
This will get you started on programming and basic circuit concepts. Once you have this under your belt, adding the logic and control parts will be easy. Ask away as you build it!
Will do.
The hardware part is easy for me.
The software will require me to do some reading and researching.
Thanks for your patience.
Butch,
I found Jeremy Blum's YouTube series of Arduino lessons about the best to learn the Arduino. His second lesson specifically addresses debouncing a switch.
Take a few minutes to watch this one, and it will probably motivate you to watch the whole series.
Good luck and let us know how you did it.
Alex
Well it took me a month, but I completed my Station-Stop using the Arduino. I used tutorials from many sources to gain the information I needed.
I used an Arduino UNO, a SEEED relay shield, and created my own board that fit between the purchased ones. My board contained terminals for inputs and LEDs to signify the operation of the relays on the relay shield.
It is easy to see the power of this method, because once all the wiring is done, program changes are easy.
I will continue to experiment with it, and encourage others to do so.
If you have ever wanted to be able to create a trolley reversing circuit, or a station stop(mine includes an "All Aboard" anouncement) or sequential lighting, this is a way to do it.
Well it took me a month, but I completed my Station-Stop using the Arduino. I used tutorials from many sources to gain the information I needed.
I used an Arduino UNO, a SEEED relay shield, and created my own board that fit between the purchased ones. My board contained terminals for inputs and LEDs to signify the operation of the relays on the relay shield.
It is easy to see the power of this method, because once all the wiring is done, program changes are easy.
I will continue to experiment with it, and encourage others to do so.
If you have ever wanted to be able to create a trolley reversing circuit, or a station stop(mine includes an "All Aboard" anouncement) or sequential lighting, this is a way to do it.
Good that you have it all working now! Could you post some pictures and wiring diagram for us? My Arduino stuff has arrived and I have only managed to modify a couple of the demo projects so far. I would be very interested in seeing what you have done with your project here.
Thanks!
The code would be useful as well.
How do you post photos?
When making a post, there is a '+ Add Attachments' (big green +) in the lower right, just above the 'Cancel - Submit Reply' buttons. Select that and you can post pictures or other attachments like .pdf's, track plan files, etc.