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