Login
or
Register
Subscriber & Member Login
Login, or register today to interact in our online community, comment on articles, receive our newsletter, manage your account online and more!
Login
Register
Home
»
Model Railroader
»
Forums
»
Electronics and DCC
»
Arduino uses and wondering why there aren't more articles here
Edit post
Edit your reply below.
Post Body
Enter your post below.
<p>[quote user="nycmodel"]</p> <p>Finally got my Arduino code worked out on the bench and wired it up to the layout for my crossing gate and flasher logic.</p> <p><img style="display:inline;" src="http://i1347.photobucket.com/albums/p709/Modelrrtrains/arduino%202016%20004_zpsh0kdljd8.jpg" alt=" " /></p> <p>Since I had many circuit boards left over from my original Bruce Chubb CRMI installation on a previous layout, I chose to utilize those cab relays where possible by modifying the boards. The perfboard on the upper right allows the Arduino outputs to drive the relays via the transistors. I added additional Optimized Detectors to cover the 6 inputs.</p> <p><img style="display:inline;" src="http://i1347.photobucket.com/albums/p709/Modelrrtrains/arduino%202016%20001_zpsjogprljk.jpg" alt=" " /></p> <p><img style="display:inline;" alt=" " /></p> <p><img style="width:704px;height:auto;display:inline;" src="http://i1347.photobucket.com/albums/p709/Modelrrtrains/arduino%20controlled%20system_jpg_zpssb0xwpb9.jpg" alt=" " /></p> <p><br />/*<br /> crossing_logic_1<br />12/24/16<br /> <br /> gates_down_1 flashers_on_1<br /> |---- X<br /> -----------------| |-----------------------------------| |------------------ signal_block_1 entire length<br /> west_block_1 mid_block_1 east_block_1<br /> <br /> -----------------| |-----------------------------------| |------------------ signal_block_2 entire length<br /> west_block_2 ----| mid_block_2 X east_block_2<br /> <br /> gates_down_2 flashers_on_2<br /> <br /> All 6 inputs are from Chubb Optimized detectors where HIGH = unoccupied and LOW = occupied<br /> All 4 outputs will turn on HIGH when the proper inputs are LOW, driving relays via transistors connected to the 12v supply. These relays<br /> will then activate crossing gates, flashers and block signals.<br /> <br /> */<br /> <br />// Define pins<br />const int west_block_1_pin = 2;<br />const int mid_block_1_pin = 3;<br />const int east_block_1_pin = 4;<br />const int west_block_2_pin = 5;<br />const int mid_block_2_pin = 6;<br />const int east_block_2_pin = 7;<br />const int gates_down_pin = 8;<br />const int flashers_on_pin = 9;<br />const int signal_block_1_pin = 10;<br />const int signal_block_2_pin = 11;<br /><br />// Define and initialize all variables as HIGH (off) except outputs that drive relays through transister switches where HIGH is on.<br />int west_block_1 = HIGH;<br />int mid_block_1 = HIGH;<br />int east_block_1 = HIGH;<br />int west_block_2 = HIGH;<br />int mid_block_2 = HIGH;<br />int east_block_2 = HIGH;<br />int gates_down_1 = LOW; //relay active HIGH to show gates down for block 1<br />int flashers_on_1 = LOW; //relay active HIGH to show flashers on for block 1<br />int gates_down_2 = LOW; //relay active HIGH to show gates down for block 2<br />int flashers_on_2 = LOW; //relay active HIGH to show flashers for block 2<br />int signal_block_1 = LOW; //relay active HIGH to show block 1 occupied<br />int signal_block_2 = LOW; //relay active HIGH to show block 2 occupied<br />int eastbound_sequence_1 = HIGH; //active HIGH show eastbound train moving through block 1<br />int westbound_sequence_1 = HIGH; //active HIGH show westbound train moving through block 1<br />int eastbound_sequence_2 = HIGH; //active HIGH show eastbound train moving through block 2<br />int westbound_sequence_2 = HIGH; //active HIGH show westbound train moving through block 2<br />void setup() {<br /> //start serial connection for debugging<br /> Serial.begin(9600);<br /> // initialize digital pin 13 as an output for onboard LED to use as a "heartbeat" for the sketch<br /> pinMode(13, OUTPUT);<br /> //configure pins 2 to 7 as an input and enable the internal pull-up resistor<br /> pinMode(west_block_1_pin, INPUT_PULLUP); // Unlike pinMode(INPUT), there is no pull-down resistor necessary. An internal<br /> pinMode(mid_block_1_pin, INPUT_PULLUP); // 20K-ohm resistor is pulled to 5V. This configuration causes the input to<br /> pinMode(east_block_1_pin, INPUT_PULLUP); // read HIGH when the switch is open, and LOW when it is closed.<br /> pinMode(west_block_2_pin, INPUT_PULLUP);<br /> pinMode(mid_block_2_pin, INPUT_PULLUP);<br /> pinMode(east_block_2_pin, INPUT_PULLUP); <br /> //configure pins 8 to 11 as outputs. HIGH when not actuated and LOW when actuated ;i.e. action activated<br /> pinMode(gates_down_pin, OUTPUT);<br /> pinMode(flashers_on_pin, OUTPUT);<br /> pinMode(signal_block_1_pin, OUTPUT);<br /> pinMode(signal_block_2_pin, OUTPUT);<br /> // Initialize all outputs as off<br /> digitalWrite(gates_down_pin, LOW); //start with gates up<br /> digitalWrite(flashers_on_pin, LOW); //start with flashers off<br /> digitalWrite(signal_block_1_pin, LOW); //start with block 1 unoccupied<br /> digitalWrite(signal_block_2_pin, LOW); //start with block 2 unoccupied<br /> <br />}<br /><br />void loop() {<br /> digitalWrite(13, HIGH); // turn the "heartbeat" LED on <br /> //read the input values into the variables<br /> west_block_1 = digitalRead(west_block_1_pin);<br /> mid_block_1 = digitalRead(mid_block_1_pin);<br /> east_block_1 = digitalRead(east_block_1_pin);<br /> west_block_2 = digitalRead(west_block_2_pin);<br /> mid_block_2 = digitalRead(mid_block_2_pin);<br /> east_block_2 = digitalRead(east_block_2_pin);<br /> Serial.print("gates_down_1: ");<br /> Serial.println(gates_down_1);<br /> Serial.print("flashers on_1: ");<br /> Serial.println(flashers_on_1);<br /> Serial.print("gates_down_2: ");<br /> Serial.println(gates_down_2);<br /> Serial.print("flashers on_2: ");<br /> Serial.println(flashers_on_2);<br /> Serial.print("west_block_1: ");<br /> Serial.println(west_block_1);<br /> Serial.print("mid_block_1: ");<br /> Serial.println(mid_block_1);<br /> Serial.print("east_block_1: ");<br /> Serial.println(east_block_1);<br /> Serial.print("west_block_2: ");<br /> Serial.println(west_block_2);<br /> Serial.print("mid_block_2: ");<br /> Serial.println(mid_block_2);<br /> Serial.print("east_block_2: ");<br /> Serial.println(east_block_2);<br /> Serial.print("eastbound_sequence_1: ");<br /> Serial.println(eastbound_sequence_1);<br /> Serial.print("westbound_sequence_1: ");<br /> Serial.println(westbound_sequence_1);<br /> Serial.print("eastbound_sequence_2: ");<br /> Serial.println(eastbound_sequence_2);<br /> Serial.print("westbound_sequence_2: ");<br /> Serial.println(westbound_sequence_2);<br /> <br /> <br /> if (west_block_1 == HIGH && mid_block_1 == HIGH && east_block_1 == HIGH && west_block_2 == HIGH && mid_block_2 == HIGH && east_block_2 == HIGH) { //blocks 1 and 2 completely clear<br /> /* ---------------------------------------------- INITIALIZATION LOGIC ------------------------------------------------------ */ <br /> digitalWrite(gates_down_pin, LOW); //raise gates<br /> digitalWrite(flashers_on_pin, LOW); //turn off flashers<br /> gates_down_1 = LOW; //show gates up<br /> flashers_on_1 = LOW; //show flashers off<br /> gates_down_2 = LOW; //show gates up<br /> flashers_on_2 = LOW; //show flashers off<br /> eastbound_sequence_1 = HIGH; //initialize block 1 eastbound sequence<br /> westbound_sequence_1 = HIGH; //initialize block 1 westbound sequence<br /> eastbound_sequence_2 = HIGH; //initialize block 2 eastbound sequence<br /> westbound_sequence_2 = HIGH; //initialize block 2 westbound sequence<br /> digitalWrite(signal_block_1_pin, LOW); //show block 1 clear<br /> digitalWrite(signal_block_2_pin, LOW); //show block 2 clear<br /> Serial.println("Initializing block 1"); <br /> Serial.println("Initializing block 2"); } <br /> else if (west_block_1 == HIGH && mid_block_1 == HIGH && east_block_1 == HIGH){ //block 1 completely clear<br /> eastbound_sequence_1 = HIGH; //initialize block 1 eastbound sequence<br /> westbound_sequence_1 = HIGH; //initialize block 1 westbound sequence<br /> digitalWrite(signal_block_1_pin, LOW); //show block 1 clear<br /> Serial.println("Initializing block 1"); }<br /> else if (west_block_2 == HIGH && mid_block_2 == HIGH && east_block_2 == HIGH){ //block 2 completely clear<br /> eastbound_sequence_2 = HIGH; //initialize block 2 eastbound sequence<br /> westbound_sequence_2 = HIGH; //initialize block 2 westbound sequence<br /> digitalWrite(signal_block_2_pin, LOW); //show block 1 clear<br /> Serial.println("Initializing block 2"); } <br /> <br /> /* ----------------------------------------------------------------------------------------------------------------------------------- */ <br /> /* If the blocks are not completely clear then proceed with the gate and flasher logic for each block */<br /> /* --------------------------------------------------------- BLOCK 1 ------------------------------------------------------------------- */<br /> /* ------------------------------------------------------ CROSSING GATE ------------------------------------------------------------- */<br /> /* ----------------------------------------------------- EASTBOUND LOGIC ------------------------------------------------------------ */<br /> if (west_block_1 == LOW && mid_block_1 == HIGH && gates_down_1 == LOW && eastbound_sequence_1 == HIGH && westbound_sequence_1 == HIGH) { //initial Eastbound approach gates<br /> digitalWrite(gates_down_pin, HIGH); //drop gates<br /> gates_down_1 = HIGH; //show gates down<br /> eastbound_sequence_1 = LOW; //set start of eastbound sequence <br /> Serial.print("Drop gates initial eastbound"); }<br /> else if (west_block_1 == HIGH && mid_block_1 == LOW && gates_down_1 == HIGH && eastbound_sequence_1 == LOW && westbound_sequence_1 == HIGH) { //clearing crossing eastbound gates<br /> gates_down_1 = LOW;}<br /> /* --------------------------------------------------- WESTBOUND LOGIC ------------------------------------------------------------ */<br /> else if (mid_block_1 == LOW && west_block_1 == HIGH && gates_down_1 == LOW && eastbound_sequence_1 == HIGH) { //&& westbound_sequence_1 == HIGH && eastbound_sequence_1 == HIGH) { //initial Westbound approach gates<br /> digitalWrite(gates_down_pin, HIGH); //drop gates<br /> gates_down_1 = HIGH; //show gates down<br /> westbound_sequence_1 = LOW; //set start of westbound sequence <br /> Serial.print("Drop gates initial westbound"); }<br /> else if (west_block_1 == LOW && mid_block_1 == HIGH && gates_down_1 == HIGH && westbound_sequence_1 == LOW) { //clearing crossing westbound gates<br /> gates_down_1 = LOW;}<br /> <br /> /* ------------------------------------------------------__ FLASHER ------------------------------------------------------------- */ <br /> /* ----------------------------------------------------- EASTBOUND LOGIC ------------------------------------------------------------ */<br /> else if (mid_block_1 == LOW && east_block_1 == HIGH && flashers_on_1 == LOW && eastbound_sequence_1 == LOW && westbound_sequence_1 == HIGH) { //initial Eastbound approach flashers<br /> digitalWrite(flashers_on_pin, HIGH); //turn on flashers<br /> flashers_on_1 = HIGH; //show flashers on<br /> eastbound_sequence_1 = LOW; //set start of eastbound sequence<br /> Serial.print("Flashers on initial eastbound"); }<br /> else if (east_block_1 == LOW && mid_block_1 == HIGH && flashers_on_1 == HIGH && eastbound_sequence_1 == LOW) { //clearing crossing eastbound flashers<br /> flashers_on_1 = LOW;}<br /> /* ----------------------------------------------------- WESTBOUND LOGIC ------------------------------------------------------------ */<br /> else if (east_block_1 == LOW && mid_block_1 == HIGH && flashers_on_1 == LOW && westbound_sequence_1 == HIGH && eastbound_sequence_1 == HIGH) { //initial Westbound approach flashers<br /> digitalWrite(flashers_on_pin, HIGH); //turn on flashers<br /> flashers_on_1 = HIGH; //show flashers on<br /> westbound_sequence_1 = LOW; //set start of westbound sequence<br /> Serial.print("Flashers on initial westbound"); }<br /> else if (east_block_1 == HIGH && mid_block_1 == LOW && flashers_on_1 == HIGH && westbound_sequence_1 == LOW) { //clearing crossing westbound flashers<br /> flashers_on_1 = LOW;}<br /> <br /> /* --------------------------------------------------------- BLOCK 2 ------------------------------------------------------------------- */<br /> /* ------------------------------------------------------ CROSSING GATE ------------------------------------------------------------- */<br /> /* ----------------------------------------------------- EASTBOUND LOGIC ------------------------------------------------------------ */<br /> if (west_block_2 == LOW && mid_block_2 == HIGH && gates_down_2 == LOW && eastbound_sequence_2 == HIGH && westbound_sequence_2 == HIGH) { //initial Eastbound approach gates<br /> digitalWrite(gates_down_pin, HIGH); //drop gates<br /> gates_down_2 = HIGH; //show gates down<br /> eastbound_sequence_2 = LOW; //set start of eastbound sequence <br /> Serial.print("Drop gates initial eastbound"); }<br /> else if (west_block_2 == HIGH && mid_block_2 == LOW && gates_down_2 == HIGH && eastbound_sequence_2 == LOW && westbound_sequence_2 == HIGH) { //clearing crossing eastbound gates<br /> gates_down_2 = LOW;}<br /> /* --------------------------------------------------- WESTBOUND LOGIC ------------------------------------------------------------ */<br /> else if (mid_block_2 == LOW && west_block_2 == HIGH && gates_down_2 == LOW && eastbound_sequence_2 == HIGH) { //&& westbound_sequence_1 == HIGH && eastbound_sequence_1 == HIGH) { //initial Westbound approach gates<br /> digitalWrite(gates_down_pin, HIGH); //drop gates<br /> gates_down_2 = HIGH; //show gates down<br /> westbound_sequence_2 = LOW; //set start of westbound sequence <br /> Serial.print("Drop gates initial westbound"); }<br /> else if (west_block_2 == LOW && mid_block_2 == HIGH && gates_down_2 == HIGH && westbound_sequence_2 == LOW) { //clearing crossing westbound gates<br /> gates_down_2 = LOW;}<br /> /* ------------------------------------------------------__ FLASHER ------------------------------------------------------------- */ <br /> /* ----------------------------------------------------- EASTBOUND LOGIC ------------------------------------------------------------ */<br /> else if (mid_block_2 == LOW && east_block_2 == HIGH && flashers_on_2 == LOW && eastbound_sequence_2 == LOW && westbound_sequence_2 == HIGH) { //initial Eastbound approach flashers<br /> digitalWrite(flashers_on_pin, HIGH); //turn on flashers<br /> flashers_on_2 = HIGH; //show flashers on<br /> eastbound_sequence_1 = LOW; //set start of eastbound sequence<br /> Serial.print("Flashers on initial eastbound"); }<br /> else if (east_block_2 == LOW && mid_block_2 == HIGH && flashers_on_2 == HIGH && eastbound_sequence_2 == LOW) { //clearing crossing eastbound flashers<br /> flashers_on_2 = LOW;}<br /> /* ----------------------------------------------------- WESTBOUND LOGIC ------------------------------------------------------------ */<br /> else if (east_block_2 == LOW && mid_block_2 == HIGH && flashers_on_2 == LOW && westbound_sequence_2 == HIGH && eastbound_sequence_2 == HIGH) { //initial Westbound approach flashers<br /> digitalWrite(flashers_on_pin, HIGH); //turn on flashers<br /> flashers_on_2 = HIGH; //show flashers on<br /> westbound_sequence_2 = LOW; //set start of westbound sequence<br /> Serial.print("Flashers on initial westbound"); }<br /> else if (east_block_2 == HIGH && mid_block_2 == LOW && flashers_on_2 == HIGH && westbound_sequence_2 == LOW) { //clearing crossing westbound flashers<br /> flashers_on_2 = LOW;}<br /> <br /> /* --------------- Fail safe train blocking crossing --------------------------------------------------- */<br /> else if (east_block_1 == LOW && mid_block_1 == LOW) { //Flasher crossing is blocked, track 1<br /> digitalWrite(flashers_on_pin, HIGH); //turn on flashers<br /> flashers_on_1 = HIGH; //show flashers on<br /> eastbound_sequence_1 = LOW; //set start of eastbound sequence<br /> Serial.print("Flashers on-fail safe"); }<br /> else if (east_block_2 == LOW && mid_block_2 == LOW) { //Flasher crossing is blocked, track 2<br /> digitalWrite(flashers_on_pin, HIGH); //turn on flashers<br /> flashers_on_2 = HIGH; //show flashers on<br /> eastbound_sequence_2 = LOW; //set start of eastbound sequence<br /> Serial.print("Flashers on-fail safe"); }<br /> else if (west_block_1 == LOW && mid_block_1 == LOW) { //gate crossing is blocked, track 1 <br /> digitalWrite(gates_down_pin, HIGH); //drop gates<br /> gates_down_1 = HIGH; //show gates down<br /> eastbound_sequence_1 = LOW; //set start of eastbound sequence <br /> Serial.print("Drop gates-fail safe"); }<br /> else if (west_block_2 == LOW && mid_block_2 == LOW) { //gate crossing is blocked, track 2 <br /> digitalWrite(gates_down_pin, HIGH); //drop gates<br /> gates_down_2 = HIGH; //show gates down<br /> eastbound_sequence_2 = LOW; //set start of eastbound sequence <br /> Serial.print("Drop gates-fail safe"); }<br /> <br /> /* ------------------- Raise gate logic --------------------------------------------------------------- */<br /> /* Since there are two tracks we don't want to raise the gates for one track if the other is still down */<br /> if (gates_down_1 == LOW && gates_down_2 == LOW){ // both tracks are clear of the gate crossing <br /> digitalWrite(gates_down_pin, LOW); //raise gates<br /> Serial.println("Raise gates"); } <br /> <br /> /* ------------------- Turn off flasher logic ____________________________________________ */ <br /> /* Since there are two tracks we don't want to turn off the flashers for one track if the other is still on */<br /> if (flashers_on_1 == LOW && flashers_on_2 == LOW){ // both tracks are clear of the flasher crossing <br /> digitalWrite(flashers_on_pin, LOW); //turn off flashers<br /> Serial.println("Turn off flashers"); } <br /> <br /> <br /> /* ----------------- BLOCK SIGNAL LOGIC ------------------------------------------ */<br /> Serial.println("");<br /> if (east_block_1 == LOW || mid_block_1 == LOW || west_block_1 == LOW){ // If any of the 3 blocks for track 1 are occupied set block 1 signals to red<br /> digitalWrite(signal_block_1_pin, HIGH); //show block 1 occupied}<br /> Serial.println("Block 1 occupied");} <br /> else if (east_block_1 && HIGH && mid_block_1 == HIGH && west_block_1 == HIGH){ //If all 3 blocks for track 1 are clear set block 1 signals to green<br /> digitalWrite(signal_block_1_pin, LOW); //show block 1 clear<br /> Serial.println("Block 1 clear");} <br /> <br /> if (east_block_2 == LOW || mid_block_2 == LOW || west_block_2 == LOW){ // If any of the 3 blocks for track 2 are occupied set block 2 signals to red<br /> digitalWrite(signal_block_2_pin, HIGH);<br /> Serial.println("Block 2 occupied");} //show block 2 occupied<br /> else if (east_block_2 && HIGH && mid_block_2 == HIGH && west_block_2 == HIGH){ //If all 3 blocks for track 2 are clear set block 2 signals to green<br /> digitalWrite(signal_block_2_pin, LOW); //show block 2 clear<br /> Serial.println("Block 2 clear");} <br /> /* ------------------------------------------------------------------------------ */<br /> // digitalWrite(13, HIGH); // turn the "heartbeat" LED on <br /> // delay(500); // wait for a half second<br /> digitalWrite(13, LOW); // turn the "heartbeat" LED off<br /> //delay(500); // wait for a half second<br /> Serial.println(""); //carriage return <br /> Serial.println(""); //carriage return <br /> Serial.println(""); //carriage return <br /> }<br /><br /></p> <div style="clear:both;"> </div> <p>[/quote]</p> <p>This is why Im not really interested in Arduino. I have no desire to write or debug 100s of lines of code, I will wait patiently for LCC to become fully fleshed out with a GUI with dropdown boxes and radio buttons to select the functions. The above lines of code are not my version of fun. If its yours, great! I on the other hand would prefer to purchase off the shelf components and not write code.</p>
Tags (Optional)
Tags are keywords that get attached to your post. They are used to categorize your submission and make it easier to search for. To add tags to your post type a tag into the box below and click the "Add Tag" button.
Add Tag
Update Reply
Subscriber & Member Login
Login, or register today to interact in our online community, comment on articles, receive our newsletter, manage your account online and more!
Login
Register
Users Online
There are no community member online
Search the Community
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
Model Railroader Newsletter
See all
Sign up for our FREE e-newsletter
and get model railroad news in your inbox!
Sign up