using flex sensors to control servos

Hey guys im working on a little project where im trying to see if i can cook myself dinner using a robotic arm. ive got flex sensors sewed into a full arm glove and im using an xbee connection to the arm. the arm is just a little rough something i put together using mechano. Im not a programmer, this is just something i do for a hobby

so, the flex sensors control the servo movement over an xbee radio connection

m having problems with the code though and its driving me nuts. there was some good info on a topic like this before but i couldnt get the code to work for me. here it is

the sender

// Set up the sensor pin numbers
int Flexsensor[3] = {1, 2, 3};

// Save the values sent, so a new value is sent only when a change occurs
int sentValues[3];

// Change these if your minimum value is greater than 0
int from[] = {0,0 ,0}; 

// Change these if you maximum value is less than 1023
int to[] = {1023,1023,1023}; 

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

void loop()
{
   for(int i=0; i<3; i++)
   {
     int val;
       // Read a sensor's value
       val = analogRead(Flexsensor[i]);

       // Map it to the servo range using it's min/max range
       val = map(val, from[i], to[i], 0, 179);

       // If the new val is not the same as the last val sent
       if(val != sentValues[i])
      {
        Serial.print("<"); // Start of packet marker
        Serial.print(i);     // which servo to move
        Serial.print(":");  // delimiter
        Serial.print(val);  // servo position to move to
        Serial.print(">"); // End of packet marker

          // Record the value sent, so we don't send again unless there is a change
        sentValues[i] = val;
      }
   }
}

and the receiver

 #include <Servo.h>

Servo myservo1;
Servo myservo2;
Servo myservo3;


boolean started = false;
boolean ended = false;
char inData[24];  // Make sure this is big enough for your data
byte index = 0;

void setup()
{
  myservo1.attach(8);
  myservo2.attach(9);
  myservo3.attach(10);
  
}

void loop()
{
   while(Serial.available() > 0)
   {
      char aChar = Serial.read(); // Read a single character
      if(aChar == '<') // Start of packet marker
      {
         started = true;
         ended = false;

         index = 0; // Initialize array index
         inData[index] = '\0'; // Keep string NULL terminated
      }
      else if(aChar == '>')
      {
         ended = true;
      }
      else
      {
         inData[index] = aChar; // Store the character
         index++; // Increment the position to store the next character
         inData[index] = '\0'; // Keep string NULL terminated
      }
   }

   if(started && ended)
   {
      char *token = strtok(inData,":"); // Extract servo number as string
      if(token)
      {
         // We got a servo number as a string. Make it a number
         int servoNumber = atoi(token);

         // Now, get the position
         token = strtok(NULL,""); // NULL to keep parsing same string. 
         // no delimiters because we want all of the rest of the string

         if(token)
         {
            // We got a position
            int position = atoi(token);

            // Now, move the right servo
            switch(servoNumber)
            {
               case 0: myservo1.write(position); break;
               case 1: myservo2.write(position); break;
               case 2: myservo3.write(position); break;
               
            }
         }
      }
   }
}

can anyone see why this wont work for me?
also i found other example code that the owner claims to work for him

sender

#include <Servo.h>

Servo myservo1;
Servo myservo2;
Servo myservo3;


boolean started = false;
boolean ended = false;
char inData[24];  // Make sure this is big enough for your data
byte index = 0;

void setup()
{
  myservo1.attach(8);
  myservo2.attach(9);
  myservo3.attach(10);
  
}

void loop()
{
   while(Serial.available() > 0)
   {
      char aChar = Serial.read(); // Read a single character
      if(aChar == '<') // Start of packet marker
      {
         started = true;
         ended = false;

         index = 0; // Initialize array index
         inData[index] = '\0'; // Keep string NULL terminated
      }
      else if(aChar == '>')
      {
         ended = true;
      }
      else
      {
         inData[index] = aChar; // Store the character
         index++; // Increment the position to store the next character
         inData[index] = '\0'; // Keep string NULL terminated
      }
   }

   if(started && ended)
   {
      char *token = strtok(inData,":"); // Extract servo number as string
      if(token)
      {
         // We got a servo number as a string. Make it a number
         int servoNumber = atoi(token);

         // Now, get the position
         token = strtok(NULL,""); // NULL to keep parsing same string. 
         // no delimiters because we want all of the rest of the string

         if(token)
         {
            // We got a position
            int position = atoi(token);

            // Now, move the right servo
            switch(servoNumber)
            {
               case 0: myservo1.write(position); break;
               case 1: myservo2.write(position); break;
               case 2: myservo3.write(position); break;
               
            }
         }
      }
   }
}

and the reciving

#include <Servo.h>

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

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

myservo1.attach(2); // attaches the servo on pin 9 to the servo object
myservo2.attach(3);
myservo3.attach(4);
myservo4.attach(5);
myservo5.attach(6);
}

void loop()
{
if(Serial.available() >=5)
{
byte servoAng1 = Serial.read();
byte servoAng2 = Serial.read();
byte servoAng3 = Serial.read();
byte servoAng4 = Serial.read();
byte servoAng5 = Serial.read();


// Send the servo to the position read...  (note: you get to make this happen)
myservo1.write(servoAng1);
myservo2.write(servoAng2);
myservo3.write(servoAng3);
myservo4.write(servoAng4);
myservo5.write(servoAng5);
}
}

any help you can give me would be greatly appreciated. this block is driving me nuts

Costello:
Hey guys im working on a little project where im trying to see if i can cook myself dinner using a robotic arm. ive got flex sensors sewed into a full arm glove and im using an xbee connection to the arm. the arm is just a little rough something i put together using mechano. Im not a programmer, this is just something i do for a hobby

so, the flex sensors control the servo movement over an xbee radio connection

m having problems with the code though and its driving me nuts. there was some good info on a topic like this before but i couldnt get the code to work for me. here it is

the sender

// Set up the sensor pin numbers

int Flexsensor[3] = {1, 2, 3};

// Save the values sent, so a new value is sent only when a change occurs
int sentValues[3];

// Change these if your minimum value is greater than 0
int from = {0,0 ,0};

// Change these if you maximum value is less than 1023
int to = {1023,1023,1023};

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

void loop()
{
   for(int i=0; i<3; i++)
   {
     int val;
       // Read a sensor’s value
       val = analogRead(Flexsensor[i]);

// Map it to the servo range using it’s min/max range
       val = map(val, from[i], to[i], 0, 179);

// If the new val is not the same as the last val sent
       if(val != sentValues[i])
      {
        Serial.print("<"); // Start of packet marker
        Serial.print(i);     // which servo to move
        Serial.print(":");  // delimiter
        Serial.print(val);  // servo position to move to
        Serial.print(">"); // End of packet marker

// Record the value sent, so we don’t send again unless there is a change
        sentValues[i] = val;
      }
   }
}




and the receiver


#include <Servo.h>

Servo myservo1;
Servo myservo2;
Servo myservo3;

boolean started = false;
boolean ended = false;
char inData[24];  // Make sure this is big enough for your data
byte index = 0;

void setup()
{
  myservo1.attach(8);
  myservo2.attach(9);
  myservo3.attach(10);
 
}

void loop()
{
   while(Serial.available() > 0)
   {
      char aChar = Serial.read(); // Read a single character
      if(aChar == ‘<’) // Start of packet marker
      {
         started = true;
         ended = false;

index = 0; // Initialize array index
         inData[index] = ‘\0’; // Keep string NULL terminated
      }
      else if(aChar == ‘>’)
      {
         ended = true;
      }
      else
      {
         inData[index] = aChar; // Store the character
         index++; // Increment the position to store the next character
         inData[index] = ‘\0’; // Keep string NULL terminated
      }
   }

if(started && ended)
   {
      char *token = strtok(inData,":"); // Extract servo number as string
      if(token)
      {
         // We got a servo number as a string. Make it a number
         int servoNumber = atoi(token);

// Now, get the position
         token = strtok(NULL,""); // NULL to keep parsing same string.
         // no delimiters because we want all of the rest of the string

if(token)
         {
            // We got a position
            int position = atoi(token);

// Now, move the right servo
            switch(servoNumber)
            {
               case 0: myservo1.write(position); break;
               case 1: myservo2.write(position); break;
               case 2: myservo3.write(position); break;
               
            }
         }
      }
   }
}




can anyone see why this wont work for me?
also i found other example code that the owner claims to work for him

sender


#include <Servo.h>

Servo myservo1;
Servo myservo2;
Servo myservo3;

boolean started = false;
boolean ended = false;
char inData[24];  // Make sure this is big enough for your data
byte index = 0;

void setup()
{
  myservo1.attach(8);
  myservo2.attach(9);
  myservo3.attach(10);
 
}

void loop()
{
   while(Serial.available() > 0)
   {
      char aChar = Serial.read(); // Read a single character
      if(aChar == ‘<’) // Start of packet marker
      {
         started = true;
         ended = false;

index = 0; // Initialize array index
         inData[index] = ‘\0’; // Keep string NULL terminated
      }
      else if(aChar == ‘>’)
      {
         ended = true;
      }
      else
      {
         inData[index] = aChar; // Store the character
         index++; // Increment the position to store the next character
         inData[index] = ‘\0’; // Keep string NULL terminated
      }
   }

if(started && ended)
   {
      char *token = strtok(inData,":"); // Extract servo number as string
      if(token)
      {
         // We got a servo number as a string. Make it a number
         int servoNumber = atoi(token);

// Now, get the position
         token = strtok(NULL,""); // NULL to keep parsing same string.
         // no delimiters because we want all of the rest of the string

if(token)
         {
            // We got a position
            int position = atoi(token);

// Now, move the right servo
            switch(servoNumber)
            {
               case 0: myservo1.write(position); break;
               case 1: myservo2.write(position); break;
               case 2: myservo3.write(position); break;
               
            }
         }
      }
   }
}




and the reciving


#include <Servo.h>

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

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

myservo1.attach(2); // attaches the servo on pin 9 to the servo object
myservo2.attach(3);
myservo3.attach(4);
myservo4.attach(5);
myservo5.attach(6);
}

void loop()
{
if(Serial.available() >=5)
{
byte servoAng1 = Serial.read();
byte servoAng2 = Serial.read();
byte servoAng3 = Serial.read();
byte servoAng4 = Serial.read();
byte servoAng5 = Serial.read();

// Send the servo to the position read…  (note: you get to make this happen)
myservo1.write(servoAng1);
myservo2.write(servoAng2);
myservo3.write(servoAng3);
myservo4.write(servoAng4);
myservo5.write(servoAng5);
}
}




any help you can give me would be greatly appreciated. this block is driving me nuts

any help you can give me would be greatly appreciated. this block is driving me nuts

Have you gotten it to work first using wired connections like below? Flex sensors may not be a good choise for a position sensors.

no. i dont know how id get the arduino to distinguish between different flex sensors.

what i mean is i dont know how to get the ardino to use data from flexServo1 to turn myservo1, and keep that seperate from the other sensors and servos.

im able to do this with code written for 1 sensor controlling 1 servo but i dont know how to add more without it going crazy. do you know how i would add more sensors and servos?

that video looks pretty impressive, is he using accelerometers? id like to try that next and i have a few wii controllers at home that would do the trick, but id like to get to use the flex sensors first. i saw a kid using one for his science fair and i thought it looked like a nice project

why not send the data as a string, and split it on the receiving end.

Send: <150,180,45>

receive after split:
servo1 = 150
servo2 = 180
servo3 = 45

If you use the string method, then your receiving code is nearly there. You just need to add another IF statement to look for a comma ‘,’ and change the array the data goes into.

i get what you mean. im just not sure how to write that in code. ill look into string writing though. thanks for your help

No no, dont use strings, send it as an array, but set it up like a string. ‘<’,‘1’,‘5’,‘0’, ‘,(separating comma)’ ,‘1’,‘8’,‘0’, …’>’

something like this?

sending

int flexSensor[3] = {1,2,3};

int sentValues[3];

// Change these if your minimum value is greater than 0
int from[] = {420,420 ,420}; 

// Change these if you maximum value is less than 1023
int to[] = {480,480,480}; 

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

void loop()
{
   for(int i=0; i<3; i++)
   {
     int val;
       // Read a sensor's value
       val = analogRead(flexSensor[i]);

       // Map it to the servo range using it's min/max range
       val = map(val, from[i], to[i], 0, 179);

       // If the new val is not the same as the last val sent
       if(val != sentValues[i])
      {
        Serial.print("<"); // Start of packet marker
        Serial.print(i);     // which servo to move
        Serial.print(":");  // delimiter
        Serial.print(val);  // servo position to move to
        Serial.print(">"); // End of packet marker

          // Record the value sent, so we don't send again unless there is a change
        sentValues[i] = val;
      }
   }
}

receiving

#include <Servo.h>

String readString, servo1, servo2, servo3;
Servo myservo1, myservo2, myservo3;  // create servo object to control a servo

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

}

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

        servo1 = readString.substring(0, 4);
        servo2 = readString.substring(4, 8);
        
        Serial.println(servo1);
        Serial.println(servo2);
        Serial.println(servo3);
          //convert readString into a number
        
        int n1 = servo1.toInt();
        int n2 = servo2.toInt();
        int n3 = servo3.toInt();
        // auto select appropriate value, copied from someone elses code
        
        myservo1.write(n1);  
        myservo2.write(n2);
        myservo3.write(n3);
        
       
        }
         readString=""; //clears variable for new input
      }
 

  
   }
}

Close, but it better to check if the incoming char is NOT equal to ',' and store the variables, and if it is equal, to switch the array.