quick servo questions

Hello,
I have a 360 degree full rotation servo from SparkFun. I have a range finer sensor in front of it and I’m just trying to get it to turn to one angle when someone steps in front of it and then back to its beginning angle when the person leaves. So I’m noticing that with these 360 degree servos…when you do myservo.write(x);, the number you write doesn’t give an angle but rather a speed and direction. When I type in 90, the servo is still, if the number is below 90 it goes in one direction, the further from 90 the faster…when the number is between 90 and 180 it goes in the opposite direction…the further from 90 the faster.

So my question is…can I just get it to go in specific angles? So if I want the servo at a 90 degree angle, switch to a 45 and then back to 90…can I do that? It seems difficult to get it back to a specific angle…do I just have the wrong servo? Any way to do this with a 360? Thanks for any help!

This is the code I have now…

#include <Servo.h> 
Servo myservo;  
int potpin = 0;  
int val;    
boolean moving=false;
boolean needtomoveback=false;

void setup() 
{ 
  myservo.attach(9); 
  Serial.begin(9600);
} 

void loop() 
{ 
  val = analogRead(potpin);  
  Serial.println("RADAR:");  
  Serial.println(val);
  Serial.println("SERVO ANGLE:");
  int servoangle = myservo.read();
  Serial.println(servoangle);
  myservo.write(90);                 
  delay(15);      

  if ((val>50)&&(moving==true)&&(needtomoveback==false)) { 
    myservo.write(90);
    moving = false;
  }
  if ((val<50)&&(moving==false)) {
    myservo.write(40); //was 40
    delay(500);
    myservo.write(90);
    moving = true;
    needtomoveback=true;
  }
  if ((val>50)&&(needtomoveback==true)) {
    myservo.write(120);
    delay(500);
    needtomoveback=false;
  }
}

I have a feeling you have the wrong servo for the task, as the 360 degree servos are designed to be used as lets say a wheel driver, an wheels dont care about angles, just speed and direction. You have two options here, One is to make a little rotary encoder to read the angle off, (the elite option that displays your manliness to all) or you can buy another non 360 servo.

So I’m noticing that with these 360 degree servos…when you do myservo.write(x);, the number you write doesn’t give an angle but rather a speed and direction. When I type in 90, the servo is still, if the number is below 90 it goes in one direction, the further from 90 the faster…when the number is between 90 and 180 it goes in the opposite direction…the further from 90 the faster.

So my question is…can I just get it to go in specific angles? So if I want the servo at a 90 degree angle, switch to a 45 and then back to 90…can I do that? It seems difficult to get it back to a specific angle…do I just have the wrong servo? Any way to do this with a 360? Thanks for any help!

If you are using continous rotation servos for driving the wheels, you will need to calibrate your code to accomidate the no movement pulse value for the servos. There is no angle positioning with continous rotation servos. You will also probably need to use the writeMicroseconds to have any reasonable control over the servos. Below is some test code I use with the serial monitor to setup two continous rotation servos I have.

// zoomkat 11-22-10 serial servo (2) test
// for writeMicroseconds, use a value like 1500
// for IDE 0019 and later
// Powering a servo from the arduino usually DOES NOT WORK.
// two servo setup with two servo commands
// send eight character string like 15001500 or 14501550

#include <Servo.h> 
String readString, servo1, servo2;
Servo myservo1;  // create servo object to control a servo 
Servo myservo2;

void setup() {
  Serial.begin(9600);
  myservo1.attach(6);  //the pin for the servo control 
  myservo2.attach(7);
  Serial.println("servo-test-21"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(10);  
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    } 
  }

  if (readString.length() >0) {
      Serial.println(readString); //see what was received
      
      // expect a string like 07002100 containing the two servo positions      
      servo1 = readString.substring(0, 4); //get the first four characters
      servo2 = readString.substring(4, 8); //get the next four characters 
      
      Serial.println(servo1);  //print to serial monitor to see results
      Serial.println(servo2);
      
      int n1; //declare as number  
      int n2;
      
      char carray1[6]; //magic needed to convert string to a number 
      servo1.toCharArray(carray1, sizeof(carray1));
      n1 = atoi(carray1); 
      
      char carray2[6];
      servo2.toCharArray(carray2, sizeof(carray2));
      n2 = atoi(carray2); 
      
      myservo1.writeMicroseconds(n1); //set servo position 
      myservo2.writeMicroseconds(n2);
    readString="";
  } 
}