Servo control using Flex sensors

I'm working on building a bionic arm using 2.2'' Flex sensors. I've got the program for controlling one servo using one flex sensor. I need to control five servos with five flex sensors, I tried copy-pasting the same code for 5 servo, doesn't seem to work for me.

The program i'm using to controlling one servo with one flex sensor,

#include <Servo.h> 

Servo myservo;  // create servo object to control a servo 

int potpin = 0;  // analog pin used to connect flex sensor output
int val;    // variable to read the value from the analog pin 0

void setup() 
{ 
  Serial.begin(9600); // begin serial commmunication with Mega at 9600 baud rate
  myservo.attach(9);  // attach servo to pin 9 
} 

void loop() 

{ 
  val = analogRead(potpin);            // read the value of the flex sensor (analog value between 0 and 1023) 
  Serial.println(val);  
  val = map(val, 755, 1000, 0, 180);     // scale it to use it with the servo (value between 0 and 180) 
  myservo.write(val);                  // set the servo position according to the scaled value 
                          
}

Can you explain what did not work ? Perhaps post the code for 2 servos rather than all 5 servos and sensors.
Almost certainly the best way forward is for you to use arrays to avoid duplication of code but for now seeing what you tried will help diagnose the problem.

Previous post that might have your info.

https://www.google.com/search?as_q=flex+sensor&as_epq=&as_oq=&as_eq=&as_nlo=&as_nhi=&lr=&cr=&as_qdr=all&as_sitesearch=http%3A%2F%2Fforum.arduino.cc%2Findex&as_occt=any&safe=images&tbs=&as_filetype=&as_rights=

UKHeliBob:
Can you explain what did not work ? Perhaps post the code for 2 servos rather than all 5 servos and sensors.
Almost certainly the best way forward is for you to use arrays to avoid duplication of code but for now seeing what you tried will help diagnose the problem.

I'm a noob with C++. I'll have to learn about arrays. also, i don't want to complicate the code too much.

This is the program i used. Only the signal from Analog pin 0 is being read and the corresponding servo responds, rest of the servos just jitter, a lot.

#include <Servo.h> 

Servo myservo1;
Servo myservo2; // create servo object to control a servo 
Servo myservo3;
Servo myservo4;
Servo myservo5;


int potpin1 = 0;  // analog pin used to connect flex sensor output
int potpin2 = 1;
int potpin3 = 2;
int potpin4 = 3;
int potpin5 = 4;


int val1;    // variable to read the value from the analog pin 
int val2;
int val3;
int val4;
int val5;

void setup() 

{ 
  Serial.begin(9600); // begin serial commmunication with Mega at 9600 baud rate
  
  myservo1.attach(9);
  myservo2.attach(10);
  myservo3.attach(11);
  myservo4.attach(12);
  myservo5.attach(13); // attach servo to pin 
} 

void loop() 

{
  
{
 
  val1 = analogRead(potpin1);            // read the value of the flex sensor (analog value between 0 and 1023) 
  Serial.println(val1);  
  val1 = map(val1, 755, 1000, 0, 180);     // scale it to use it with the servo (value between 0 and 180) 
  myservo1.write(val1);                  // set the servo position according to the scaled value 
                          
} 


{ 
  val2 = analogRead(potpin2);            
  Serial.println(val2);  
  val2 = map(val2, 755, 1000, 0, 180);      
  myservo2.write(val2);                   
                          
} 


{ 
  val3 = analogRead(potpin3);            
  Serial.println(val3);  
  val3 = map(val3, 755, 1000, 0, 180);     
  myservo3.write(val3);                   
                          
} 


{ 
  val4 = analogRead(potpin4);             
  Serial.println(val4);  
  val4 = map(val4, 755, 1000, 0, 180);     
  myservo4.write(val4);                  
                          
} 



{ 
  val5 = analogRead(potpin5);            
  Serial.println(val5);  
  val5 = map(val5, 755, 1000, 0, 180);      
  myservo5.write(val5);                 
                          
} 

}

i don't want to complicate the code too much.

On the contrary, arrays will simplify and shorten your code.

As AWOL says, using arrays will ultimately simplify your program.

As to the current ones, what values do you get printed for val1, val2 etc ?

You have a load of unnecessary opening and closing braces. In your current program you can remove all of them except the first and last ones of the loop() function. The purpose of the pairs of braces is to enclose code that is executed as a block, often as a result of a control structure such as if, while or do or to define the start and end of a function such as loop().

UKHeliBob:
As AWOL says, using arrays will ultimately simplify your program.

As to the current ones, what values do you get printed for val1, val2 etc ?

You have a load of unnecessary opening and closing braces. In your current program you can remove all of them except the first and last ones of the loop() function. The purpose of the pairs of braces is to enclose code that is executed as a block, often as a result of a control structure such as if, while or do or to define the start and end of a function such as loop().

If you're asking about the flex sensor values, it's ranging from around 700 when flat to 980 when bent 180 degrees. At least, those are the values i get on the serial monitor and those values are from analog pin 0; I do not get any values from the other sensors. :drooling_face:

I read about arrays, i remember using them to write a program to solve matrices. but I dont know how to use them for my current application in Arduino. Any examples? i've already gone through the blinking LEDs example on the arduino website tutorial, doesn't really light my bulb. :blush:

I think there's at least one array example in the IDE.

const byte N_SERVOS = 5;
Servo myservo [N_SERVOS];
const byte servoPin [N_SERVOS] = {9, 10, 11, 12, 13};

void setup() 
{ 
  for (byte i = 0; i < N_SERVOS; i++) {  
    myservo [i].attach(servoPin [i]);
  }  
} 
//loop etc

AWOL:
I think there's at least one array example in the IDE.

const byte N_SERVOS = 5;

Servo myservo [N_SERVOS];
const byte servoPin [N_SERVOS] = {9, 10, 11, 12, 13};

void setup()
{
  for (byte i = 0; i < N_SERVOS; i++) { 
    myservo [i].attach(servoPin [i]);
  } 
}
//loop etc



Yes, there is. Its the blink LEDs example, didn't help me understand arrays to a great extent.

Can you help me write the code using arrays? PLEASE?
I can initialize and declare an array, what after that? XD

How are you powering these servos?

Arrch:
How are you powering these servos?

I'm using 6V servos, so built a separate power supply using 7806 ICs.

Can you help me write the code using arrays?

See, the example in reply #7

  myservo1.write(val1);                  // set the servo position according to the scaled value 
  ...  
  myservo2.write(val2);                   
  ...
  myservo5.write(val5);

How can you tell which value is which?

Try something like this for each of your values and post the results:

Serial.print("val2 = [");
Serial.print(val2);
Serial.println(']');

AWOL:

Can you help me write the code using arrays?

See, the example in reply #7

it'd help me understand if you can add comments to the code in post #7. just the major ones, i'm aware of the basics.

Arrch:

  myservo1.write(val1);                  // set the servo position according to the scaled value 

... 
  myservo2.write(val2);                   
  ...
  myservo5.write(val5);




How can you tell which value is which?

Try something like this for each of your values and post the results:



Serial.print("val2 = [");
Serial.print(val2);
Serial.println(']');

Sure. unfortunately, i dont have the Arduino with me now, i'm gonna try that ASAP.

Anjanbabu:
it'd help me understand if you can add comments to the code in post #7. just the major ones, i'm aware of the basics.

Some basic comments added

const byte N_SERVOS = 5; // defines the number of servos
Servo myservo [N_SERVOS]; // creates an array of servo objects
const byte servoPin [N_SERVOS] = {9, 10, 11, 12, 13}; // defines the pins that each of the servo objects are attached to

void setup() 
{ 
  for (byte i = 0; i < N_SERVOS; i++) {  // goes through each element in our array of servos
    myservo [i].attach(servoPin [i]); // and attaches the appropriate pin with the appropriate object
  }  
} 
//loop etc

If you're asking about the flex sensor values, it's ranging from around 700 when flat to 980 when bent 180 degrees. At least, those are the values i get on the serial monitor and those values are from analog pin 0; I do not get any values from the other sensors.

Until you solve this then it is a waste of time even considering writing the program using arrays. Do each of the sensors work if you put them on analogue pin 0 ?

The "flex sensors" may only be variable resistors and may need to be integrated into a voltage divider or some other circuit to broaden the output range of the flexing.

zoomkat:
The "flex sensors" may only be variable resistors and may need to be integrated into a voltage divider or some other circuit to broaden the output range of the flexing.

Yes, i'm using the sensors with a voltage divider circuit.

Arrch:

Anjanbabu:
it'd help me understand if you can add comments to the code in post #7. just the major ones, i'm aware of the basics.

Some basic comments added

const byte N_SERVOS = 5; // defines the number of servos

Servo myservo [N_SERVOS]; // creates an array of servo objects
const byte servoPin [N_SERVOS] = {9, 10, 11, 12, 13}; // defines the pins that each of the servo objects are attached to

void setup()
{
  for (byte i = 0; i < N_SERVOS; i++) {  // goes through each element in our array of servos
    myservo [i].attach(servoPin [i]); // and attaches the appropriate pin with the appropriate object
  } 
}
//loop etc

where do you initialize the analog pins to read the flex sensor signals?
and I'm failing to make this code work, i need more help :frowning:

can i use this loop for you have mentioned-- loop etc?

void loop() 

{

  val1 = analogRead(potpin1);            // read the value of the flex sensor (analog value between 0 and 1023) 
  Serial.println(val1);  
  val1 = map(val1, 755, 1000, 0, 180);     // scale it to use it with the servo (value between 0 and 180) 
  myservo1.write(val1);                  // set the servo position according to the scaled value 
                          
 
  val2 = analogRead(potpin2);            
  Serial.println(val2);  
  val2 = map(val2, 755, 1000, 0, 180);      
  myservo2.write(val2);                   
                          
 
  val3 = analogRead(potpin3);            
  Serial.println(val3);  
  val3 = map(val3, 755, 1000, 0, 180);     
  myservo3.write(val3);                   
                          
 
  val4 = analogRead(potpin4);             
  Serial.println(val4);  
  val4 = map(val4, 755, 1000, 0, 180);     
  myservo4.write(val4);                  
                          
 
  val5 = analogRead(potpin5);            
  Serial.println(val5);  
  val5 = map(val5, 755, 1000, 0, 180);      
  myservo5.write(val5);                 
                           

}