| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

Back and Forth

Page history last edited by PBworks 16 years, 8 months ago

Back and Forth

This is the example from the programming notebook that uses a for loop to first increment the angle from 10 to 170 and then decrement from 170 to 10. It runs reasonably slow because it waits 20ms in between each degree. How could this be speed up?

 

/*  Simple Servo Output

    Simply rotates a servo back and forth

    PG38 Programming Notebook

*/

int servoPin = 2;   // servo connected to digital pin 9

int myAngle;        // angle of the servo roughly 0-180

int pulseWidth;     // servoPulse function variable

 

void setup()

{

  pinMode(servoPin, OUTPUT);   // sets pin 2 as output

}

 

void servoPulse(int servoPin, int myAngle)    

{      

  pulseWidth = (myAngle * 10) + 600;  // determines delay  

  digitalWrite(servoPin, HIGH);       // set servo high

  delayMicroseconds(pulseWidth);      // micro pause

  digitalWrite(servoPin, LOW);        // set servo low

  delay(20);                          // refresh cycle

}

 

void loop()

{

  // servo starts at 10 deg and rotates to 170 deg

  for (myAngle=10; myAngle<=170; myAngle++)   

  {

    servoPulse(servoPin, myAngle);

  }

  // servo starts at 170 deg and rotates to 10 deg

  for (myAngle=170; myAngle>=10; myAngle--)   

  {

    servoPulse(servoPin, myAngle);

  }

}

Comments (0)

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