Arduino + Xbee S1 +,Wii nunchuck

Hey there!
I have a wii nunchuck and a couple of arduinos and xbee’s. I am trying to send the Joystick X values over xbee to the other arduino with a xbee and to print it out to the serial monitor on my computer.

However i am getting a lot of junk on the serial monitor. And im not sure why.

Heres the code

Reciever:

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


void loop(){
  
 while (Serial.available() > 0){
   
   Serial.println(Serial.read() - '0');

 }
 
 
}

Transciever:

#include <Wire.h>
#include <Servo.h>
const int vccPin = A3;
const int gndPin = A2;

Servo servo;

const int dataLength = 6; // Number of bytes to request 

static byte rawData[dataLength];

enum nunchuckItems { 
  JoyX, JoyY, accelX, accelY, accelZ, btnZ, btnC};

void setup()
{
  pinMode(gndPin, OUTPUT);
  pinMode(vccPin, OUTPUT);
  digitalWrite(gndPin, LOW);
  digitalWrite(vccPin, HIGH);
  servo.attach(9);

  delay(1000);
  Serial.begin(9600);

  nunchuckInit();
}

void loop()
{
  nunchuckRead();

 
  
  int joyX = getValue(JoyX);
  Serial.println(joyX);
 
  

  }

  void nunchuckInit(){

    Wire.begin();
    Wire.beginTransmission(0x52);
    Wire.write((byte)0x40);
    Wire.write((byte)0x00);
    Wire.endTransmission();

  }

static void nunchuckRequest(){
  Wire.beginTransmission(0x52);
  Wire.write((byte)0x00);
  Wire.endTransmission(); 
}

boolean nunchuckRead(){

  int cnt = 0;
  Wire.requestFrom(0x52, dataLength);

  while (Wire.available()){

    rawData[cnt] = nunchuckDecode(Wire.read());
    cnt++;
  } 

  nunchuckRequest();
  if (cnt >= dataLength)
    return true;
  else 
    return false;

}


static char nunchuckDecode(byte x){
  return (x ^ 0x17) + 0x17;

}

int getValue(int item){

  if (item <= accelZ)
    return (int)rawData[item];
  else if (item == btnZ)
    return bitRead(rawData[5], 0) ? 0: 1;
  else if (item == btnC)
    return bitRead(rawData[5], 1) ? 0: 1;

}

Thank you and any suggestions will help!

However i am getting a lot of junk on the serial monitor. And im not sure why.

Are you using the Serial instance to talk to the XBee or to talk to the PC? Both is the wrong answer. So is yes.

How are the XBees configured? How are the XBees connected to the Arduinos?

Both xbees are connected to the arduinos with arduino wireless proto shields and they are configure correctly.
Im sorry i didnt understand your first question

Im sorry i didnt understand your first question

You have a phone, right? It's like a serial port. It can be used to talk to one other phone at a time. You can't call your girlfriend and your mother at the same time.

You can't use the Serial port to talk to the PC (the Serial Monitor) and the XBee at the same time.

Oh ok i get what you mean!

So can you suggest a way to confirm i am getting the right data?

So can you suggest a way to confirm i am getting the right data?

From what? On what?

I know my xbee's are working and that my arduinos can comunicate wireless because of other simple tests.

But for some reason it doesnt seem to be working properly with the above code...

On one arduino i have attached a wii nunchuck and i am getting the Joystick values (30 - 225) and sending them over serial:

int joyX = getValue(JoyX);
Serial.println(joyX);

On the other arduino i want to collect that data:

if (Serial.available() > 0){

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

}

But im having trouble confirming im getting the right data Since i cant use Serial.print() to check.
And every test i try it doesnt work....
e.g

if(data > 126){

digitalWrite(13, HIGH);

}

But the light wont light no matter what.

Please help! its very frustrating.

Thanks !!

On one arduino i have attached a wii nunchuck and i am getting the Joystick values (30 - 225) and sending them over serial:

int joyX = getValue(JoyX);
Serial.println(joyX);

What the XBee is sending, when the value is 175 is '1', '7', '5', '\r', and '\n'. Which of those 5 values is the one of interest?

What does '\r' - '0' mean? How about '\n' - '0'?

Remove the XBee from the sender. Connect the sender to the PC. Are you seeing reasonable values in the Serial Monitor?

If so, put the XBee back on. On the receiver end, store the received data in a char, since that is what you sent. You did NOT send ints. Stop subtracting '0' from every byte received. Now, do you see meaningful data in the Serial Monitor for the receiver?

I will try this out! And yes on the sender end i know i am getting reasonable data. But how come the data im sending are not ints?

Thanks for the help!

But how come the data im sending are not ints?

Because you are sending characters.