Control a servo using the Arduino.
DO NOT EVER CONNECT BATTERY STRAIGHT TO ARDUINO. Don’t learn this the hard way like me .___.
#include <Servo.h>
Servo myservo;
void setup() {
myservo.attach(2);
myservo.write(180);
}
void loop() { }
/* Import the Servo library. Libraries provide extra functionality, e.g. working with hardware, for us in Arduino programs (aka sketches). Now we can use the attach() and write() functions to control our servos. */
#include <Servo.h>
Servo myservo; // Create servo object to control a servo.
// A max of 8 servos can be created
/* Arduino code requires a setup() and a loop() block. Code in the setup() block is run once, code in the loop() block is run continuously thereafter. */
void setup {
// Attach()- Let the Arduino know which pin to write to (the pin the real-life servo is connected to)
myservo.attach(2);
// See Arduino documentation for ServoWrite (quoted below, after this)
myservo.write(180);
}
// loop() block here, even if its empty, because Arduino code requires it
void loop() { }
play |>
and the upload =>|
buttons. Serial port ____ not found
or Problem uploading to board
Try again with a different port
"Tools > Serial Port > COM2
(or /dev/ttyUSB0
for linux)upload
button againQuest completed. Yay!
Now go forth and build your robot army and take over the world! *__* bwahaha
Comments? Suggestions? Did this tutorial take too long? Was it too easy? Do the images fail? Let me know.
~nouyang@mit.edu