How do I receive more than one sensor value wirelessly using 2 xbees and 2 arduino megas

 

Im very new to useing xbee with my arduino and ive found some tutorials online showing me how to control one servo wirelessly with one  potentiometer but im unsure on how i would control multiple servos with multiple potentiometers at the same time .Im wanting to use this code for a project that im working on to control a humanoid robot wirelessly useing a suit that I would wear that has potentiometers on the joints . Here is the code I am currently useing to controle just one servo with one potentiometer  :

sender:

 

// SENDER

int potpin = 0;

 

void setup() 

{

// Serial port enable

Serial.begin(9600);

}

 

void loop() {

// read analog pin 5

int val = map(analogRead(potpin), 0, 1023, 0, 9);

Serial.println(val);

delay(50);

}

 

reciever :

 

#include <Servo.h>

int led = 13;

int servopin = 8;

 

Servo hand;

 

void setup()

{

  Serial.begin(9600);

  pinMode(led, OUTPUT); 

  hand.attach(servopin);

}

 

void loop()

{

  digitalWrite(led, HIGH);

 

  while(Serial.available() == 0);

 

  int data = Serial.read() - '0';

  int pos = map(data, 0, 9, 0, 180);

  pos = constrain(pos, 0, 180);

 

 

  hand.write(pos);

  delay(15);

while(Serial.available()>0) Serial.read(); // used instead of the flush command

}

 

 

Any help on this would be hugely appreciated .

 

 

The easiest way to do it is

The easiest way to do it is to send a letter and a 3 digit value and parse on the receiver.

Something like for servo 1 angle 90, you send A090, for servo 2 angle 180, you send B180.

here is some code i used for

here is some code i used for my iphone contrlled robot it uses the same thing beat_slayer talked about.

 

const int left1 = 9;

const int left2 = 10;

const int right1 = 4;

const int right2 = 5;

int enable1 = 11; 

int enable2 = 6;

 

char topchan = ‘a’;

char midchan = ‘b’;

char downchan = ‘c’;

char rightchan = ‘d’;

char leftchan = ‘e’;

char sliderchan = ‘s’;

char serialchar = 0;

 

void setup()

{

 

  pinMode(left1,OUTPUT);

  pinMode(left2,OUTPUT);

  pinMode(right1,OUTPUT);

  pinMode(right2,OUTPUT);

  pinMode(enable1,OUTPUT);

  pinMode(enable2,OUTPUT);

 

  Serial.begin(9600);

}

 

 

void loop(){

 

while(Serial.available() <=0);  //Wait for a character on the serial port.

  serialchar = Serial.read();     //Copy the character from the serial port to the variable

  if(serialchar == topchan){  //Check to see if the character is the servo ID for the tilt servo

    forward();

    //while(Serial.available() <=0);  //Wait for the second command byte from the serial port.

    //analogWrite(left2,Serial.read());

  }

 

  if(serialchar == downchan){ //Check to see if the initial serial character was the servo ID for the pan servo.

    back();

    //while(Serial.available() <= 0);  //Wait for the second command byte from the serial port.

    //analogWrite(right1,Serial.read());

  }

 

  if(serialchar == rightchan)

  {

    right();

  }

 

  if(serialchar == leftchan)

  {

    left();

  }

  if(serialchar == midchan)

  {

    halt();

  }

  if(serialchar == sliderchan){ //Check to see if the initial serial character was the servo ID for the pan servo.

    while(Serial.available() <= 0);  //Wait for the second command byte from the serial port.

    int i = Serial.read();

 

    analogWrite(enable1,i);

    analogWrite(enable2,i);

  }

 

}

 

 

 

void forward()

{

 

  digitalWrite(left1,LOW);

  digitalWrite(left2,HIGH);

  digitalWrite(right1,HIGH);

  digitalWrite(right2,LOW);

  digitalWrite(enable1,HIGH);

  digitalWrite(enable2,HIGH);

 

}

 

 

void back()

 

{

 

  digitalWrite(left1,HIGH);

  digitalWrite(left2,LOW);

  digitalWrite(right1,LOW);

  digitalWrite(right2,HIGH);

  digitalWrite(enable1,HIGH);

  digitalWrite(enable2,HIGH);

 

 

 

void left()

 

{

 

  digitalWrite(left1,HIGH);

  digitalWrite(left2,LOW);

  digitalWrite(right1,HIGH);

  digitalWrite(right2,LOW);

  digitalWrite(enable1,HIGH);

  digitalWrite(enable2,HIGH);

 

}

 

void right()

 

{

 

  digitalWrite(left1,LOW);

  digitalWrite(left2,HIGH);

  digitalWrite(right1,LOW);

  digitalWrite(right2,HIGH);

  digitalWrite(enable1,HIGH);

  digitalWrite(enable2,HIGH);

 

}

 

void halt()

{

 

  digitalWrite(left1,LOW);

  digitalWrite(left2,LOW);

  digitalWrite(right1,LOW);

  digitalWrite(right2,LOW);

  digitalWrite(enable1,LOW);

  digitalWrite(enable2,LOW);

 

}

**hmmm ok still a little unsure **

im not the best with programing but i understand what you mean by putting a letter in front of the variable im sending to differentiate between the values . 

there are some parts i dont really understand of the code :

while(Serial.available() <=0);  //Wait for a character on the serial port.

  serialchar = Serial.read();     //Copy the character from the serial port to the variable

  if(serialchar == topchan){  //Check to see if the character is the servo ID for the tilt servo    here you are just reading the letter but im not sure how I would read the letter in front of the variable and then the variable by its self.

    forward();

    //while(Serial.available() <=0);  //Wait for the second command byte from the serial port.

    //analogWrite(left2,Serial.read()); 

and im still a little unsure on the sending of the variables with the character in front of them

"sorry about the bold i just did that so you could see what i type and dont miss it in the code "

** what you would recomend**

 what you would recomend would be to convert the data to ascii with other ascii characters either side of the data that i turned in to ascii then I would some how pick out the data i converted into ascci on the receveing end is this right? If you could type some sudo code for ruffly how i would do this that be really usefull because im still unsure of how i would go about doing that the sending of the code doesnt seem to bad but the recieveing is still quite confuseing . the takeing out each of the segments i send it from the overall chunk is abit confuseing . and im unsure what parseing is and how it can be used.

"and i havent used dynamixel servos before but i do under stand what you mean when you were talking about them "

and thank you every one for the help so far i really appreciate it 

Ah god I’m stupid I used the
Ah god I’m stupid I used the tecknique in a specific part of the code
Here it is

if(serialchar == sliderchan){ //Check to see if the initial serial character was the servo ID for the pan servo.

while(Serial.available() <= 0);  //Wait for the second command byte from the serial port.

int i = Serial.read();



analogWrite(enable1,i);

analogWrite(enable2,i);

}

Use that code and it will for sure work what the code is doing is it checks if the serial port is reaceving anything(that’s the while loop beneath void loop.) then it reads the byte in the serial buffer to see if it got a letter s or something. Then it reads the bytes after it I controlled 6 LEDs from processing this way remember the serial buffer should recieve something like this s255.

so would my code look some thing like this?

#include <Servo.h> 

 

  Servo rwrist;

 //RIGHT SIDE

Servo rTshoulder;

  //4

Servo rShoulder;

Servo rArm;

Servo rHand;

 

//Initiate variables for servos on LEFT SIDE

Servo lTShoulder;

Servo lShoulder;

Servo lArm;

Servo lHand;

Servo lWrist;

 

// variables for other parts

Servo head;

Servo Twaist;

 

 

 

char serialchar = 0;

char rhandchar =‘h’;

char rarmchar =‘a’;

 

 

 

void setup() 

{

 

    rArm.attach(12);

  rHand.attach(11);  // attaches the servo on pin 10 to the servo object  

  rTshoulder.attach(9);  // attaches the servo on pin 9 to the servo object 

  rShoulder.attach(10);  // attaches the servo on pin 10 to the servo object      

  rwrist.attach(13);  // attaches the servo on pin 10 to the servo object   

  //********************************************************************  

  lTShoulder.attach(3);

  lShoulder.attach(2);  // attaches the servo on pin 10 to the servo object  

  lHand.attach(6);  // attaches the servo on pin 9 to the servo object 

  lArm.attach(5);  // attaches the servo on pin 10 to the servo object 

  lWrist.attach(7);

  //**************************************************************************

  head.attach(4); // attaches servo on pin 8 to servo object

  Twaist.attach(8);

 

// Serial port enable

Serial.begin(9600);

}

 

void loop() {

  while(Serial.available() <=0);  //Wait for a character on the serial port.

 

  serialchar = Serial.read();     //Copy the character from the serial port to the variable

if(serialchar == rhandchar){ //Check to see if the initial serial character was the servo ID for the pan servo.

 

while(Serial.available()){

int i = Serial.read();

rHand.write(i);

  }

  }

if(serialchar == rarmchar){

 

while(Serial.available()){

int i = Serial.read();

rArm.write(i);

 

  }

  }

 

 

}

 

You’re using int I for both
You’re using int I for both arm and hand should be 2 different vars otherwise I think it’s fine just try it and see if it works I’m in a hurry gotta get to school

hmm I cant seem to be able to get it working here is my code

the reciever :

 

#include <Servo.h> 

 

  Servo rwrist;

 //RIGHT SIDE

Servo rTshoulder;

  //4

Servo rShoulder;

Servo rArm;

Servo rHand;

 

//Initiate variables for servos on LEFT SIDE

Servo lTShoulder;

Servo lShoulder;

Servo lArm;

Servo lHand;

Servo lWrist;

 

// variables for other parts

Servo head;

Servo Twaist;

 

void setup(){

 

      rArm.attach(13);

  rHand.attach(2);  // attaches the servo on pin 10 to the servo object  

  rTshoulder.attach(9);  // attaches the servo on pin 9 to the servo object 

  rShoulder.attach(10);  // attaches the servo on pin 10 to the servo object      

  rwrist.attach(13);  // attaches the servo on pin 10 to the servo object   

  //********************************************************************  

  lTShoulder.attach(3);

  lShoulder.attach(2);  // attaches the servo on pin 10 to the servo object  

  lHand.attach(6);  // attaches the servo on pin 9 to the servo object 

  lArm.attach(5);  // attaches the servo on pin 10 to the servo object 

  lWrist.attach(7);

  //**************************************************************************

  head.attach(4); // attaches servo on pin 8 to servo object

  Twaist.attach(8);

 

// Serial port enable

Serial.begin(9600);

 

 

}

 

 

void loop() {

 

 

 

  while(Serial.available() <=0);

 

String cmd1;

for(byte i = 0; i < 3; i++) {

  char c = Serial.read();

  cmd1 += c;

  }

 

String cmd2;

for(byte i = 0; i < 3; i++) {

  char c = Serial.read();

  cmd2 += c;

    }

 

byte cmd3 = 0;

while(Serial.available()) {

  if(Serial.available() == 3) cmd3 += Serial.read() * 100;

  if(Serial.available() == 2) cmd3 += Serial.read() * 10;  

  if(Serial.available() == 1) cmd3 += Serial.read();

   }

 

if(cmd1 == “LFT”) {

  if(cmd2 == “HND”) lHand.write(cmd3);

  delay(100);

 

      }

 

and the sender :

// SENDER

int potpin = 0;

int potpin1 = 1;

void setup() 

{

// Serial port enable

Serial.begin(9600);

}

 

void loop() {

// read analog pin 5

int val = map(analogRead(potpin), 0, 1023, 0, 180);

int val1 = map(analogRead(potpin1), 0, 1023, 0, 180);

Serial.println(‘LFT’);

Serial.print(‘HND’);

Serial.print(val);

//Serial.println(val1);

//Serial.print(‘h’);

delay(50);

 

 

 

}

Here is your

Here is your problem, and the solution:

Serial.println(‘LFT’);

**hmmm still doesnt work **

i also tryed changeing the delay at the end of the send code to one second and that didnt work . should i be turning on the reciever then the transmitter or doesnt that matter . also they dont seem to be connecting to each other now .

 

Replacewhile(Serial.available

Replace

while(Serial.available() <=0);

by:

if (Serial.available() != 0);

still doesnt seem to be working

it seems to be sending fine as far as i can tell its sending it shows this in the serial monitor   1800420036180 then repeats 180042003618018004200361801800420036180 should there be a gap or something inbetween?

heres my code again :

 

#include <Servo.h> 

 

  Servo rwrist;

 //RIGHT SIDE

Servo rTshoulder;

  //4

Servo rShoulder;

Servo rArm;

Servo rHand;

 

//Initiate variables for servos on LEFT SIDE

Servo lTShoulder;

Servo lShoulder;

Servo lArm;

Servo lHand;

Servo lWrist;

 

// variables for other parts

Servo head;

Servo Twaist;

 

void setup(){

 

      rArm.attach(13);

  rHand.attach(2);  // attaches the servo on pin 10 to the servo object  

  rTshoulder.attach(9);  // attaches the servo on pin 9 to the servo object 

  rShoulder.attach(10);  // attaches the servo on pin 10 to the servo object      

  rwrist.attach(13);  // attaches the servo on pin 10 to the servo object   

  //

****************************************************************  

  lTShoulder.attach(3);

  lShoulder.attach(2);  // attaches the servo on pin 10 to the servo object  

  lHand.attach(13);  // attaches the servo on pin 9 to the servo object 

  lArm.attach(5);  // attaches the servo on pin 10 to the servo object 

  lWrist.attach(7);

  //**************************************************************************

  head.attach(4); // attaches servo on pin 8 to servo object

  Twaist.attach(8);

 

// Serial port enable

Serial.begin(9600);

 

 

}

 

 

void loop() {

 

 

 

if (Serial.available() != 0);

 

String cmd1;

for(byte i = 0; i < 3; i++) {

  char c = Serial.read();

  cmd1 += c;

  }

 

String cmd2;

for(byte i = 0; i < 3; i++) {

  char c = Serial.read();

  cmd2 += c;

    }

 

byte cmd3 = 0;

while(Serial.available()) {

  if(Serial.available() == 3) cmd3 += Serial.read() * 100;

  if(Serial.available() == 2) cmd3 += Serial.read() * 10;  

  if(Serial.available() == 1) cmd3 += Serial.read();

   }

 

if(cmd1 == “LFT”) {

  if(cmd2 == “HND”) lHand.write(cmd3);

  delay(100);

 

      }

 

and the sender : 

// SENDER

int potpin = 0;

int potpin1 = 1;

void setup() 

{

// Serial port enable

Serial.begin(9600);

}

 

void loop() {

// read analog pin 5

int val = map(analogRead(potpin), 0, 1023, 0, 180);

int val1 = map(analogRead(potpin1), 0, 1023, 0, 180);

Serial.print(‘LFT’);

Serial.print(‘HND’);

Serial.print(val);

//Serial.println(val1);

//Serial.print(‘h’);

delay(1000);

 

}

Yeah you’re right my fault

Yeah you’re right my fault for don’t see it at first time, you’re not  using right the if statement, replace:

if (Serial.available() != 0);

by

if (Serial.available() != 0){

String cmd1;

for(byte i = 0; i < 3; i++) {

if(cmd1 == “LFT”) {

if(cmd2 == “HND”) lHand.write(cmd3);

delay(100);

}

}

still doesnt seem to be working

ive changer the if statement  like you said  would it have some thing to do with my sending code? :

 

reciever code:

 

#include <Servo.h> 

//Initiate variables for servos on LEFT SIDE

Servo lTShoulder;

Servo lShoulder;

Servo lArm;

Servo lHand;

 

void setup(){

 

  lTShoulder.attach(3);

  lShoulder.attach(2);  // attaches the servo on pin 10 to the servo object  

  lHand.attach(13);  // attaches the servo on pin 9 to the servo object 

  lArm.attach(5);  // attaches the servo on pin 10 to the servo object 

  lWrist.attach(7);

  //

**********************************************************************

  head.attach(4); // attaches servo on pin 8 to servo object

  Twaist.attach(8);

 

// Serial port enable

Serial.begin(9600);

 

 

}

 

 

void loop() {

 

 

 

if (Serial.available() != 0){

 

String cmd1;

for(byte i = 0; i < 3; i++) {

  char c = Serial.read();

  cmd1 += c;

  }

 

String cmd2;

for(byte i = 0; i < 3; i++) {

  char c = Serial.read();

  cmd2 += c;

    }

 

byte cmd3 = 0;

while(Serial.available()) {

  if(Serial.available() == 3) cmd3 += Serial.read() * 100;

  if(Serial.available() == 2) cmd3 += Serial.read() * 10;  

  if(Serial.available() == 1) cmd3 += Serial.read();

   }

 

if(cmd1 == “LFT”) {

  if(cmd2 == “HND”) lHand.write(cmd3);

  delay(100);

}

      }

 

sending code:

// SENDER

int potpin = 0;

int potpin1 = 1;

void setup() 

{

// Serial port enable

Serial.begin(9600);

}

 

void loop() {

// read analog pin 5

int val = map(analogRead(potpin), 0, 1023, 0, 180);

int val1 = map(analogRead(potpin1), 0, 1023, 0, 180);

Serial.print(‘LFT’);

Serial.print(‘HND’);

Serial.print(val);

 

delay(1000);

 

}

SENDER

SENDER CODE

###########################

int potpin = 0;

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

void loop() {
  int val = map(analogRead(potpin), 0, 1023, 0, 180);
  Serial.print(‘LFTHND’);
  Serial.print(val);
  delay(500);
}

###########################

RECEIVER CODE

###########################

#include <Servo.h>

Servo rTshoulder;
Servo rShoulder;
Servo rArm;
Servo rWrist;
Servo rHand;

Servo lTShoulder;
Servo lShoulder;
Servo lArm;
Servo lHand;
Servo lWrist;

Servo head;
Servo Twaist;

void setup() {
  rTshoulder.attach(9);
  rShoulder.attach(10);
  rArm.attach(13);
  rWrist.attach(12);
  rHand.attach(2);

  lTShoulder.attach(3);
  lShoulder.attach(4);
  lArm.attach(5);
  lWrist.attach(7);
  lHand.attach(11);

  head.attach(6);
  Twaist.attach(8);
 
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() != 0) {
    char cmd1[3];
    for (byte i = 0; i < 3; i++) {
      char c = Serial.read();
      cmd1[i] = c;
    }
    Serial.print(cmd1);
    char cmd2[3];
    for (byte i = 0; i < 3; i++) {
      char c = Serial.read();
      cmd2[i] = c;
    }
    Serial.print(cmd2);
    byte cmd3 = 0;
    while(Serial.available()) {
      if(Serial.available() == 3) cmd3 += Serial.read() * 100;
      if(Serial.available() == 2) cmd3 += Serial.read() * 10; 
      if(Serial.available() == 1) cmd3 += Serial.read();
    }
    Serial.print(cmd3);
    if (cmd1 == “LFT”) {
      Serial.print(“LEFT”);
      if(cmd2 == “HND”) {
        Serial.print(“HAND”);
        lHand.write(cmd3);
      }
      delay(100);
    }
  }
}

###########################

Run this code and see what are the replies by the receiver code, I’ve also remapped some ports since they  were used by two servos.

ok ive done that still not working

the reciever is recieving some weird stuff eg 

N!00ÿÿÿÿÿ N!00ÿÿÿÿÿ N!03ÿÿÿÿÿ N!06ÿÿÿÿÿ N!04ÿÿÿÿÿ N!512ÿÿÿÿÿ N!00ÿÿÿÿÿ N!00ÿÿÿÿÿ

**ok thanks **

where would i connect the resistor to ? im not really sure where my com line is