Go Down

Topic: 2 XBees, 2 Arduinos, numbers in terminal don't match... (Read 927 times) previous topic - next topic

czyskows

Hi. I'm trying to do what should be a simple wireless send/receive setup with 2 XBees and 2 Arduinos. On the send side, I have a potentiometer plugged into analog input 0 on  arduino 1. The TX RX cables of XBee1 are connected to the RX TX of arduino1. Then, on the receive side, I have another arduino with a Lillypad vibrating motor attached to pin9 for PWM output. The TX RX of XBee2 are connected to the RX TX of this arduino. When I connect the sending arduino (with the potentiometer) to the computer via usb, I get numbers 0 - 600 as I move the knob, which is what I would expect. However, when I connect the receiving arduino and check the serial terminal, as I move the pot, I get 0, 1, 2, 3, and then at the top of the dial, -38. The numbers move as I move the knob, but not in the way they should. Any ideas? Thanks in advance for your help.

Colin

markbee

It would be a good idea to see the sketch. Otherwise the error is on line 42.

And: How are the Xbees configured? What type of Xbees?

markbee
XBee blog: http://lookmanowire.blogspot.com/

czyskows

Here is the code for both Arduinos.

The pot...

Code: [Select]


//ARDUINO POT

//Send data from a POT over serial to the computer

//Define Pins
int potPin = 0;

void setup()
{
  //Create Serial Object (9600 Baud)
  Serial.begin(9600);
}

void loop()
{
 
  int val = analogRead(potPin);
 
  Serial.println(val);
  delay(50);
 
}


and the motor...

Code: [Select]


//ARDUINO MOTOR

//Define Pins
int motorPin = 9;


void setup()
{
//Start Serial
Serial.begin(9600);
 
delay(500);
}

void loop()


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

  int pos = map(data, 0, 600, 0, 255);

  analogWrite(motorPin, pos);
 
  Serial.println(pos);

  Serial.flush(); 
 
 
}

Graynomad

#3
Jul 23, 2011, 03:07 am Last Edit: Jul 23, 2011, 03:10 am by Graynomad Reason: 1
val is coming through as 1, 2 or 3 characters depending on it's magnitude, plus a \r\n.

You only wait for the first character then do the work. I would start by just doing

Serial.print (data,HEX)

at the receive end to see exactly what you are getting, this will also verify the Xbee link.

Then you will need to wait for all characters by testing for the \r\n, something like this

Code: [Select]
pos = 0;
if (Serial.available() > 0)
  c = Serial.read();

  switch (c) {

    case '\r':
       analogWrite(motorPin, pos);
pos = 0;
break;

    case '\n':
break;

   default:
pos *= 10;
pos += c;
break;
  }
}


______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

czyskows

Hi,

Thanks for the help! Unfortunately, it's still not working.  When I use

Serial.println(pos, HEX);

I get the same 0,1,2,3, and then FFFFFFDD when the pot gets to the top.

When I use the switch case, I just get 0 in the terminal.

I'm kind of new at this stuff... Can you tell me what the \r\n mean?  Is that part of the packet that's being sent from the XBee?

Thanks again.

Colin

Graynomad

Your println(val) will send a varying number of characters depending on the magnitude of val. It will also add a CR/LF (aka "\r\n") as the last two characters.

Serial.println (1234); sends '1', '2', '3', '4', 13, 10  (13 = CR, 10 = LF)
Serial.println (123); sends '1', '2', '3',13, 10 
Serial.println (12); sends '1', '2', 13, 10 

So you can't just read a single character and use that. You have to accumulate the values until you get the CR ('\r') then use the value.

That is what my code snippet should do, however I did just spot a bug, I wasn't converting the characters back to binary.

Code: [Select]
pos = 0;
if (Serial.available() > 0)
   c = Serial.read();

   switch (c) {

     case '\r':
        analogWrite(motorPin, pos);
pos = 0;
break;

     case '\n':
break;

    default:
pos *= 10;
pos += c - '0';
break;
   }
}


However before you do that I'm suggesting that you just print the values received from the Xbee, that will tell you if it's working or not.

So get rid of everything and just do

Code: [Select]
  while( Serial.available() == 0);
  int data = Serial.read() ;
  Serial.println(data);
 


It's best to test one thing at a time rather that staring at the end result and wondering why it's not correct.

______
Rob



Rob Gray aka the GRAYnomad www.robgray.com

czyskows

Hi again,

First, thanks for taking the time to help me with this. I'm a noob and pretty confused.  Anyway... I tried the code that just polls the terminal.   What I get at the top of the pot is 48,13,10.  In the middle, it's 54,52,52,13,10.  At the bottom, it's 54,52,56,13,10.  I don't know if this suggests that the XBees are working or not. It seems to mean that they're transmitting, but I don't know if what they're transmitting makes any sense.

The code that I then tried using for the motor end is this:

Code: [Select]


//Define Pins
int motorPin = 9;
int c;

void setup()
{
//Start Serial
Serial.begin(9600);
 
delay(500);
}

void loop()


int pos = 0;
if (Serial.available() > 0)
int  c = Serial.read();

   switch (c) {

     case '\r':
        analogWrite(motorPin, pos);
pos = 0;
break;

     case '\n':
break;

    default:
pos *= 10;
pos += c - '0';
        Serial.println(pos);
break;
   }
}
[\code]

Is that how the whole thing should look?  When I upload this code, all i get is -48 over and over again.  Am I missing something obvious here??? Again, your help is greatly appreciated. 

Colin

czyskows

When I upload this code, all I get is -48.  Again, I'm a noob. Is there some dumb mistake I'm making in the code??? And again, thanks for your help!

Colin

Graynomad

#8
Jul 28, 2011, 03:33 am Last Edit: Jul 28, 2011, 03:37 am by Graynomad Reason: 1
Those numbers are decimal representations of ASCII chars

48,13,10 == 0 CR LF
54,52,52,13,10 == 6 4 4 CR LF
54,52,56,13,10 == 6 4 6 CR LF

So at the top you are getting 0, makes sense, depending on how the pot is wired.
In the middle you are getting 644, makes sense.
In the bottom you are getting 646, doesn't make sense, are you sure about this one?

So it looks like the Xbees are working.

As for the -48, I may have stuffed up with that code, I'll have a look. -48 == -0 which should give me a hint.

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

Graynomad

OK I think the problem is that my code snippet was not written to be entirely inside a function (in this case loop())

Why? Because having

int pos = 0;

inside a function initializes pos to 0 every time the function runs which in this case is probably a 1000 times for every character received. So no matter how many chars have been received before pos will always be 0 and when you finally get a CR that's the value that is printed.

Changing that line to

static int pos = 0;

should fix the problem. This causes the variable to only be initialized the first time the function is called.

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

Go Up