{"id":707,"date":"2016-09-12T01:15:03","date_gmt":"2016-09-12T01:15:03","guid":{"rendered":"https:\/\/danzbb.com\/?p=707"},"modified":"2016-09-13T02:00:00","modified_gmt":"2016-09-13T02:00:00","slug":"servo-activated-lgb-rail-crossing","status":"publish","type":"post","link":"https:\/\/danzbb.com\/index.php\/2016\/09\/12\/servo-activated-lgb-rail-crossing\/","title":{"rendered":"Servo Activated LGB Rail Crossing"},"content":{"rendered":"<p>I&#8217;ve been fiddling with this for quite some time. My initial experiments didn&#8217;t work out too well as the IR sensors didn&#8217;t like the waterproofing and the &#8220;stainless&#8221; steel wire I was using rusted out.<\/p>\n<p>But here&#8217;s some progress<\/p>\n<p><iframe loading=\"lazy\" src=\"\/\/www.youtube.com\/embed\/sznwIbtJGKo\" width=\"560\" height=\"314\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p>The servos are mounted in the bottom of a set of LGB crossing gates.<\/p>\n<p><a href=\"https:\/\/danzbb.com\/wp-content\/uploads\/2016\/09\/crossing_gate_servo.jpg\"><img loading=\"lazy\" class=\"alignnone size-medium wp-image-706\" src=\"https:\/\/danzbb.com\/wp-content\/uploads\/2016\/09\/crossing_gate_servo-300x249.jpg\" alt=\"crossing_gate_servo\" width=\"300\" height=\"249\" srcset=\"https:\/\/danzbb.com\/wp-content\/uploads\/2016\/09\/crossing_gate_servo-300x249.jpg 300w, https:\/\/danzbb.com\/wp-content\/uploads\/2016\/09\/crossing_gate_servo-768x638.jpg 768w, https:\/\/danzbb.com\/wp-content\/uploads\/2016\/09\/crossing_gate_servo.jpg 800w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>The servos are controlled by an Arduino board.<\/p>\n<p><!--more-->The program is set up to close the gates when a trigger is sensed (currently a reed switch). Then it starts counting to three seconds and monitors the switch. If another trigger occurs within the three seconds, the timer starts over. Once no trigger has been sensed for three seconds, the gates raise.<\/p>\n<p>Here&#8217;s a circuit drawing<\/p>\n<p><a href=\"https:\/\/danzbb.com\/wp-content\/uploads\/2016\/09\/servo-gate.jpg\"><img loading=\"lazy\" class=\"alignnone size-large wp-image-709\" src=\"https:\/\/danzbb.com\/wp-content\/uploads\/2016\/09\/servo-gate-1024x927.jpg\" alt=\"servo-gate\" width=\"604\" height=\"547\" srcset=\"https:\/\/danzbb.com\/wp-content\/uploads\/2016\/09\/servo-gate.jpg 1024w, https:\/\/danzbb.com\/wp-content\/uploads\/2016\/09\/servo-gate-300x272.jpg 300w, https:\/\/danzbb.com\/wp-content\/uploads\/2016\/09\/servo-gate-768x695.jpg 768w\" sizes=\"(max-width: 604px) 100vw, 604px\" \/><\/a><\/p>\n<p>Here&#8217;s the Arduino sketch<\/p>\n<pre>\/\/ include the servo header file\r\n#include &lt;Servo.h&gt;\r\n\r\n\/\/ create servo object to control a servo\r\nServo myservo1;\r\nServo myservo2;\r\n\r\n\/\/ constants for pins\r\nconst int buttonPin = 1;\r\nconst int servo1Pin = 2;\r\nconst int servo2Pin = 3;\r\nconst int ledPin = 13;\r\n\r\n\/\/ variables\r\nboolean gateClosed = false;\r\nint buttonState = 0;\r\nint pos = 80;\r\nint oppos = 0;\r\nlong detectTime;\r\nlong t;\r\n\r\n\/\/setup routine\r\nvoid setup(){\r\n \/\/ initialize the LED pin as an output:\r\n pinMode(ledPin, OUTPUT);\r\n \/\/ initialize the pushbutton pin as an input:\r\n pinMode(buttonPin, INPUT);\r\n \/\/ set the LED to off\r\n digitalWrite(ledPin, LOW);\r\n \/\/ initialize the servo pin as a servo\r\n myservo1.attach(servo1Pin);\r\n myservo2.attach(servo2Pin);\r\n}\r\n\r\n\/\/ main program loop\r\nvoid loop(){\r\n \/\/ read the button\r\n buttonState = digitalRead(buttonPin);\r\n \/\/ if the button state is HIGH then the switch is closed\r\n if (buttonState == HIGH){\r\n \/\/ set time of switch closed detection\r\n detectTime=millis();\r\n \/\/call the closeGate subroutine\r\n closeGate();\r\n }\r\n \/\/ set t to the value of current time minus\r\n \/\/ switch detection time\r\n t= millis()-detectTime;\r\n \/\/check if it has been more than 4 seconds since the\r\n \/\/ last switch closed detection\r\n if (gateClosed==true &amp;&amp; t&gt;=3500 ){\r\n \/\/ call the openGate subroutine\r\n openGate();\r\n }\r\n}\r\n\r\n\/\/ subroutine to close the gates\r\nvoid closeGate(){\r\n \/\/ only run if the gate state is open\r\n if (gateClosed==false){\r\n \/\/set the LED \"on\" for visual feedback'\r\n digitalWrite(ledPin, HIGH);\r\n \/\/run the servo to the closed position\r\n for (pos = 80; pos &gt;= 0; pos -= 1) {\r\n myservo1.write(pos);\r\n oppos = (pos*1.375);\r\n myservo2.write(oppos);\r\n delay(30);\r\n }\r\n }\r\n \/\/set the gate state to closed\r\n gateClosed=true;\r\n}\r\n\r\n\/\/ subroutine to open the gates\r\nvoid openGate(){\r\n \/\/ only run if the gate state is open\r\n if (gateClosed==true){\r\n \/\/set the LED \"on\" for visual feedback'\r\n digitalWrite(ledPin, LOW);\r\n \/\/run the servo to the open position\r\n for (pos = 0; pos &lt;= 80; pos += 1) { \r\n myservo1.write(pos);\r\n oppos = (pos*1.375);\r\n myservo2.write(oppos);\r\n delay(30);\r\n }\r\n }\r\n \/\/set the gate state to open\r\n gateClosed=false;\r\n }<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been fiddling with this for quite some time. My initial experiments didn&#8217;t work out too well as the IR sensors didn&#8217;t like the waterproofing and the &#8220;stainless&#8221; steel wire I was using rusted out. But here&#8217;s some progress The servos are mounted in the bottom of a set of LGB crossing gates. The servos &hellip; <a href=\"https:\/\/danzbb.com\/index.php\/2016\/09\/12\/servo-activated-lgb-rail-crossing\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Servo Activated LGB Rail Crossing<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"https:\/\/danzbb.com\/index.php\/wp-json\/wp\/v2\/posts\/707"}],"collection":[{"href":"https:\/\/danzbb.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/danzbb.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/danzbb.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/danzbb.com\/index.php\/wp-json\/wp\/v2\/comments?post=707"}],"version-history":[{"count":3,"href":"https:\/\/danzbb.com\/index.php\/wp-json\/wp\/v2\/posts\/707\/revisions"}],"predecessor-version":[{"id":711,"href":"https:\/\/danzbb.com\/index.php\/wp-json\/wp\/v2\/posts\/707\/revisions\/711"}],"wp:attachment":[{"href":"https:\/\/danzbb.com\/index.php\/wp-json\/wp\/v2\/media?parent=707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/danzbb.com\/index.php\/wp-json\/wp\/v2\/categories?post=707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/danzbb.com\/index.php\/wp-json\/wp\/v2\/tags?post=707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}