Sweep Example Issues

Hey there. So, I’m very new to the world of Arduinos.

I’m currently in a Controls class in college and we have a project due in 6 weeks. Essentially we have to make a light detecting satellite utilizing a motor and a 5" radius disk, and a laser pointer. The disc has to rotate anywhere between 0-180 degrees and has to point a laser at a target in line with the light. We’ve got the Arduino Uno Rev 3 Ultimate Starter Kit, a standard RadioShack servo (4.8-6.0V), a RadioShack LDR pack, and an external battery pack (4 AA’s).

So, as practice, we’ve been trying to set up examples that we believe will be useful. We’ve tried quite a few different servo codes and none of them have worked. We need a consistent fluent motion and we’ve been getting a step-like response. For example, on the Sweep Example that’s supposed to have the servo oscillate between 0-180 degrees, the servo is starting at 0 degrees, stepping slowly till it reaches 180 degrees and stopping. This happens when the power to the servo is hooked up to the Arduino. When the power to the servo is hooked up to the battery pack, it doesn’t do anything. It only moves when the battery pack is first hooked up. Our professor says we need to hook up a spring to the disc, giving the servo a resistance, but I’m not sure that’s the solution.

I’m really stumped. Everyone in my group knows very little about coding and electrical hardware. We’ve got about six weeks till we need a working “satellite” and are trying to learn as much as possible now so that we can have a halfway decent project when it’s due. Thank you for your help.

/* Sweep
 by BARRAGAN <http://barraganstudio.com> 
 This example code is in the public domain.

 modified 8 Nov 2013
 by Scott Fitzgerald
 http://arduino.cc/en/Tutorial/Sweep
*/ 

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // twelve servo objects can be created on most boards
 
int pos = 0;    // variable to store the servo position 
 
void setup() 
{ 
  myservo.attach(9);  // 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>=0; 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 
  } 
}

If it's behaving differently under battery power than when powered by the usb port, I'd imagine that it's a power issue.

What kind of AAs are you using. If you can lay your hands on a lipo pack I think you'd find a significant improvement on performance.

The servo should NOT be powered from the Arduino 5v pin. It should have its own power supply (perhaps 4xAA NimH batteries or a 5v or 6v wall wart with at least 1 amp output - more would be better). An important issue is to ensure the servo power supply and the Arduino have a common GND connection.

You may find that using servo.writeMicroseconds() rather than servo.write() gives you finer control.

...R

Robin2: The servo should NOT be powered from the Arduino 5v pin. It should have its own power supply (perhaps 4xAA NimH batteries or a 5v or 6v wall wart with at least 1 amp output - more would be better). An important issue is to ensure the servo power supply and the Arduino have a common GND connection.

You may find that using servo.writeMicroseconds() rather than servo.write() gives you finer control.

...R

The servo is currently connected to the battery pack with 4 Alkaline batteries. How do I ensure the Arduino and the battery pack have a common ground? Do I run a cable going from GND to the ground cable on my battery pack?

When I replaced servo.write() with servo.writeMicroseconds() I got an error message saying, "no matching function for call Servo::writeMircroseconds()."

So I hooked up the ground front the Arduino to the battery pack and now it’s sweeping. However, you can still notice a little step or delay during certain parts of the sweep. I’m going to mess with the code a little bit (like changing the angle and delay. Do you guys think I need better batteries?

Shortening the delay improved the Sweep.

However, you can still notice a little step or delay during certain parts of the sweep.

All servos may not be able to move full range in both directions due to manufacturing inaccuracies. You will need to test your servo to find the command limits of your particular servo. Below is some servo test code that includes most of the functions in the servo library.

// zoomkat 12-25-13 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// Send an a to attach servo or d to detach servo
// for IDE 1.0.5 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.

#include <Servo.h> 
String readString; //String captured from serial port
Servo myservo;  // create servo object to control a servo 
int n; //value to write to servo

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(1500); //set initial servo position if desired
  myservo.attach(7, 500, 2500);  //the pin for the servo control, and range if desired
  Serial.println("servo all-in-one test code 12-25-13"); // so I can keep track of what is loaded
  Serial.println();
}

void loop() {
  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string 

      // attach or detach servo if desired
    if (readString == "d") { 
      myservo.detach(); //detach servo
      Serial.println("servo detached");
      goto bailout; //jump over writing to servo
    }
    if (readString == "a") {
      myservo.attach(7); //reattach servo to pin 7
      Serial.println("servo attached");
      goto bailout;
    }    

    n = readString.toInt();  //convert readString into a number

    // auto select appropriate value
    if(n >= 500)
    {
      Serial.print("writing Microseconds: ");
      Serial.println(n);
      myservo.writeMicroseconds(n);
    }
    else
    {   
      Serial.print("writing Angle: ");
      Serial.println(n);
      myservo.write(n); 
    }

bailout: //reenter code loop
    Serial.print("Last servo command position: ");    
    Serial.println(myservo.read());
    Serial.println();
    readString=""; //empty for next input
  }
}