go_away

Author Topic: Software Servo code. Want to use for() statements  (Read 1215 times)

0 Members and 1 Guest are viewing this topic.

Offline offyTopic starter

  • Supreme Robot
  • *****
  • Posts: 340
  • Helpful? 1
Software Servo code. Want to use for() statements
« on: March 21, 2009, 08:17:17 PM »
Ok, So I have software servo installed for my roboduino so it now works on ports 3,5,6,11 also. So I have my servo plugged in, typed up this code (going through tutorials and stuff) and it doesn't work. The only code I ever got working with softwareservo was the example code for it.

Code: [Select]
#include <SoftwareServo.h>
 
SoftwareServo myservo;
 
int pos = 0;
 
void setup()
{
  myservo.attach(3);  // attaches the servo on pin 9 to the servo object
}
 
 
void loop()
{
  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees
  {                                  // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
  {                               
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
 SoftwareServo::refresh();
}
« Last Edit: March 21, 2009, 08:24:09 PM by offy »

Offline Ro-Bot-X

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,431
  • Helpful? 25
  • Store: RoBotXDesigns.ca
Re: Software Servo code. Want to use for() statements
« Reply #1 on: March 21, 2009, 11:40:49 PM »
Well, let's see...

Enter first for loop. Pos is 0, write it to servo, wait 15 ms, increment pos, write it to servo, wait 15 ms, increment pos... and so on until pos is 180. Enter second for loop and decrement pos until 0. Refresh the servo.

In order for this code to work as espected, you MUST refresh the servo EVERY TIME you write a new position to it, then wait 15 ms. Why? because the library is made as "set and forget". That means if you don't write a new position and refresh, the servo will hold the curent position.
Check out the uBotino robot controller!

Offline offyTopic starter

  • Supreme Robot
  • *****
  • Posts: 340
  • Helpful? 1
Re: Software Servo code. Want to use for() statements
« Reply #2 on: March 22, 2009, 05:51:34 AM »
GREAT! Thank you so much. I was trying to get this working for a few days.

 


Get Your Ad Here

data_list