How To Set up xbee for analog input

ok so i am trying to make a animatronic hand. i have a hand hooked up with 5 servos controlled by a arduino. right now i simply have a wired glove running from the flex sensors to the arduino and the arduino outputting to the servo.

NOWWWW lets say i want to make it wireless... with xbee

no i have a buget f 100 buks and i priced them for about 80 buks

now i have no knolege of xbee programing maby you could help me with the coding for one flex sensor sent from one arduino with a xbee to another one that controllers the servos

i will be great appreciative and will credit you when i publish this

The XBees, as you have described things, are only sending and receiving radio signals. The Arduinos are collecting the sensor data and activating the servos.

In this case, sending data from one Arduino to another is very simple. Just write to the serial port on the sender and read from the serial port on the receiver.

Use Series 1 modules. They are easier to set up.

All you need to do is define what you want to send, and send it. Something like "<1:135>" which means servo 1 is to be set to 135 degrees. The < and > indicate the start and end of a packet, and the servoNumber:servoValue part is the packet.

This command is easily constructed on the sender (exactly like you would send data to the Serial Monitor. It is easily collected as an array on the receiver, since the start and end of the packet is defined. It is easily parsed, using either strtok to extract tokens (the need to then be converted to numbers), or sscanf to parse in a single step.

The XBees, as you have described things, are only sending and receiving radio signals. The Arduinos are collecting the sensor data and activating the servos.

In this case, sending data from one Arduino to another is very simple. Just write to the serial port on the sender and read from the serial port on the receiver.

Use Series 1 modules. They are easier to set up.

All you need to do is define what you want to send, and send it. Something like "<1:135>" which means servo 1 is to be set to 135 degrees. The < and > indicate the start and end of a packet, and the servoNumber:servoValue part is the packet.

This command is easily constructed on the sender (exactly like you would send data to the Serial Monitor. It is easily collected as an array on the receiver, since the start and end of the packet is defined. It is easily parsed, using either strtok to extract tokens (the need to then be converted to numbers), or sscanf to parse in a single step.

ok so heres my code currently with out wireless added or anything simply wired. ok can u help me with just a simple maby i flex to 1 servo code for the x bee

Code:

 /*
  Servo control from an analog input
 */

 int servoPin1 = 2; 
 int servoPin2 = 3; 
 int servoPin3 = 4; 
 int servoPin4 = 5; 
 int servoPin5 = 6; 
 
 int minPulse1 = 500 ; 
 int minPulse2 = 500 ;  
 int minPulse3 = 500 ;  
 int minPulse4 = 500 ;  
 int minPulse5 = 500 ;  
 
 int maxPulse1 = 4000;
 int maxPulse2 = 4000;  
 int maxPulse3 = 4000;  
 int maxPulse4 = 4000;  
 int maxPulse5 = 4000;  
 
 int pulse1 = 0;
 int pulse2 = 0;        
 int pulse3 = 0;        
 int pulse4 = 0;        
 int pulse5 = 0;         

 long lastPulse1 = 0;
 long lastPulse2 = 0;    
 long lastPulse3 = 0;    
 long lastPulse4 = 0;    
 long lastPulse5 = 0;
 
 int refreshTime1 = 20; 
 int refreshTime2 = 20; 
 int refreshTime3 = 20; 
 int refreshTime4 = 20; 
 int refreshTime5 = 20; 

  int analogPin1 = 1; 
  int analogPin2 = 2; 
  int analogPin3 = 3; 
  int analogPin4 = 4; 
  int analogPin5 = 5; 

 int analogValue1 = analogRead(analogPin1);
 int analogValue2 = analogRead(analogPin2);
 int analogValue3 = analogRead(analogPin3);
 int analogValue4 = analogRead(analogPin4);
 int analogValue5 = analogRead(analogPin5); 
 
 void setup() {
  pinMode(servoPin1, OUTPUT);
  pinMode(servoPin2, OUTPUT);
  pinMode(servoPin3, OUTPUT);
  pinMode(servoPin4, OUTPUT);
  pinMode(servoPin5, OUTPUT);
  pulse1 = minPulse1;  
  pulse2 = minPulse2;
  pulse3 = minPulse3;
  pulse4 = minPulse4;
  pulse5 = minPulse5;
  Serial.begin(9600);
 }

 void loop() {
  analogValue1 = analogRead(analogPin1); 
  analogValue2 = analogRead(analogPin2);
  analogValue3 = analogRead(analogPin3);
  analogValue4 = analogRead(analogPin4);
  analogValue5 = analogRead(analogPin5);

  pulse1 = map(analogValue1,500,0 ,minPulse1,maxPulse1);
  pulse2 = map(analogValue2,500,0 ,minPulse2,maxPulse2);
  pulse3 = map(analogValue3,500,50 ,minPulse3,maxPulse3);
  pulse4 = map(analogValue4,500,50 ,minPulse4,maxPulse4);
  pulse5 = map(analogValue5,500,50 ,minPulse5,maxPulse5);
          

  
if (millis() - lastPulse1 >= refreshTime1) {
    digitalWrite(servoPin1, HIGH);   
    delayMicroseconds(pulse1);       
    digitalWrite(servoPin1, LOW);   
    lastPulse1 = millis(); 
} 
 if (millis() - lastPulse2 >= refreshTime2) {
    digitalWrite(servoPin2, HIGH);   
    delayMicroseconds(pulse2);       
    digitalWrite(servoPin2, LOW);   
    lastPulse2 = millis();  
 }
  if (millis() - lastPulse3 >= refreshTime3) {
    digitalWrite(servoPin3, HIGH);   
    delayMicroseconds(pulse3);       
    digitalWrite(servoPin3, LOW);   
    lastPulse3 = millis();
  }  
   if (millis() - lastPulse4 >= refreshTime4) {
    digitalWrite(servoPin4, HIGH);   
    delayMicroseconds(pulse4);       
    digitalWrite(servoPin4, LOW);   
    lastPulse4 = millis();
   }  
    if (millis() - lastPulse5 >= refreshTime5) {
    digitalWrite(servoPin5, HIGH);   
    delayMicroseconds(pulse5);       
    digitalWrite(servoPin5, LOW);   
    lastPulse5 = millis();      
    }
}

You know, there is a very nice Servo library. Using it would make this code much shorter.

You are collecting the sensor data (although using arrays would make this much easier, too).

You are commanding a servo, based on a sensor reading.

If you want to have another Arduino manipulate the servo, you would add something like this:

Serial.print("<");
Serial.print(servoPin1);
Serial.print(":");
Serial.print(pulse1);
Serial.print(">");

This assumes that you are using a XBee shield that expects to get the data to broadcast from pin 0 and to put any data it receives on pin 1.

Then, on the receiver, collect the serial data into an array, starting to store data after you see a <. Stop storing data, and parse what has been stored when you see a >.

You know, there is a very nice Servo library. Using it would make this code much shorter.

You are collecting the sensor data (although using arrays would make this much easier, too).

You are commanding a servo, based on a sensor reading.

If you want to have another Arduino manipulate the servo, you would add something like this:

Code:

Serial.print("<");
Serial.print(servoPin1);
Serial.print(":");
Serial.print(pulse1);
Serial.print(">");

This assumes that you are using a XBee shield that expects to get the data to broadcast from pin 0 and to put any data it receives on pin 1.

Then, on the receiver, collect the serial data into an array, starting to store data after you see a <. Stop storing data, and parse what has been stored when you see a >.

ok so im still a little confused.

ill have 2 arduinos one for sending the inputs from the sensors and one receiving controlling the servos.

so could you clarify the code for sending and receiving i have an idea but im not sure

thank you

The serial print stuff I showed is all you need on the sender.

On the receiver, use some code like this:

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

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)
   {
      // We got a complete packet. Do something
   }
}

Where the "We got a complete packet. Do something" comment is you would put code to parse the inData array to determine which servo to send to what angle. There are several ways to do this. You could use sscanf with a format specifier of "%i:%i" to extract two integers. Or, you could use strtok to extract the tokens. The first token is delimited by the :. The second token is delimited by the NULL (with NULL as the string to parse too, to keep parsing the same string). Then, use atoi to convert each token to a number.

So is this what you mean by this

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

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)
   {
       /*
  Servo control from an analog input
 */

 int servoPin1 = 2;
 int servoPin2 = 3;
 int servoPin3 = 4;
 int servoPin4 = 5;
 int servoPin5 = 6;

 int minPulse1 = 500 ;
 int minPulse2 = 500 ;  
 int minPulse3 = 500 ;  
 int minPulse4 = 500 ;  
 int minPulse5 = 500 ;  

 int maxPulse1 = 4000;
 int maxPulse2 = 4000;  
 int maxPulse3 = 4000;  
 int maxPulse4 = 4000;  
 int maxPulse5 = 4000;  

 int pulse1 = 0;
 int pulse2 = 0;        
 int pulse3 = 0;        
 int pulse4 = 0;        
 int pulse5 = 0;        

 long lastPulse1 = 0;
 long lastPulse2 = 0;    
 long lastPulse3 = 0;    
 long lastPulse4 = 0;    
 long lastPulse5 = 0;

 int refreshTime1 = 20;
 int refreshTime2 = 20;
 int refreshTime3 = 20;
 int refreshTime4 = 20;
 int refreshTime5 = 20;

  int analogPin1 = 1;
  int analogPin2 = 2;
  int analogPin3 = 3;
  int analogPin4 = 4;
  int analogPin5 = 5;

 int analogValue1 = analogRead(analogPin1);
 int analogValue2 = analogRead(analogPin2);
 int analogValue3 = analogRead(analogPin3);
 int analogValue4 = analogRead(analogPin4);
 int analogValue5 = analogRead(analogPin5);

 void setup() {
  pinMode(servoPin1, OUTPUT);
  pinMode(servoPin2, OUTPUT);
  pinMode(servoPin3, OUTPUT);
  pinMode(servoPin4, OUTPUT);
  pinMode(servoPin5, OUTPUT);
  pulse1 = minPulse1;  
  pulse2 = minPulse2;
  pulse3 = minPulse3;
  pulse4 = minPulse4;
  pulse5 = minPulse5;
  Serial.begin(9600);
 }

 void loop() {
  analogValue1 = analogRead(analogPin1);
  analogValue2 = analogRead(analogPin2);
  analogValue3 = analogRead(analogPin3);
  analogValue4 = analogRead(analogPin4);
  analogValue5 = analogRead(analogPin5);

  pulse1 = map(analogValue1,500,0 ,minPulse1,maxPulse1);
  pulse2 = map(analogValue2,500,0 ,minPulse2,maxPulse2);
  pulse3 = map(analogValue3,500,50 ,minPulse3,maxPulse3);
  pulse4 = map(analogValue4,500,50 ,minPulse4,maxPulse4);
  pulse5 = map(analogValue5,500,50 ,minPulse5,maxPulse5);
          

  
if (millis() - lastPulse1 >= refreshTime1) {
    digitalWrite(servoPin1, HIGH);  
    delayMicroseconds(pulse1);      
    digitalWrite(servoPin1, LOW);  
    lastPulse1 = millis();
}
 if (millis() - lastPulse2 >= refreshTime2) {
    digitalWrite(servoPin2, HIGH);  
    delayMicroseconds(pulse2);      
    digitalWrite(servoPin2, LOW);  
    lastPulse2 = millis();  
 }
  if (millis() - lastPulse3 >= refreshTime3) {
    digitalWrite(servoPin3, HIGH);  
    delayMicroseconds(pulse3);      
    digitalWrite(servoPin3, LOW);  
    lastPulse3 = millis();
  }  
   if (millis() - lastPulse4 >= refreshTime4) {
    digitalWrite(servoPin4, HIGH);  
    delayMicroseconds(pulse4);      
    digitalWrite(servoPin4, LOW);  
    lastPulse4 = millis();
   }  
    if (millis() - lastPulse5 >= refreshTime5) {
    digitalWrite(servoPin5, HIGH);  
    delayMicroseconds(pulse5);      
    digitalWrite(servoPin5, LOW);  
    lastPulse5 = millis();      
    }
}






 
// We got a complete packet. Do something
   }
}

You now have two loop() functions, which is not allowed. In both loop functions, you are reading analog data. I thought you were reading analog data on the sender, and just moving servos on the receiver.

This code is far more complex than it needs to be since you are not using arrays. For instance:

 int servoPin1 = 2;
 int servoPin2 = 3;
 int servoPin3 = 4;
 int servoPin4 = 5;
 int servoPin5 = 6;

should be

int servoPins[] = {2, 3, 4, 5, 6};

If the servos are all the same, why do the minimum and maximum pulse values need to be different for each servo? Why does the refresh time change?

lastPulseN is a long. millis() returns unsigned long. So, lastPulseN (or lastPulses[]) should be unsigned long.

Yes i am reading analog data on the sender, and just moving servos on the receiver is this not what is happening? and so how can i fix the loop() problem?

Also will this work also

int minPulse [] = {500};

You now have two loop() functions, which is not allowed. In both loop functions, you are reading analog data. I thought you were reading analog data on the sender, and just moving servos on the receiver.

This code is far more complex than it needs to be since you are not using arrays. For instance:
Code:

int servoPin1 = 2;
int servoPin2 = 3;
int servoPin3 = 4;
int servoPin4 = 5;
int servoPin5 = 6;

should be
Code:

int servoPins[] = {2, 3, 4, 5, 6};

If the servos are all the same, why do the minimum and maximum pulse values need to be different for each servo? Why does the refresh time change?

lastPulseN is a long. millis() returns unsigned long. So, lastPulseN (or lastPulses[]) should be unsigned long.

Also will this work also

int minPulse [] = {500};

It will, but that declares an array of length 1. Why does it need to be an array?

int minPulse = 500;

is all that is needed.

Yes i am reading analog data on the sender, and just moving servos on the receiver is this not what is happening? and so how can i fix the loop() problem?

You can't run the same sketch on the sender and the receiver. The sender needs to read the sensors and send the data to the serial port. The sender knows nothing about servo pins or servo limit values.

The receiver does NOT read any sensor data, or know anything about sensor pins.

Fixing the loop problem is easy. Remove everything from the sketch that has anything to do with reading sensor data. Then, delete everything from the 2nd loop() that is also in the first loop(). If there is anything left, move it into the first loop() function, and delete the 2nd loop function.

ooooooooo ok lol i feel stupid lol hahahah

Quote:
Also will this work also

Code:

int minPulse [] = {500};

It will, but that declares an array of length 1. Why does it need to be an array?
Code:

int minPulse = 500;

is all that is needed.

Quote:
Yes i am reading analog data on the sender, and just moving servos on the receiver is this not what is happening? and so how can i fix the loop() problem?

You can't run the same sketch on the sender and the receiver. The sender needs to read the sensors and send the data to the serial port. The sender knows nothing about servo pins or servo limit values.

The receiver does NOT read any sensor data, or know anything about sensor pins.

Fixing the loop problem is easy. Remove everything from the sketch that has anything to do with reading sensor data. Then, delete everything from the 2nd loop() that is also in the first loop(). If there is anything left, move it into the first loop() function, and delete the 2nd loop function.

but with this:

int minPulse = 500;

theres multiple ones like this:

 int minPulse1 = 500 ;
 int minPulse2 = 500 ;  
 int minPulse3 = 500 ;  
 int minPulse4 = 500 ;  
 int minPulse5 = 500 ;

Now does this work as this:

int minPulse = 500;

and can u maby help me some more with the sending part

does this code go with the sending part?

  analogValue1 = analogRead(analogPin1);
  analogValue2 = analogRead(analogPin2);
  analogValue3 = analogRead(analogPin3);
  analogValue4 = analogRead(analogPin4);
  analogValue5 = analogRead(analogPin5);

  pulse1 = map(analogValue1,500,0 ,minPulse1,maxPulse1);
  pulse2 = map(analogValue2,500,0 ,minPulse2,maxPulse2);
  pulse3 = map(analogValue3,500,50 ,minPulse3,maxPulse3);
  pulse4 = map(analogValue4,500,50 ,minPulse4,maxPulse4);
  pulse5 = map(analogValue5,500,50 ,minPulse5,maxPulse5);

and maby can you help me by putting this in the right format for sending

if (millis() - lastPulse1 >= refreshTime1) {
    digitalWrite(servoPin1, HIGH);  
    delayMicroseconds(pulse1);      
    digitalWrite(servoPin1, LOW);  
    lastPulse1 = millis();

Thank You and i will be crediting you for this btw

theres multiple ones like this:

There are, but the question was "do there need to be?" With 5 identical servos, minPulse and maxPulse should be the same for all of them.

does this code go with the sending part?

Yes. It is the code that determines where to move the servos to.

and maby can you help me by putting this in the right format for sending

Sure. On the sender, select all that code, and delete it. It is the code for actually positioning the servos, so it does on the receiver.

You won't be using code that looks anything like that, since you will be switching to the servo library, right?

You won't be using code that looks anything like that, since you will be switching to the servo library, right?

well... honestly i never got it working wit the sensors i tried but i never got it to work maybe you could help me with this also

The servo library is very easy to use. Create a servo object (or array of servo objects).

Servo myServo;

In setup(), connect the servo object to it's control pin.

myServo.attach(somePin);

Then, in loop(), move the servo to some position:

myServo.write(90); // move to 90 degrees.

yes yes buttttttt the big part tha got me was that flex sensor and how to code that to work with the servos.

but do you think it will be easier to make it wireless with the servo libary? or the code i have right now?

The flex sensor read returns a value in the range 0 to 1023. Mapping that to 0 to 180 is straightforward. The result is a value that can be used as input to the Servo::write function. What could be easier?

The flex sensor read returns a value in the range 0 to 1023. Mapping that to 0 to 180 is straightforward. The result is a value that can be used as input to the Servo::write function. What could be easier?

is this using the servo libary?

#include <Servo.h>

Servo myservo; 


int Flexsensor= 0; 
int Flexsensor=1;
int Flexsensor=2;
int Flexsensor=3;
int Flexsensor =4;
int val;    

void setup()
{
myservo.attach(1); 
myservo.attach(2); 
myservo.attach(3); 
myservo.attach(4); 
myservo.attach(5);  // 
}

void loop()
{
val = analogRead(Flexsensor);           
val = map(val, 500, 0, 500, 4000);    
myservo.write(val);                  
delay(15);                          
}

now is that it i feel like i forgot something because it was so easy lol

That should work, except that you attached the same servo to 5 different pins. Which one actually drives the servo?

The value that you write to the servo, using Servo::write, is the angle that you want the servo to move to. So, the to range of the map call should be 0, 180. The value that you are mapping from is in the range 0 to 1023.

Your map command should look like:

val = map(val, 0, 1023, 0, 179);
[quote]val = map(val, 0, 1023, 0, 179); 

[/quote]

ok i have that all firgured out because it really depends how i have my servos positions and i no those numbers i have dont make a hole lata sense but they work

Now the only problm i have is getting a xbee...

as you no on my other posts i have a budget and need two xbees and two shields or a way to put them hook them up to the arduino
any help would do

By the way this is my new code i made that eearlier one in about a couple seconds but heres the one i got working:

#include <Servo.h>

Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;


int Flexsensor0= 1;
int Flexsensor1=2;
int Flexsensor2=3;
int Flexsensor3=4;
int Flexsensor4 =5;
int val;

void setup()
{
myservo1.attach(2);
myservo2.attach(3);
myservo3.attach(4);
myservo4.attach(5);
myservo5.attach(6);  //
}

void loop()
{{{{{
val = analogRead(Flexsensor0);
val = map(val, 500, 0, 500, 4000);
myservo1.write(val);
delay(15);
}
val = analogRead(Flexsensor1);
val = map(val, 500, 0, 500, 4000);
myservo2.write(val);
delay(15);
}
val = analogRead(Flexsensor2);
val = map(val, 500, 0, 500, 4000);
myservo3.write(val);
delay(15);
}
val = analogRead(Flexsensor3);
val = map(val, 500, 0, 500, 4000);
myservo4.write(val);
delay(15);
}
val = analogRead(Flexsensor4);
val = map(val, 500, 0, 500, 4000);
myservo5.write(val);
delay(15);
}

now another thing is still the sending code now i have hopfully evething here for the sending code but not iin the sending format:

void loop()
{{{{{
val = analogRead(Flexsensor0);
val = map(val, 500, 0, 500, 4000);
myservo1.write(val);
delay(15);
}
val = analogRead(Flexsensor1);
val = map(val, 500, 0, 500, 4000);
myservo2.write(val);
delay(15);
}
val = analogRead(Flexsensor2);
val = map(val, 500, 0, 500, 4000);
myservo3.write(val);
delay(15);
}
val = analogRead(Flexsensor3);
val = map(val, 500, 0, 500, 4000);
myservo4.write(val);
delay(15);
}
val = analogRead(Flexsensor4);
val = map(val, 500, 0, 500, 4000);
myservo5.write(val);
delay(15);
}

now if you could maybe help me by explaining how to send this is the right format it would be great

val = map(val, 0, 1023, 0, 179);

ok i have that all firgured out because it really depends how i have my servos positions and i no those numbers i have dont make a hole lata sense but they work

Now the only problm i have is getting a xbee...

as you no on my other posts i have a budget and need two xbees and two shields or a way to put them hook them up to the arduino
any help would do

By the way this is my new code i made that eearlier one in about a couple seconds but heres the one i got working:

#include <Servo.h>

Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;


int Flexsensor0= 1;
int Flexsensor1=2;
int Flexsensor2=3;
int Flexsensor3=4;
int Flexsensor4 =5;
int val;

void setup()
{
myservo1.attach(2);
myservo2.attach(3);
myservo3.attach(4);
myservo4.attach(5);
myservo5.attach(6);  //
}

void loop()
{{{{{
val = analogRead(Flexsensor0);
val = map(val, 500, 0, 500, 4000);
myservo1.write(val);
delay(15);
}
val = analogRead(Flexsensor1);
val = map(val, 500, 0, 500, 4000);
myservo2.write(val);
delay(15);
}
val = analogRead(Flexsensor2);
val = map(val, 500, 0, 500, 4000);
myservo3.write(val);
delay(15);
}
val = analogRead(Flexsensor3);
val = map(val, 500, 0, 500, 4000);
myservo4.write(val);
delay(15);
}
val = analogRead(Flexsensor4);
val = map(val, 500, 0, 500, 4000);
myservo5.write(val);
delay(15);
}

now another thing is still the sending code now i have hopfully evething here for the sending code but not iin the sending format:
Code:

void loop()
{{{{{
val = analogRead(Flexsensor0);
val = map(val, 500, 0, 500, 4000);
myservo1.write(val);
delay(15);
}
val = analogRead(Flexsensor1);
val = map(val, 500, 0, 500, 4000);
myservo2.write(val);
delay(15);
}
val = analogRead(Flexsensor2);
val = map(val, 500, 0, 500, 4000);
myservo3.write(val);
delay(15);
}
val = analogRead(Flexsensor3);
val = map(val, 500, 0, 500, 4000);
myservo4.write(val);
delay(15);
}
val = analogRead(Flexsensor4);
val = map(val, 500, 0, 500, 4000);
myservo5.write(val);
delay(15);
}

now if you could maybe help me by explaining how to send this is the right format it would be great