Let's Make Robots!

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 .

 

 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

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

the reciever is recieving some weird stuff eg 

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

The "ÿÿÿÿ" characters could be from framing errors. I've used a pull-up resistor on my com lines to clear up similar problems.

Edit: What does "Serial.read()" return when the UART buffer is empty? I'm betting "ÿ".

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 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.

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 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);

}

}

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 .

 

Replace

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

by:

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

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);

 

 

 

}