Servos not responding

Hello!

I’ve got a problem with my arduino not sending the correct information (or any information at all) to a pair of servos. I’m using the latest arduino IDE (1.03 i think, i downloaded it earlier today), Windows 7, and the arduino is a mega2560 with a prototype shield v.5 from adafruit. I’ve tried all kinds of different pins, I’ve verified that the servos work. I’ve verified that I can get the servos to work with the arduino mega2560 using the example sketches, just not the code in question. When I read the value I’m sending to the servo data line its within 0 and 180 always, but there is no signal being sent (no voltage coming from the arduino at all) in the case of the fingerServo servo or very slow blips in the case of the wristRServo servo. This is the case no matter what pins I attach them to.

The servos will “reset” to the position set in the setup function when the arduino is reset, the problem only occurs when trying to write to the servo in the loop function.

Here is the code but I can’t help but feel like this is a timer issue that I’m unfamiliar with or something else hardware related, the variables controlling servo position are correct but the arduino just doesn’t want to send the data. Regardless the code is below.

The code is meant to control part of a robotic arm via two sensors. The two servos are a “finger” servo and “wrist” servo. Anything that has to do with the Serial library is for troubleshooting purposes only.

#include <Servo.h>
 
Servo fingerServo;  // create servo object to control a servo 
Servo wristRServo;

//constants

#define WRSAMPLES 99
#define ESAMPLES 99
#define FSAMPLES 49

//variables

int pos = 0;    // variable to store the servo position 
int indexFlexControl = A2;
int xAccelPin = A3;
unsigned long elbow_sum=0;
int elbow_avg=0;
unsigned long finger_sum=0;
int finger_avg=0;
int calButton = 7;
int fHigh = 0;
int fLow = 0;
int calLedHigh = 3;
int calLedLow = 4;
int elbowInitial=1;
int wristRInitial=1;
int fingerInitial=1;
int elbowCount=0;
int wristRCount=0;
int fingerCount=0;
int wristRPos=90;
int fingerPos=90;
int wristR_sum=0;
int wristR_avg=0;

//arrays
int wristR_array[WRSAMPLES];
int finger_array[FSAMPLES];
int elbow_array [ESAMPLES];

int wristRSense ();
int fingerSense ();






void setup() 
{ 

 
  //setup servos and serial library
  fingerServo.attach(5);  
  wristRServo.attach(9);  
  pinMode(calButton, INPUT);
  pinMode(calLedHigh, OUTPUT);
  pinMode(calLedLow, OUTPUT);
  Serial.begin(9600);

  int fingerCal [FSAMPLES];
  
//finger calibration 
  
  while(digitalRead(calButton) == LOW){              //waiting for calibration button to be pressed
    delay(15);
  }
  delay(200);  //waiting for signal noise from button press to drop
  
  //open hand calibration
  for(int q=(FSAMPLES+1); q>0; q--){ 
    
    fingerCal[q-1] = analogRead(indexFlexControl);
    finger_sum += fingerCal[q-1];
  }
  fHigh = finger_sum/(FSAMPLES+1);
  
  
  digitalWrite(calLedHigh, HIGH);
  //end of open hand calibration

   
  finger_sum = 0;
  
  
  
  while(digitalRead(calButton) == LOW){ //waiting for calibration button to be pressed
    delay(15);
  }
  delay(2000);  //waiting for signal noise from button press to drop
  
  //closed hand calibration
  for(int q=(FSAMPLES); q>-1; q--){ 
    
    fingerCal[q-1] = analogRead(indexFlexControl);
    finger_sum += fingerCal[q-1];
  }
  fLow = finger_sum/(FSAMPLES+1);
  
  digitalWrite(calLedLow, HIGH);
  //end of closed hand calibration

}





void loop() 
{

  //call wrist sensing function
  wristRSense();
  wristRServo.write(wristRPos);

  

  //call finger sensing function
  fingerSense();

  fingerServo.write(fingerPos);
  fingerServo.read();

}





//Determinging wrist position
int wristRSense(){
  //obtaining wrist rotation position for the first cycle
  if(wristRInitial == 1){
    wristR_sum=0;
    wristR_avg=0;
    for(int n=(WRSAMPLES+1); n>0; n--){

      wristR_array[n-1] = analogRead(xAccelPin);
      wristR_sum += wristR_array[n-1];
    }
    
    wristR_avg = wristR_sum/(WRSAMPLES+1);
    
    wristRPos = map(wristR_avg, 395, 625, 5, 175);
    wristRPos = constrain(wristRPos, 5, 175);
    wristRInitial=0;
  }
  //obtaining wrist rotation position for every cycle after first
  else{
    wristR_sum -= wristR_array[wristRCount];
    wristR_array[wristRCount] = analogRead(xAccelPin);
    delay(15);
    wristR_sum += wristR_array[wristRCount];
    
    wristR_avg = wristR_sum/(WRSAMPLES+1);
    
    wristRPos = map(wristR_avg, 395, 625, 5, 175);
    wristRPos = constrain(wristRPos, 5, 175);
    
    //increasing count variable for next cycle
    if(wristRCount >= WRSAMPLES){
      wristRCount=0;
    }
    else{
      wristRCount++;
    }
  }
  return(wristRPos);
  
}





int fingerSense(){
  //obtaining finger position for the first cycle
  if(fingerInitial == 1){
    finger_sum=0;
    finger_avg=0;
    for(int n=(FSAMPLES+1); n>0; n--){

      finger_array[n-1] = analogRead(indexFlexControl);
      finger_sum += finger_array[n-1];

    }
    
    finger_avg = finger_sum/(FSAMPLES+1);
    
    fingerPos = map(finger_avg, fLow, fHigh, 5, 175);
    fingerPos = constrain(fingerPos, 5, 175);
    fingerInitial=0;
        Serial.println(fingerPos);
  }
  //obtaining wrist rotation position for every cycle after first
  else{
    finger_sum = finger_sum - finger_array[fingerCount];
    finger_array[fingerCount] = analogRead(indexFlexControl);
    finger_sum = finger_sum + finger_array[fingerCount];
    
    finger_avg = finger_sum/(FSAMPLES+1);
    
    fingerPos = map(finger_avg, fLow, fHigh, 5, 175);
    fingerPos = constrain(fingerPos, 5, 175);
    Serial.println(fingerPos);
    //increasing count variable for next cycle
    if(fingerCount >= FSAMPLES){
      fingerCount=0;
    }
    else{
      fingerCount++;
    }
  }
  return(fingerPos);
  
}

Any and all help is highly appreciated!! I’m losing my mind over this thing.

P.S. Please let me know if you see any other possible code errors or glaringly bad code practices that should be fixed as well. (EE not comp-sci lol)

You need to power the servos separately from the arduino board as this often cannot supply enough power for a servo to work.

for(int q=(FSAMPLES+1); q>0; q--){ 
    
    fingerCal[q-1]

Somewhat convoluted code here. FSAMPLES is defined to be 49. So, valid indices for "fingerCal" run from 0 to 48 inclusive. First iteration of the loop, "q" has a value of 50. 50 - 1 = 49. Oops.

I'm getting really, really bored with cross-posting now. Don't do it IT WASTES TIME.

Multi servo test code.

//zoomkat 11-22-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added 

String readString;
#include <Servo.h> 
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);

  //myservoa.writeMicroseconds(1500); //set initial servo position if desired

  myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control 
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or 30c,180b,70a,120d,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

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

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
        {
          Serial.print("writing Microseconds: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
          if(readString.indexOf('c') >0) myservoc.write(n);
          if(readString.indexOf('d') >0) myservod.write(n);
        }
         readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}