Xbee Series 1 problem

Hello everyone!

I've spend the last 2 days figuring out what im doing wrong.. Maybe you guys could help me?

Goal:

Use a potmeter on an arduino mega and send its value wirelessly to a arduino uno and control a servo based on that value.

I've followed Jeremy Blums tutorial on Youtube, but something isnt working right.

Here is my Arduino megas sender code:

// Potmeter arduino 

int potPin = A0;

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

void loop()
{
 
  int val = map(analogRead(potPin), 0, 1023, 0 , 9);
  Serial.println(val);
  
}

That works fine and i can see the values on the serial monitor going from 0 - 9.

Heres the receiver code:

//Servo arduino

#include <Servo.h>

int servoPin = 9;

Servo jeremysServo;

void setup () 
{
  Serial.begin(9600);
  
  jeremysServo.attach(servoPin);
}
void loop()
{
  while(Serial.available() == 0);
  
  int data = Serial.read() - '0';
  
  int pos = map(data, 0, 9, 0, 180);
  pos = constrain(pos, 0, 180);
  
  //Turn servo
  
  jeremysServo.write(pos);
  Serial.print(pos);
  Serial.flush();
}

When im reading this in the serial monitor, the values are comming in so fast the servo doesnt know what to do with itself :fearful:
Can i slow down the reading a bit or am i doing something completely wrong?

I've tested the Xbees with PixelSketch and everything is working fine.

Any help is much appreciated!

Marc