Servo Motors Won't Budge in Project

Hello,

I am working on a copying arm. This includes a control arm and a copy arm. The user manipulates the control arm through potentiometer joints, and the servos attached to the joints on the copying arm mimics that manipulation. I am working on the guts of the project, which means that I am connecting the potentiometers and servo motors to the arduino. I have two servo motors - a radioshack micro servo motor and a parallax standard servo connected to pins 6 and 9 with an external power source of four AA batteries. The potentiometers work fine, but the servo motors have the problem. When I plug in the arduino and upload the sketch, the motors start moving. A few seconds later, they freeze and start to vibrate. What is going on? How can I fix this?

If this is any help, here is my code.

#include <Servo.h>

Servo elbow; //create servo object elbow
Servo hand; //"""""""""""""""""""""hand
int elbow_j = 0; //sets potentionmeter 1 to 0
int hand_j = 1; //’’’’’’’’’’’’’’’’’’’'2 to 1
int degreeOne;
int degreeTwo;

void setup()
{
elbow.attach(9);
hand.attach(6);
}

void loop()
{
degreeOne = analogRead(elbow_j);
degreeTwo = analogRead(hand_j);
degreeOne = map(degreeOne, 0, 1023, 0 , 179);
degreeTwo = map(degreeTwo, 0, 1023, 0 , 179);
elbow.write(degreeOne);
hand.write(degreeTwo);
delay(15);
}

Thanks in advance.

Hi, Some servos can not actually go all the way to "zero" or "180".

Does the example in the Arduino IDE examples work?? Or try the two Servo examples on the http:/ArduinoInfo.Info WIKI here: http://arduino-info.wikispaces.com/YourDuinoStarterSoftwareSketches

Try ServoSweep first. ServoPotPosition is similar to what you want to do.

You will notice that these examples do this:

#define ServoMIN  20  // Don't go to very end of servo travel
#define ServoMAX  160 // which may not be all the way from 0 to 180.

Get one servo working right first... Then the other.

Thanks! I will try that!

Some servo/pot test code that might be worth trying. Edit: servo attach copy paste error corrected.

//zoomkat multi pot/servo test 3-23-13
//includes dead band for testing and limit servo hunting
//view output using the serial monitor

#include <Servo.h> 
Servo myservo1;  //declare servos
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;

int potpin1 = 0;  //analog input pin A0
int potpin2 = 1;
int potpin3 = 2;
int potpin4 = 3;
int potpin5 = 4;

int newval1, oldval1;  //pot input values
int newval2, oldval2;
int newval3, oldval3;
int newval4, oldval4;
int newval5, oldval5;

void setup() 
{
  Serial.begin(9600);  
  myservo1.attach(2);  
  myservo2.attach(3);
  myservo3.attach(4);
  myservo4.attach(5);
  myservo5.attach(6);
  Serial.println("testing multi pot servo");  
}

void loop()
{ 
  newval1 = analogRead(potpin1);           
  newval1 = map(newval1, 0, 1023, 0, 179); 
  if (newval1 < (oldval1-2) || newval1 > (oldval1+2)){ //dead band 
    myservo1.write(newval1); //position the servo
    Serial.print("1- ");
    Serial.println(newval1); //print the new value for testing 
    oldval1=newval1; //set the current old value
  }

  newval2 = analogRead(potpin2);
  newval2 = map(newval2, 0, 1023, 0, 179);
  if (newval2 < (oldval2-2) || newval2 > (oldval2+2)){  
    myservo2.write(newval2);
    Serial.print("2- ");    
    Serial.println(newval2);
    oldval2=newval2;
  }

  newval3 = analogRead(potpin3);           
  newval3 = map(newval3, 0, 1023, 0, 179); 
  if (newval1 < (oldval1-2) || newval3 > (oldval3+2)){  
    myservo1.write(newval3);
    Serial.print("3- ");
    Serial.println(newval3);
    oldval3=newval3;
  }

  newval4 = analogRead(potpin4);           
  newval4 = map(newval4, 0, 1023, 0, 179); 
  if (newval1 < (oldval1-2) || newval4 > (oldval4+2)){  
    myservo1.write(newval4);
    Serial.print("4- ");
    Serial.println(newval4);
    oldval4=newval4;
  }

  newval5 = analogRead(potpin5);           
  newval5 = map(newval5, 0, 1023, 0, 179); 
  if (newval1 < (oldval1-2) || newval5 > (oldval5+2)){  
    myservo1.write(newval5);
    Serial.print("5- ");
    Serial.println(newval5);
    oldval5=newval5;
  } 
  delay(50);  //to slow loop for testing
}

Some servo/pot test code that might be worth trying.

Hey, nice, Zoomkat!

Can I steal that for ArduinoInfo.Info ??

myservo1.attach(2);  
  myservo2.attach(3);
  myservo2.attach(4);
  myservo2.attach(5);
  myservo2.attach(6);

Can I steal that for ArduinoInfo.Info ??

I wouldn’t.

Besides, why write 30 lines of code, when you can do the same job with 80?

Isn't that example begging and pleading with you to use arrays?

Any time you find yourself numbering variables to tell them apart it's a hint that you should considering storing them in an array instead, and all the global variables in that sketch should IMO be converted to arrays.

Hi guys,

There are conflicting objectives with programming examples, because the audience varies so much.

The ideal would be the straightline obvious approach, good for the newbies and casual coders, followed by a functionally identical sketch which uses arrays and structs and iteration to compact code and make perfect sense to those of us whom are familiar with those concepts. And it could be commented enough that it builds familiarity with those techniques for beginners. Too often the advanced examples don't show how they regularize simpler constructs.

Anyone who has or can point to such dual-level code examples, please let me know.

Can I steal that for ArduinoInfo.Info ??

Sure. I like to post code the is as basic as possible to help get people get started.

AWOL: myservo1.attach(2);    myservo2.attach(3);  myservo2.attach(4);  myservo2.attach(5);  myservo2.attach(6);

Can I steal that for ArduinoInfo.Info ??

I wouldn't.

Besides, why write 30 lines of code, when you can do the same job with 80?

Then don't. Nobody is stopping you. :)

PeterH: Isn't that example begging and pleading with you to use arrays?

Any time you find yourself numbering variables to tell them apart it's a hint that you should considering storing them in an array instead, and all the global variables in that sketch should IMO be converted to arrays.

Sounds like a good project for you, so post your code here or on your web site. Don't be shy! :)

zoomkat: Some servo/pot test code that might be worth trying.

int potpin1 = 0;  //analog input pin A0

Analog input pin A0 is pin 14, not 0.

Henry_Best:

zoomkat:
Some servo/pot test code that might be worth trying.

int potpin1 = 0;  //analog input pin A0

Analog input pin A0 is pin 14, not 0.

What ever floats your boat. I use the labeling on the board to identify where to place the pot wiper wire.

Analog input pin A0 is pin 14, not 0.

So?
The analogRead function manages to make the mental leap to cope with this.

The point I was making earlier about Zoomkat’s code was not that it is crying out for arrays (it is), but that obvious cut-and-paste errors in the code I quoted may mislead noobs, and whilst it may compile, it certainly wasn’t tested.

This code is similarly untested.

//AWOL's multi pot/servo test 12/06/13
//view output using the serial monitor
#include <Servo.h> 

#define N_SERVOS   5   // you can go up to the maximum number supported by the board, but the code won't get any longer.
#define DEAD_BAND 2

const byte potPin [N_SERVOS] = {0, 1, 2, 3, 4};  // yes,I know they're sequential. It's for flexibility
const byte servoPin [N_SERVOS] = {2, 3, 4, 5, 6}; // see comment above
int oldval[N_SERVOS];  //Note, these all start off as zero. This may need handling differently.
Servo myservo [N_SERVOS];

void setup() 
{
  Serial.begin(9600);  // for debug output
  for (int i = 0; i < N_SERVOS; i++) {
    if(servoPin [i]) {  // just in case we forgot to fill the array.
      myservo [i].attach(servoPin [i]);  
    }
  }
  Serial.println("testing multi pot servo");  
}

void loop()
{ 
  for (int i = 0; i < N_SERVOS; i++) {
    int newval = analogRead(potPin [i]);  // no point in giving "newval" greater scope than it needs.         
    newval = map(newval, 0, 1024, 0, 179); 
    if (abs (newval - oldval[i]) > DEAD_BAND) { 
      myservo[i].write(newval);
      Serial.print(i);
      Serial.print(" ");
      Serial.println(newval); //print the new value for testing (optional)
      oldval[i] = newval;
    }  
  }
}

terryking228: Hi guys,

There are conflicting objectives with programming examples, because the audience varies so much.

The ideal would be the straightline obvious approach, good for the newbies and casual coders, followed by a functionally identical sketch which uses arrays and structs and iteration to compact code and make perfect sense to those of us whom are familiar with those concepts. And it could be commented enough that it builds familiarity with those techniques for beginners. Too often the advanced examples don't show how they regularize simpler constructs.

Anyone who has or can point to such dual-level code examples, please let me know.

If you wanted to demonstrate how to use a servo and potentiometer without involving the complexity of arrays, it would make more sense to demonstrate it for a single servo. This avoids setting examples that use poor techniques.

The point I was making earlier about Zoomkat's code was not that it is crying out for arrays (it is), but that obvious cut-and-paste errors in the code I quoted may mislead noobs, and whilst it may compile, it certainly wasn't tested.

Point about copy/paste error taken (code may still not work, I'll have to reconfigure a servo power supply to test). The issue about your code and a newbie is if the newbie wants to only wants to have pots/servos on fewer non consecutive pins, your code would probably require significant modification and understanding. Hopefully with not using nested loops for enumeration of the pot/servo pins, the non used portions of the simple code could just be commented out if desired. From what I've seen starting out with simple and easily followed code works out better for newbies. YMMV

Anyone who has or can point to such dual-level code examples, please let me know.

Below is code that originally used a loop to read the analog input pins. The loop was simple, but didn’t really allow for easy deviation or change. I removed the loop and made individual pin reads which makes for easier customization of the code and each pin read.

// zoomkat meta refresh server test code 4/12/13
// arduino IDE 1.0
// for W5100 ethernet shield
// the IP address will be dependent on your local network/router
// port 80 is default for HTTP, but can be changed as needed
// use IP address like http://192.168.1.102:84 in your brouser
// or http://zoomkat.no-ip.com:84 with dynamic IP service
// use the \ slash to escape the " in the html
// meta refresh set for 2 seconds

#include <SPI.h>
#include <Ethernet.h>
const int analogInPin0 = A0;
const int analogInPin1 = A1;
const int analogInPin2 = A2;
const int analogInPin3 = A3;
const int analogInPin4 = A4;
const int analogInPin5 = A5;


int x=0; //set refresh counter to 0
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,102); // ip in lan
EthernetServer server(84); //server is using port 84

void setup()
{
  // start the server
  Ethernet.begin(mac, ip);
  server.begin();
}

void loop()
{
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
     while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // see if HTTP request has ended with blank line
        if (c == '\n') {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          
          //meta-refresh page every 2 seconds
          x=x+1; //page upload counter
          client.println("<HTML>");
          client.print("<HEAD>");
          client.print("<meta http-equiv='refresh' content='2'>"); //url='http://192.168.1.102:84/'
          client.print("<TITLE />Zoomkat's meta-refresh test 4-12-13</title>");
          client.print("</head>");
          client.println("<BODY>");
          client.print("Zoomkat's meta-refresh test 4-12-13");
          client.println("
");
                    
          client.print("page refresh number ");
          client.println(x); //current refresh count
          client.println("
");
          client.println("
");
          
          client.print("Zoomkat's arduino analog input values:");
          client.println("
");
          client.println("
");
          
          // output the value of each analog input pin
            client.print("analog input0 ");
            client.print(" is ");
            client.print(analogRead(analogInPin0));
            client.println("
");
            
            client.print("analog input1 ");
            client.print(" is ");
            client.print(analogRead(analogInPin1));
            client.println("
");
                        
            client.print("analog input2 ");
            client.print(" is ");
            client.print(analogRead(analogInPin2));
            client.println("
");
            
            client.print("analog input3 ");
            client.print(" is ");
            client.print(analogRead(analogInPin3));
            client.println("
");
                                    
            client.print("analog input4 ");
            client.print(" is ");
            client.print(analogRead(analogInPin4));
            client.println("
");
            
            client.print("analog input5 ");
            client.print(" is ");
            client.print(analogRead(analogInPin5));
            client.println("
");
                                    
           break;
          client.println("</BODY>");
          client.println("</HTML>");
         }
        }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
  }
}

Well, I'm a guy whose LAN has the workgroup KISS.

My wife thought it was cute too, speaking of dual-level coding...