Servo Motor Code/Set Up

My group and myself are looking for help with our foundation digital design class. We are required to program a stuffed animal to interact with the audience and we are having trouble making a code and wiring up to a bread board for a servo motor. If anyone has any tips it would be great!

1 Like

You mean like an R/C servo?

There's a library handles all of that.

I'm a newbe but find servo action very rewarding ... here's a snipit of early code that as I remember works
the IR sensor is from Pololu.com > Sharp GP2Y0A02

//two position servo
#include <Servo.h>
#define SENSOR 5
int IRvalue = 0;
Servo myservo; // create servo object
void setup()
{
pinMode (2 , OUTPUT); // sensor active indication LED
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo.writeMicroseconds(600);// assure servo starts in correct position
delay(400);
// Serial.begin(9600); used to see actual SENSOR values
}

void loop()
{
delay (4000);
IRvalue = analogRead (SENSOR)/100;// look at sensor and pause ... divide X 100 for simpler numeric reference
// Serial.print("IRvalue ="); // text
// Serial.println(IRvalue);// IRvalue with line feed
// for testing
if (IRvalue < 8){ // sensor normally 10 delay(2000);// delay before action
delay(200);

digitalWrite(2,HIGH); // light up led indicating sensor activated
}
else{
delay (200);
digitalWrite(2,LOW); // no sense active
}
if (IRvalue < 7) { //sensor output went below 10
delay(2200); // wait for the servo to reach the position
myservo.writeMicroseconds(1200); // tell servo to go to active position in'
// be carefull to not enter inappropriate values as it may cause servo to jamb
//some do some don't
delay(1200); // wait for the servo to reach the position
myservo.writeMicroseconds(600); // tell servo to return to passive position
delay(100);
}
}

The smiley is a way of telling you to post code using the # icon on the editor's toolbar.