{"id":382,"date":"2013-07-25T08:53:50","date_gmt":"2013-07-25T08:53:50","guid":{"rendered":"http:\/\/blog.daft-ideas.co.uk\/?p=382"},"modified":"2013-07-25T08:53:50","modified_gmt":"2013-07-25T08:53:50","slug":"arduino-controlling-servos","status":"publish","type":"post","link":"https:\/\/blog.daft-ideas.co.uk\/2013\/07\/25\/arduino-controlling-servos\/","title":{"rendered":"Arduino: Controlling Servos"},"content":{"rendered":"
Last Sunday, Matt came to visit. He brought his MAN TIN along, which is filled with micro controllers and breadboards and miscellaneous wires and a machine that goes “Ping”.<\/p>\n
We’ve not done any UAV work for a very long time, in fact the last time we did any was last Autumn. This has actually been my favourite session to date, because we made things move!<\/p>\n
We’re going to be using servos to drive our control surfaces on the UAV, and this was a sort of Proof of Concept in making the things work.<\/p>\n
We built the board, and hooked up the servos. The arduino program actually has some sample code for servos built in, so, not knowing how they work, we just hooked up that sample code.<\/p>\n
All it does is make the servo rotate 180 degrees in one direction, then 180 degrees in the other direction, over and over again.<\/p>\n
Here’s the code from the Arduino website. It’s not identical to ours, because we changed the pins, and then experimented with the speeds a bit. The speed was easy enough to alter: just change the delay between moves. <\/p>\n
\r\n\r\n\/\/ Sweep\r\n\/\/ by BARRAGAN <http:\/\/barraganstudio.com>\r\n\/\/ This example code is in the public domain.\r\n\r\n\r\n#include <Servo.h>\r\n\r\nServo myservo;\u00a0\u00a0\/\/ create servo object to control a servo\r\n\/\/ a maximum of eight servo objects can be created\r\n\r\nint\u00a0pos\u00a0=\u00a00;\u00a0\u00a0 \u00a0\/\/ variable to store the servo position\r\n\r\nvoid\u00a0setup()\r\n{\r\nmyservo.attach(9);\u00a0\u00a0\/\/ attaches the servo on pin 9 to the servo object\r\n}\r\n\r\n\r\nvoid\u00a0loop()\r\n{\r\nfor(pos\u00a0=\u00a00;\u00a0pos\u00a0<\u00a0180;\u00a0pos\u00a0+=\u00a01)\u00a0\u00a0\/\/ goes from 0 degrees to 180 degrees\r\n{\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/ in steps of 1 degree\r\nmyservo.write(pos);\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/ tell servo to go to position in variable 'pos'\r\ndelay(15);\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ waits 15ms for the servo to reach the position\r\n}\r\nfor(pos\u00a0=\u00a0180;\u00a0pos>=1;\u00a0pos-=1)\u00a0\u00a0 \u00a0\u00a0\/\/ goes from 180 degrees to 0 degrees\r\n{\r\nmyservo.write(pos);\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/ tell servo to go to position in variable 'pos'\r\ndelay(15);\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\/\/ waits 15ms for the servo to reach the position\r\n}\r\n}\r\n\r\n<\/pre>\nHere’s a video of it actually working:<\/p>\n