Wow- it’s been a long while since my last post, but I’m back to present the first post in a new series: hello.build. This is where we’ll track our progress on some hardware projects we have in the pipeline. More on that soon…
I recently picked up an Arduino, which is an open source microcontroller platform created to make it easy for people to enter into the world of physical computing. Just do a quick You Tube search for Arduino, and you’ll come across tons of really great projects. With a few easy-to-follow tutorials from Make, you can really do a lot of cool stuff.
Tonight, my goal was to get a servo motor up and running, controlling its position with a potentiometer. Here’s the code:
—————
//Hello World, Servo Style!
#define SERVO 9 //The servo is hooked up to PWM output 9
int pot; //Pot is an analog potentiometer input
int servo_position; //This is what value is going to get sent to the servo
void setup(){
pinMode(SERVO, OUTPUT); //Set the servo as an output
Serial.begin(9600); //Open up a Serial Communique so we can see data
}
void loop(){
pot = analogRead(0); //Read in the value of the knob
servo_position = pot/4; //The analog input reads between 0 and 1023, and the PWM
//output needs values between 0 and 255, so divide by 4.
Serial.println(pot); //let’s print what we read in to the computer screen
Serial.println(servo_position); //lets see what value we are going to send out
analogWrite(SERVO, servo_position); //send out the position to the servo
}
—————
And here’s a video of the result: