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.

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

 

}

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 "

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.

 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 

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

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 problem, and the solution:

Serial.println('LFT');

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 .