{"id":174,"date":"2014-12-16T15:47:20","date_gmt":"2014-12-16T23:47:20","guid":{"rendered":"http:\/\/thenscaler.com\/?page_id=174"},"modified":"2016-12-13T23:22:26","modified_gmt":"2016-12-14T07:22:26","slug":"turnout-control-with-arduino-servos","status":"publish","type":"page","link":"https:\/\/thenscaler.com\/?page_id=174","title":{"rendered":"Turnout Control with Arduino &#038; Servos"},"content":{"rendered":"<div id=\"attachment_213\" style=\"width: 610px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/11\/servo-installation-complete-top-view.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-213\" class=\"size-full wp-image-213\" src=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/11\/servo-installation-complete-top-view.jpg\" alt=\"Complete servo installation.\" width=\"600\" height=\"400\" srcset=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/11\/servo-installation-complete-top-view.jpg 600w, https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/11\/servo-installation-complete-top-view-300x200.jpg 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><p id=\"caption-attachment-213\" class=\"wp-caption-text\">Complete servo installation.<\/p><\/div>\n<p>[Since this was originally posted I&#8217;ve come up with a <a href=\"https:\/\/thenscaler.com\/?p=848\" target=\"_blank\">low-noise, low-profile side mounting method<\/a>. The mounting method does not change the programming required to control one, <a href=\"https:\/\/thenscaler.com\/?page_id=661\" target=\"_blank\">or more<\/a>, servos.]<\/p>\n<p>This is a\u00a0basic method for using an off-the-shelf micro servo as a turnout motor, in this case the<\/p>\n<div id=\"attachment_218\" style=\"width: 160px\" class=\"wp-caption alignright\"><a href=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/11\/SG90.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-218\" class=\"size-thumbnail wp-image-218\" src=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/11\/SG90-150x150.jpg\" alt=\"Tower Pro SG90\" width=\"150\" height=\"150\" srcset=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/11\/SG90-150x150.jpg 150w, https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/11\/SG90-300x300.jpg 300w, https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/11\/SG90-50x50.jpg 50w, https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/11\/SG90.jpg 500w\" sizes=\"auto, (max-width: 150px) 100vw, 150px\" \/><\/a><p id=\"caption-attachment-218\" class=\"wp-caption-text\">Tower Pro SG90<\/p><\/div>\n<p><a href=\"http:\/\/www.amazon.com\/exec\/obidos\/ASIN\/B00CCZ2CRA\/beaglebaybooks\" target=\"_blank\">Tower Pro SG90<\/a>\u00a0which can be purchased for $2 to $3 each.<\/p>\n<p>The first thing I do is prepare the servo by setting the position of the shaft to 90 degrees, the midpoint of its 180 degree total travel.\u00a0I do this by attaching the servo to an <a href=\"http:\/\/www.amazon.com\/exec\/obidos\/ASIN\/B00F6JCV20\/beaglebaybooks\" target=\"_blank\">Arduino Uno <\/a>and uploading this little sketch once to set the servo (for those who don&#8217;t know, &#8220;\/\/&#8221; signifies the start of\u00a0a comment ):<\/p>\n<pre>#include &lt;Servo.h&gt; \/\/ compiler directive to get the servo library\r\n\u00a0\r\nServo myservo;\u00a0 \/\/ create a servo object\r\n\/\/ from here on\u00a0use the\u00a0variable \"myservo\" to \r\n\/\/ access and control the servo object\r\nvoid setup() \/\/ the setup function is run once at boot\r\n{ \r\n\u00a0 myservo.attach(9);\u00a0 \/\/ attaches to the servo on pin 9\r\n\u00a0 myservo.write(90);  \/\/ set the servo shaft at 90 degrees\r\n} \r\n\u00a0\r\n\/\/ the loop function runs after setup until the board is powered off\u00a0\r\nvoid loop() \r\n{ \r\n\u00a0 \/\/ do nothing\r\n}<\/pre>\n<p>Here is the basic circuit for controlling a servo with an Arduino Uno, where the servo draws power from the Arduino. Servos can be independently powered, in which case only the signal wire and ground are connected to the Arduino.<\/p>\n<div id=\"attachment_214\" style=\"width: 483px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/11\/sweep_BB.png\" target=\"_blank\" rel=\"http:\/\/www.arduino.cc\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-214\" class=\"wp-image-214 size-full\" src=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/11\/sweep_BB.png\" alt=\"Basic Arduino\/servo circuit.\" width=\"473\" height=\"270\" srcset=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/11\/sweep_BB.png 473w, https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/11\/sweep_BB-300x171.png 300w\" sizes=\"auto, (max-width: 473px) 100vw, 473px\" \/><\/a><p id=\"caption-attachment-214\" class=\"wp-caption-text\">Basic Arduino\/servo circuit (from www.arduino.cc).<\/p><\/div>\n<p>The Arduino can <a title=\"Arduino Limitations\" href=\"http:\/\/playground.arduino.cc\/Main\/ArduinoPinCurrentLimitations\" target=\"_blank\">Source a maximum of\u00a0200 mA current, and can Sink a maximum of 400 mA current<\/a>.\u00a0As you connect and try to run more devices, you&#8217;ll get to those\u00a0limits quickly. In Model Railroad applications, given that we&#8217;re already routing\u00a0track power, routing a few amps of\u00a0+5 volt power to supply actuators like servos is a no-brainer for performance and system integrity.\u00a0\u00a0<strong>Whenever you use a separate power supply, the servo\u00a0ground has to connect to both the\u00a0power supply\u00a0ground and the\u00a0Arduino ground.<br \/>\n<\/strong><\/p>\n<p>I fabricate a simple mounting bracket with a small plywood plate and a couple of blocks. Now I mount the servo in the bracket, place the horn onto the shaft\u00a0in the position shown and then screw it down to the shaft.<\/p>\n<div id=\"attachment_205\" style=\"width: 610px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/11\/servo-in-bracket.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-205\" class=\"size-full wp-image-205\" src=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/11\/servo-in-bracket.jpg\" alt=\"Micro Servo with shaft\/horn set to 90 degrees\" width=\"600\" height=\"379\" srcset=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/11\/servo-in-bracket.jpg 600w, https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/11\/servo-in-bracket-300x189.jpg 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><p id=\"caption-attachment-205\" class=\"wp-caption-text\">Micro Servo with shaft\/horn set to 90 degrees<\/p><\/div>\n<p>Using a 1\/16&#8243; bit, I drill out a hole in the horn (usually the second hole from the end of the horn)\u00a0and a hole in the turnout actuator bar.\u00a0 Don&#8217;t over ream the holes; the soft, slightly flexible\u00a0plastic will provide a snug fit so you don&#8217;t have to use adhesives. Then, I slide a piece of 1\/16&#8243; brass rod through the horn to establish the location\u00a0for a hole in the mounting plate.<\/p>\n<div id=\"attachment_208\" style=\"width: 610px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/11\/servo-finding-center-with-rod.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-208\" class=\"size-full wp-image-208\" src=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/11\/servo-finding-center-with-rod.jpg\" alt=\"Establishing the location for the rod hole.\" width=\"600\" height=\"399\" srcset=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/11\/servo-finding-center-with-rod.jpg 600w, https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/11\/servo-finding-center-with-rod-300x199.jpg 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><p id=\"caption-attachment-208\" class=\"wp-caption-text\">Establishing the location for the rod hole.<\/p><\/div>\n<p>I mark and drill the hole in the base plate. I rock the bit along the axis the rod will move (left to right in the view\u00a0below)\u00a0to slightly elongate it, and give it a more hour-glass profile. This hole functions as a fulcrum for the rod.<\/p>\n<div id=\"attachment_207\" style=\"width: 610px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/11\/servo-bracket.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-207\" class=\"size-full wp-image-207\" src=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/11\/servo-bracket.jpg\" alt=\"Servo mounting bracket ready for use.\" width=\"600\" height=\"408\" srcset=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/11\/servo-bracket.jpg 600w, https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/11\/servo-bracket-300x204.jpg 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><p id=\"caption-attachment-207\" class=\"wp-caption-text\">Servo mounting bracket ready for use.<\/p><\/div>\n<p>I mount the servo below the turnout. For this demonstration, I used hot glue to mount the bracket to the underside of the demonstration layout segment; screws are a better idea for most situations, allowing you to build in some adjustability. With the turnout set in the straight-through position, I carefully thread the rod through the turnout actuator bar,\u00a0down through a prepared hole in the layout to the hole in the mounting plate and then\u00a0the servo horn. The brass rod is perpendicular to the horn at the 90 degree setting.\u00a0 Moving the servo horn tilts the rod, moving the turnout above to its divergent position.<\/p>\n<div id=\"attachment_209\" style=\"width: 610px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/11\/servo-threading-rod.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-209\" class=\"size-full wp-image-209\" src=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/11\/servo-threading-rod.jpg\" alt=\"Threading the rod down from the turnout to the servo.\" width=\"600\" height=\"387\" srcset=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/11\/servo-threading-rod.jpg 600w, https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/11\/servo-threading-rod-300x193.jpg 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><p id=\"caption-attachment-209\" class=\"wp-caption-text\">Threading the rod down from the turnout to the servo.<\/p><\/div>\n<p>At this point I test the servo and make any adjustments necessary for smooth operation. When I&#8217;m satisfied everything is right, I trim the rod to its final size.<\/p>\n<p>I&#8217;m planning to try music wire instead brass rod in the near future.\u00a0The problem with brass rod is that it is stiff, and the servo can get fussy at the end of movement ranges because there is no give. Music wire is like spring wire and should allow me to apply pressure at the ends of movement ranges without overtaxing the servo.\u00a0 I&#8217;ll update this page with the results of those tests.<\/p>\n<div id=\"attachment_211\" style=\"width: 610px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/11\/servo-mounted-and-ready.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-211\" class=\"size-full wp-image-211\" src=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/11\/servo-mounted-and-ready.jpg\" alt=\"Servo mounted and ready\" width=\"600\" height=\"337\" srcset=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/11\/servo-mounted-and-ready.jpg 600w, https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/11\/servo-mounted-and-ready-300x168.jpg 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><p id=\"caption-attachment-211\" class=\"wp-caption-text\">Servo mounted and ready<\/p><\/div>\n<div id=\"attachment_235\" style=\"width: 303px\" class=\"wp-caption alignright\"><a href=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/12\/Servo-Control-by-Button.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-235\" class=\"size-medium wp-image-235\" src=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/12\/Servo-Control-by-Button-293x300.jpg\" alt=\"Servo Control by Button\" width=\"293\" height=\"300\" srcset=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/12\/Servo-Control-by-Button-293x300.jpg 293w, https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/12\/Servo-Control-by-Button-50x50.jpg 50w, https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/12\/Servo-Control-by-Button.jpg 625w\" sizes=\"auto, (max-width: 293px) 100vw, 293px\" \/><\/a><p id=\"caption-attachment-235\" class=\"wp-caption-text\">Servo Control by Button<\/p><\/div>\n<p>The button takes power from the +5v board supply and, when the button is pushed, routes the power to a designated pin, putting the pin in a HIGH state. On the output side of the button a pull-down resistor routes stray power to ground to guarantee that the pin is in a LOW state whenever the button is not pushed.<\/p>\n<p>Here is\u00a0a simple sketch\u00a0to control a servo\u00a0and have it\u00a0move over about 2 seconds every time a button is pressed. The straight position is always 90 degrees because of the way I install the servo.\u00a0 The divergent angle depends on how the servo was installed in relation to the turnout&#8211; it will either be a larger angle in the 110 &#8211; 120 degree range, or a smaller angle in the 60-70 degree range.\u00a0 With practice and consistent placement of servos, they can all be the same; but if not, storing unique settings for each servo is not difficult.<\/p>\n<pre>#include &lt;Servo.h&gt;\r\n\r\n\/\/ constant variables used to set servo angles, in degrees\r\nconst int straight = 90; \r\nconst int divergent = 110;\r\n\r\n\/\/ constant variables holding the ids of the pins we are using\r\nconst int buttonpin = 8;\r\nconst int servopin = 9;\r\n\r\n\/\/ servo movement step delay, in milliseconds\r\nconst int step_delay = 70;\r\n\r\n\/\/ create a servo object\r\nServo myservo;\u00a0 \r\n\u00a0\r\n\/\/ global variables to store servo position\r\nint pos = straight; \/\/ current\r\nint old_pos = pos; \/\/ previous\r\n\r\nvoid setup() \r\n{ \r\n\u00a0 \/\/ set the mode for the digital pins in use\r\n\u00a0 pinMode(buttonpin, INPUT);\r\n\u00a0\u00a0 \r\n\u00a0 \/\/ setup the servo\r\n\u00a0 myservo.attach(servopin);\u00a0 \/\/ attach to the servo on pin 9\r\n\u00a0 myservo.write(pos); \/\/ set the initial servo position\r\n}\r\n\r\nvoid loop() \r\n{ \r\n\u00a0\/\/ start each iteration of the loop by reading the button\r\n\u00a0\/\/ if the button is pressed (reads HIGH), move the servo\r\n\u00a0 int button_state = digitalRead(buttonpin);\r\n\u00a0 if(button_state == HIGH){\r\n\u00a0\u00a0\u00a0 \r\n\u00a0\u00a0\u00a0 old_pos = pos;\u00a0\u00a0 \/\/ save the current position\r\n\u00a0\u00a0\u00a0 \r\n\u00a0\u00a0\u00a0 \/\/ Toggle the position to the opposite value\r\n\u00a0\u00a0\u00a0 pos = pos == straight ? divergent: straight;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \r\n\u00a0\u00a0\u00a0 \/\/ Move the servo to its new position\r\n\u00a0\u00a0\u00a0 if(old_pos &lt; pos){\u00a0\u00a0 \/\/ if the new angle is higher\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ increment the servo position from oldpos to pos\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 for(int i = old_pos + 1; i &lt;= pos; i++){\u00a0 \r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 myservo.write(i); \/\/ write the next position to the servo\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 delay(step_delay); \/\/ wait\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 } else {\u00a0 \/\/ otherwise the new angle is equal or lower\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ decrement the servo position from oldpos to pos\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 for(int i = old_pos - 1; i &gt;= pos; i--){ \r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 myservo.write(i); \/\/ write the next position to the servo\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 delay(step_delay); \/\/ wait\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 \r\n\u00a0 } \r\n}\/\/ end of loop<\/pre>\n<p>Finally, we can add one more refinement and have the Arduino feedback position status via two pins that we can use to power leds at the control panel. The circuit looks like this:<\/p>\n<div id=\"attachment_234\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/12\/Servo-Control-by-Button-with-leds.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-234\" class=\"size-medium wp-image-234\" src=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/12\/Servo-Control-by-Button-with-leds-300x213.jpg\" alt=\"Servo Control with LED Feedback\" width=\"300\" height=\"213\" srcset=\"https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/12\/Servo-Control-by-Button-with-leds-300x213.jpg 300w, https:\/\/thenscaler.com\/wp-content\/uploads\/2014\/12\/Servo-Control-by-Button-with-leds.jpg 912w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-234\" class=\"wp-caption-text\">Servo Control with LED Feedback<\/p><\/div>\n<p>To update the sketch, we&#8217;ll add variables for the led pins and add commands to turn the leds on and off.<\/p>\n<pre>#include &lt;Servo.h&gt;\r\n\r\n\/\/ constant variables used to set servo angles, in degrees\r\nconst int straight = 90; \r\nconst int divergent = 110;\r\n\r\n\/\/ constant variables holding the ids of the pins we are using\r\n<span style=\"color: #0000ff;\">const int divergent_led = 6;<\/span>\r\n<span style=\"color: #0000ff;\">const int straight_led = 7;<\/span>\r\nconst int buttonpin = 8;\r\nconst int servopin = 9;\r\n\r\n\/\/ servo movement step delay, in milliseconds\r\nconst int step_delay = 70;\r\n\r\n\/\/ create a servo object\r\nServo myservo;\u00a0 \r\n\u00a0\r\n\/\/ global variables to store servo position\r\nint pos = straight; \/\/ current\r\nint old_pos = pos; \/\/ previous\r\n\r\nvoid setup() \r\n{ \r\n\u00a0 \/\/ set the mode for the digital pins in use\r\n\u00a0 pinMode(buttonpin, INPUT);\r\n\u00a0 pinMode(straight_led, OUTPUT);\r\n\u00a0 pinMode(divergent_led, OUTPUT);\r\n\u00a0 \r\n\u00a0 \/\/ setup the servo\r\n\u00a0 myservo.attach(servopin);\u00a0 \/\/ attach to the servo on pin 9\r\n\u00a0 myservo.write(pos); \/\/ set the initial servo position\r\n\u00a0 \r\n\u00a0 \/\/ set initial led states\r\n\u00a0<span style=\"color: #0000ff;\">  digitalWrite(straight_led, HIGH);<\/span>\r\n<span style=\"color: #0000ff;\">\u00a0   digitalWrite(divergent_led, LOW);<\/span>\r\n}\r\n\r\nvoid loop() \r\n{ \r\n\u00a0\/\/ start each iteration of the loop by reading the button\r\n\u00a0\/\/ if the button is pressed (reads HIGH), move the servo\r\n\u00a0 int button_state = digitalRead(buttonpin);\r\n\u00a0 if(button_state == HIGH){\r\n\u00a0\u00a0\u00a0 \/\/ turn off the lit led\r\n\u00a0\u00a0\u00a0 <span style=\"color: #0000ff;\">if(pos == straight){<\/span>\r\n<span style=\"color: #0000ff;\">\u00a0\u00a0\u00a0\u00a0\u00a0         digitalWrite(straight_led, LOW);<\/span>\r\n<span style=\"color: #0000ff;\">\u00a0\u00a0\u00a0       } else {<\/span>\r\n<span style=\"color: #0000ff;\">\u00a0\u00a0\u00a0\u00a0\u00a0         digitalWrite(divergent_led, LOW);<\/span>\r\n<span style=\"color: #0000ff;\">\u00a0\u00a0\u00a0       }<\/span>\r\n\u00a0\u00a0\u00a0 old_pos = pos;\u00a0\u00a0 \/\/ save the current position\r\n\u00a0\u00a0\u00a0 \r\n\u00a0\u00a0\u00a0 \/\/ Toggle the position to the opposite value\r\n\u00a0\u00a0\u00a0 pos = pos == straight ? divergent: straight;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \r\n\u00a0\u00a0\u00a0 \/\/ Move the servo to its new position\r\n\u00a0\u00a0\u00a0 if(old_pos &lt; pos){\u00a0\u00a0 \/\/ if the new angle is higher\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ increment the servo position from oldpos to pos\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 for(int i = old_pos + 1; i &lt;= pos; i++){\u00a0 \r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 myservo.write(i); \/\/ write the next position to the servo\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 delay(step_delay); \/\/ wait\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 } else {\u00a0 \/\/ otherwise the new angle is equal or lower\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ decrement the servo position from oldpos to pos\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 for(int i = old_pos - 1; i &gt;= pos; i--){ \r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 myservo.write(i); \/\/ write the next position to the servo\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 delay(step_delay); \/\/ wait\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 \/\/ turn on the appropriate LED.\r\n\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\"> if(pos == straight){<\/span>\r\n<span style=\"color: #0000ff;\">\u00a0\u00a0\u00a0\u00a0\u00a0       digitalWrite(straight_led, HIGH);<\/span>\r\n<span style=\"color: #0000ff;\">\u00a0\u00a0    \u00a0 } else {<\/span>\r\n<span style=\"color: #0000ff;\">\u00a0\u00a0\u00a0\u00a0\u00a0       digitalWrite(divergent_led, HIGH);<\/span>\r\n<span style=\"color: #0000ff;\">\u00a0\u00a0\u00a0    }<\/span>\r\n\u00a0 } \r\n}\/\/ end of loop<\/pre>\n<p>To control multiple servos with one Arduino, your sketch\u00a0would need variables for each servo to hold its pin id and\u00a0unique divergent angle. More advanced programmers will want to create something like an <a href=\"http:\/\/forum.arduino.cc\/index.php?topic=43587.0\" target=\"_blank\">array of data structures<\/a>\u00a0to organize pertinent data about the servos.<\/p>\n<p>Now lets put it all together:<\/p>\n<p><iframe loading=\"lazy\" src=\"\/\/www.youtube.com\/embed\/xamBnlvLJ9Q?feature=player_detailpage\" width=\"640\" height=\"360\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p>NOTE: The delay() function is used a lot in small demonstration sketches to control timing.\u00a0 The problem with delay is that it throws the board into a do-nothing state that prevents anything else from happening. In more complex sketches it is often advisable to avoid delay() and use other methods to meter actions across multiple controller cycles. In this case, be aware that the board is tied up while the servo is in motion.<\/p>\n<hr \/>\n<p>&nbsp;<\/p>\n<p>For more on avoiding delay() and controlling multiple turnouts, see <a href=\"https:\/\/thenscaler.com\/?page_id=661\">Multiple Turnouts and Other Multitasking<\/a>.<\/p>\n<hr \/>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[Since this was originally posted I&#8217;ve come up with a low-noise, low-profile side mounting method. The mounting method does not change the programming required to control one, or more, servos.] This is a\u00a0basic method for using an off-the-shelf micro servo as a turnout motor, in this case the Tower Pro SG90\u00a0which can be purchased for [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":157,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-174","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/thenscaler.com\/index.php?rest_route=\/wp\/v2\/pages\/174","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thenscaler.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/thenscaler.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/thenscaler.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/thenscaler.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=174"}],"version-history":[{"count":32,"href":"https:\/\/thenscaler.com\/index.php?rest_route=\/wp\/v2\/pages\/174\/revisions"}],"predecessor-version":[{"id":1440,"href":"https:\/\/thenscaler.com\/index.php?rest_route=\/wp\/v2\/pages\/174\/revisions\/1440"}],"up":[{"embeddable":true,"href":"https:\/\/thenscaler.com\/index.php?rest_route=\/wp\/v2\/pages\/157"}],"wp:attachment":[{"href":"https:\/\/thenscaler.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}