Arduino Servo Code For a Robot


.





If you're a newbie in Arduino and would like to build and program robots, then this is the code for you. It is written for an Arduino board and it's purpose is to control servo motors. In the code are numbers, and I'll let you change them up and experiment with the code. Hopefully you'll learn something about programming after you use the code. Thanks for reading and click here for even more code relating to Arduino!
The code

/*
Arduino Servo Code
*/

#include <Servo.h>
Servo servoOne; // Define our Servo

void setup()
{
servoOne.attach(10); // servo on digital pin 10
}

void loop()
{
servoOne.write(45); // Turn Servo Left to 45 degrees
delay(1000); // Wait 1 second
servoOne.write(0); // Turn Servo Left to 0 degrees
delay(1000); // Wait 1 second
servoOne.write(90); // Turn Servo back to center position (90 degrees)
delay(1000); // Wait 1 second
servoOne.write(135); // Turn Servo Right to 135 degrees
delay(1000); // Wait 1 second
servoOne.write(180); // Turn Servo Right to 180 degrees
delay(1000); // Wait 1 second
servoOne.write(90); // Turn Servo back to center position (90 degrees)
delay(1000); // Wait 1 second
}

Adding more than one servo!


1. Define a second servo 
Servo servoTwo; // Define our Servo)

2. Attach it
servoTwo.attach(12); // servo on digital pin 12

3. Add it void loop
servoTwo.write(45);
delay(1000);

Removing/Editing Delays

1. Delete the "delay(1000); // Wait 1 second" in the code

2. Edit the number "(1000)" to whatever you want. 1000 equals one second.

Changing the degrees

1. Change the "(45)" to whatever angle you want in "servoOne.write(45); // Turn Servo Left to 45 degrees"

Even more code!

If you're interested in programming the servo with more extensive code, then check out the forums and ask for help there. If you need any further assistance on programming, feel free to contact me at the "contact" page!






Your Reply