| 
View
 

servo_180_rotation

Page history last edited by Kristin Lucas 17 years, 8 months ago
/*Servo Motor Demo
* ------------
* Rotates servo through 180 degrees, using "servoPulse" function 
* adapted from "Temporary Servo Function"  by Tom Igoe and Jeff Gray
*/
 
 
int servoPin = 9;            //  servo connected to digital pin 9
int myAngle;                 // angle of the servo (roughly in degrees) 0 - 180
int pulseWidth;              // function variable
int ledPin = 13;                // LED connected to digital pin 13
  
void setup()
{
 pinMode(2, OUTPUT);      // sets pin D9 as output
 pinMode(ledPin, OUTPUT);      // sets the digital pin as output
}
  
void servoPulse(int servoPin, int myAngle)     // servo function
{                                              // this is a function for determining our pulsewidth for the servo
 pulseWidth = (myAngle * 6) + 320;            // this determines our delay below (for a standard servo's pot)
 digitalWrite(servoPin, HIGH);                // sets servo high (on)
 delayMicroseconds(pulseWidth);               // wait a very small amount (determined by pulsewidth)
 digitalWrite(servoPin, LOW);                 // sets servo low (off)
 delay(20);                                   // refresh cycle of typical servos (20 ms)
}
  
void loop() {
  // cycle through every angle (rotate the servo 180 slowly)
  for (myAngle=0; myAngle<=180; myAngle++) {
    digitalWrite(ledPin, HIGH);   // sets the LED on
    servoPulse(servoPin, myAngle);
    //delay(100);
    digitalWrite(ledPin, LOW);    // sets the LED off
    servoPulse(servoPin, myAngle);
    delay(500);
    digitalWrite(ledPin, HIGH);   // sets the LED on
    servoPulse(servoPin, myAngle);
    //delay(100);
    digitalWrite(ledPin, LOW);    // sets the LED off
    servoPulse(servoPin, myAngle);
    delay(500);
     digitalWrite(ledPin, HIGH);   // sets the LED on
    servoPulse(servoPin, 0);
    //delay(100);
    digitalWrite(ledPin, LOW);    // sets the LED off
    servoPulse(servoPin, 180);
    delay(500);
     digitalWrite(ledPin, HIGH);   // sets the LED on
    servoPulse(servoPin, 0);
   // delay(100);
     digitalWrite(ledPin, LOW);   // sets the LED on
    servoPulse(servoPin, 90);
    delay(1000);

Comments (0)

You don't have permission to comment on this page.