Arduino + WIi nunchuck

Hi!
I am using the accelerometer in the wii nunchuck, i wrote some code that works pretty well but when i go to print the accelerometer X values after i pass a certain amount around 140 some weird symbols start appearing before the numbers

Here is the code:

#include <Wire.h>

const int vccPin = A3;
const int gndPin = A2;

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

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

  nunchuckInit();
}

void loop()
{
  nunchuckRead();

  int acceleration = getValue(accelX);
    Serial.println(acceleration);


  if ((acceleration >= 75) && (acceleration <= 185)){
    byte x = map(acceleration, 75, 185, 0, 63);
    Serial.write(x);
      

    delay(20);
    }
  }

  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;

}

And the output on the serial monitor will look like this when i pass a certain value

"135
#147
&148
)175
<180
!160

stuff like that… I have no idea what that is or how to fix it, will those symbols appear if i do serial.write and read it another program?
Please Help!

Thank you!!

Serial.write(x);

Try:

Serial.print((int) x);

Try this.

void loop()
{
  nunchuckRead();

  int acceleration = getValue(accelX);

  if ((acceleration >= 75) && (acceleration <= 185)){
    byte x = map(acceleration, 75, 185, 0, 63);   
    delay(20);
    }

  Serial.print(acceleration);
  Serial.print('/t');
  Serial.println(x);

  }

x is a byte, I think you'll want to cast it to int.

Why can't I modify my post?

X prints perfectly as a byte, because acceleration can't go below 75 or above 185, so x can't ever go below 0 or above 255.

What I do want to change are my Serial.prints lines to this.

Serial.print(acceleration);
Serial.print("\t");
Serial.println(x);

Why can’t I modify my post?

New forum feature? I’ve raised a bug report.

Possibly they don’t want people amending/deleting posts after they have been answered but this might be a bit harsh.

Thank you guys for all the help! But unfortunately i havent been able to fix it. I having the problem when i do
Serial.println(acceleration);

Its very weird and no one knows what to do... is it noise of some kind?
Where could it be coming from (the weird symbols)?

Anyways thanks!!

Replace your loop() with what I gave + the revised serial.prints and it will work.

I think i figured it out! I deleted the if statement and it started to print normally... maybe there was some interference or something, pretty weird.