Go Down

Topic: XBee variable send and receive (Read 1 time) previous topic - next topic

xmike

Hello
I want to send multiple variables with a XBEE and receive!

How do I do it

Michael

PaulS

Quote
I want to send multiple variables with a XBEE and receive!

Exactly the same way you would using a wire.

xmike


I have an Arduino UNO an Arduino MEGA 2 XBEE (Series 1).
And would like to connect multiple Poti on Arduino MEGA and the XBEE module to send Arduino UNO and several servo control.
So with potentiometer 1 servo 1
       with potentiometer 2 servo 2
       with potentiometer 3 servo 3
        etc. .. Taxes!

PaulS

Quote
And would like to connect multiple Poti on Arduino MEGA and the XBEE module to send Arduino UNO and several servo control.
So with potentiometer 1 servo 1
       with potentiometer 2 servo 2
       with potentiometer 3 servo 3
        etc. .. Taxes!


Suppose that you connected TX of the UNO to RX of the Mega, and RX of the UNO to TX of the Mega, and connected the grounds. You'd use Serial.print(), Serial.println(), or Serial.write() to send the data and you'd use Serial.available() and Serial.read() to receive the data.

Well, guess what. You'd use EXACTLY the same code with the XBees in place, instead of the wires.

xmike

this is my cod of the transmitter
Code: [Select]

int potPin_1 = 0;      // 1. Poti
int potPin_2 = 1;      // 2. Poti
int potPin_3 = 2;      // 3. Poti

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

void loop()
{
  int potVal = analogRead(potPin_1);         
  byte servoVal_1 = map(potVal,0,1023,0,179);
 
int potVal = analogRead(potPin_2);         
  byte servoVal_2 = map(potVal,0,1023,0,179);

  int potVal = analogRead(potPin_3);         
  byte servoVal_3 = map(potVal,0,1023,0,179);
 
  Serial.write(servoVal_1);     
  Serial.write(servoVal_2);     
  Serial.write(servoVal_3);   
  delay(100);                 

how can my cod for the receiver look!

I mean as I kan variable (servoVal_1, servoVal_2, servoVal_3) and receive the correct Servo to assign?

PaulS

Serial data is not guaranteed to be delivered. As your sender is constructed, the receiver would read a byte, and assume it was for servo 1. Then, it would read a byte, and assume it is for servo 2. Then, it would read another byte, and assume that it is for servo 3.

Now, suppose a byte got lost. Say the one for servo 2. Servo 3's data would be read, and sent to servo 2. Then, servo 1's data would be read and sent to servo 3. Then, servo 2's data would be read and sent to servo 1.

Doesn't sound good, does it?

Sending something like "<1:120>" would be better, wouldn't it? No doubt what the value is, or which servo it is for. You could read the data using code like this:

Code: [Select]
#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

Where it says "Process the packet", you'd use strtok() to extract the servo number (as a character) and the servo value, as a string, and atoi() to convert the string to a number.

xmike

Hi PaulS

I am like any on line!

I getesten so much and I'm in no branch!
Work only 1 month with Arduino and still do not understand anything!

I want to make something finite like that!
http://www.youtube.com/watch?v=BSsC_mi9BPQ

PaulS

Code: [Select]
  Serial.write(servoVal_1);
should become
Code: [Select]
  Serial.write("<1,");
  Serial.write(servoVal_1);     
  Serial.write(">");


The receiver code that I posted should be easy to modify, to parse a string like "1,145" (which is what would be in inData, if servoVal_1 was 145.

Give it your best attempt, and I'll correct any issues.

xmike

Hello
Today I tested with XBee.h What and now I receive Pot'i of first dates!
But it captures the power of nothing or I do something wrong?

Cod transmitter
Code: [Select]

#include <XBee.h>

int potPin = 0;
//XBEE------------------------------------------------------------------
XBee xbee = XBee();
uint8_t payload[] = {0,0};
//uint8_t payload[] = {0};
Tx16Request tx = Tx16Request(0x0055, payload, sizeof(payload)); // 0X0055 ist die MYID

void setup()
{
  Serial.begin(9600);
  Serial.print("Bereit!");
  xbee.begin(9600);
}
void loop()
{
   //was auch immer machen
int potVal = analogRead(potPin);
  byte servoVal = map(potVal,0,1023,0,179);
   payload[0] = (servoVal); // werte setzen
   //...
   
   Serial.println(servoVal);
   xbee.send(tx);
   delay(100);
}

Cod receiver
Code: [Select]

#include <Servo.h>

#include <XBee.h>
Servo servo_1;


//XBEE------------------------------------------------------------------
XBee xbee = XBee();
Rx16Response rx16 = Rx16Response();
void setup()
{
  xbee.begin(9600);
  servo_1.attach(9);
}
void loop()
{
  if (xbee.readPacket(5000))
  {   
    if (xbee.getResponse().isAvailable())
    {
       xbee.getResponse().getZBRxResponse(rx16);
       Serial.println(rx16.getData()[0]);
    {
       servo_1.write(rx16.getData()[0]);
    } 
}
   
}
}



Michael

PaulS

Quote
I do something wrong?

Yes.

Code: [Select]
Tx16Request tx = Tx16Request(0x0055, payload, sizeof(payload)); // 0X0055 ist die MYID
This does not bind payload and tx. It simply copies the data in payload to the rx instance.

Code: [Select]
   payload[0] = (servoVal); // werte setzen
The parentheses are useless. You are assigning a new value to payload, but:
Code: [Select]
   xbee.send(tx);
sending the old value.

Code: [Select]
    {
       servo_1.write(rx16.getData()[0]);
    } 

Useless curly braces.

xmike

Hi PaulS

I did not understand your answer!
How Solte dan see my Cod right out?

Can you tell me please please help me continue to ben on despair?

Michael

PaulS

Quote
I did not understand your answer!
How Solte dan see my Cod right out?

It's only fair that you not understand my answer. I don't understand this question.

After you have a payload to be sent, you need to create an instance of the Tx16Request class to send the data.
Code: [Select]
  int potVal = analogRead(potPin);
  byte servoVal = map(potVal,0,1023,0,179);
  payload[0] = servoVal; // werte setzen
 
  Tx16Request tx = Tx16Request(0x0055, payload, sizeof(payload)); // 0X0055 ist die MYID

  Serial.println(servoVal);
  xbee.send(tx);

xmike

Hi PaulS
again from the beginning!
I would like to. with one each pot'i a servo control (RC remote control)
  I build models of rc in scale 1/32 of Siku and liked me for these models build a remote control!

Michael

PaulS

Quote
I would like to. with one each pot'i a servo control (RC remote control)

The snippet I fixed does that for one pot for one servo.

If you want to do it for more than one pot for more than one serve, you need to make the payload array larger and store more values in it.

When all the values have been stored in it, create the tx instance and send it.

xmike

Hi PaulS

we show ourselves the right to request that!

http://www.youtube.com/watch?v=LWzb_MFYs7k

And that's what I liked a remote control with Arduino and xbee.

Go Up