Servo Motor control

Hi, I am a mechanical engineering student working on a group project that uses an arduino to control three servo motors (HS 5585MH). We’re using 3 flex sensors (Flex Sensor 2.2")(one for each motor) to move the motor to desired position, the coding for this is below. The issue we are having is that we’d like one sensor to move only one motor, however they interfere with each other. Moving sensor A results in movement in motors A,B and C. The movements are not equal (i.e. one motor will turn 20 degrees whilst another turns 65 for example) . We are unsure as to whether this is a coding problem or maybe an issue with our circuit? We can’t see any problems with either but have limited experience in coding and even less with arduinos. The code was created by combining the sweepedit code from the ardunio example folder and the flexsensortest code found via google. Any help would be greatly appreciated.

             // Flex sensor test program
// Mike Grusin, SFE, 2011
// This program is free, use it however you wish!

// Make the following connections between the Arduino and the flex sensor
// Note that the flex sensor pins are interchangeable

// Sensor pin - GND
// Sensor pin - Analog In 0, with 10K resistor to +5V

// Upload this sketch to your Arduino, then activate the Serial Monitor
// (set the Serial Monitor to 9600 baud)
#include <Servo.h> 
Servo middle,index,thumb;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
int posm = 0;    // variable to store the servo position 
int errorm = 0;  //variable used to calculate correct servo position
int esvm = 0;    //variable to define servo's expected position based on sensor reading
int posi = 0;     
int errori = 0;
int esvi = 0;
int post = 0;    
int errort = 0;
int esvt = 0;

void setup()
  // initialize serial communications

  middle.attach(7);  // attaches the servo on pin 7 to the servo object 
  thumb.attach(8);  //

void loop()
  int sensor_index, degrees_index;    //initialize values to read sensor and assign degrees
  int sensor_middle, degrees_middle;
  int sensor_thumb, degrees_thumb;
  // read the voltage from the voltage divider (sensor plus resistor)
  sensor_index = analogRead(1);
  sensor_middle= analogRead(0);
  // convert the voltage reading to inches
  // the first two numbers are the sensor values for straight (768) and bent (853)
  // the second two numbers are the degree readings we'll map that to (0 to 180 degrees)
  degrees_index= map(sensor_index, 568,748, 0, 180);
  degrees_middle = map(sensor_middle, 568,748, 0, 180);
  degrees_thumb = map(sensor_thumb, 577,757, 0, 180);
  // note that the above numbers are ideal, your sensor's values will vary
  // to improve the accuracy, run the program, note your sensor's analog values
  // when it's straight and bent, and insert those values into the above function.
  // print out the result
  Serial.print("analog input0: ");
  Serial.print("   degrees_middle: ");
  Serial.print("   position_middle: ");
  Serial.print("analog input1: ");
  Serial.print("   degrees_index: ");
  Serial.print("   position_index: ");
  Serial.print("analog input2: ");
  Serial.print("   degrees_thumb: ");
  Serial.print("   position_thumb: ");
  // pause before taking the next reading
//makes the expected servo position equal to the actual servo position, then finds the difference between the sensor reading and the expected position
  int esvm=posm;
  int errorm= constrain(degrees_middle,0,180)-esvm;
   int esvi=posi;
  int errori= constrain(degrees_index,0,180)-esvi;
   int esvt=post;
  int errort= constrain(degrees_thumb,0,180)-esvt;
 if(errorm >=5)  // if the error is greater than 5, increase the motor position by 10
    posm += 10; 
   else if(errorm <=-5)     //else if the erros is less than -5, decrease the motor position by 10
    posm -= 10;
   delay (5);        //otherwise delay

  if(errori >=5)  
    posi += 10; 
   else if(errori <=-5)     
    posi -= 10;
   delay (5);
 if(errort >=5)  
    post += 10; 
   else if(errort <=-5)     
    post -= 10;
   delay (50);
    middle.write(posm);              // tell servo to go to position in variable 'posm' 

What resistance are these flex sensors? If more than 10k I suggest changing the code thus:

  // read the voltage from the voltage divider (sensor plus resistor)
  sensor_index = analogRead(1);
  sensor_index = analogRead(1);
  sensor_middle= analogRead(0);
  sensor_middle= analogRead(0);

double reading will allow much much more time for settling between analog multiplexer changes for high-impedance
sources (110us instead of 6us).

project that uses an arduino to control three servo motors (HS 5585MH).

How are you powering your servos? Whenever DC motors are involved, the noise gremlins have a big party :wink:

Noise can easily mess up analog readings.


Hi Mark T, thanks for your advice!! Your suggestion helped reduce the noise (thanks joe mcd for further clarification of the cause!!) and now the motors are running smoothly. Your help was very much appreciated!!

Hi. Can a flex sensor turn the servo motor in clockwise rather than anticlockwise? If yes, which part of coding need to be change? Is it the map part of the coding?