NEED HELP!! Arduino Uno Serial Write Problem!!

Hi,

I'm on a Project with two Arduino Uno and two XBee Sields (Series 2)!
The Communication between the two Arduinows via Xbee seems to be perfect (i can send and receive Data) but the Quality is very bad..

This is my Code to send Values from a Slide-Poti with Baudrate 9600

potValue = analogRead(A5);
Serial.write("<1:");
Serial.write(potValue);
Serial.write(">");
  
Serial.write("<2:");
Serial.write(potValue);
Serial.write(">");
Serial.println(potValue);

The Output on the Serial-Monitor looks like this

<1:Ð><2:Ð>208
<1:><2:>270
<1:g><2:g>359

So as i can see, when i print the Value of the Poti on the Serial-Monitor it looks fine
if i try to write it over the Serial Interface only crap will be transmitted...

this is what i receive with the other Arduino (not the same value as above):

1:Ý
2:Ý477
1:Ü
2:Ü476
1:Ý
2:Ý477
1:Þ
2:Þ478
1:ß
2:ß479
1:³
2:³179

How can i fix that Problem? I hope someone can help me!

thanks
cualalumpur

here is my complete Code
Transmit

#define MAX_MILLIS_TO_WAIT 2000

unsigned long bias;
unsigned long stopwatch;
int led_green = 13;
int led_yellow = 11;
int led_red = 12;
int potValue = 0;
int potValue_safe = 0;


void setup()
{
  pinMode(led_green, OUTPUT);
  pinMode(led_yellow, OUTPUT);
  pinMode(led_red, OUTPUT);
  potValue = analogRead(A5);
  potValue_safe = potValue;
  Serial.begin(9600);
}

void loop()
{
  stopwatch = newMillis();
  delay(300);
  potValue = analogRead(A5);
  if(potValue == potValue_safe || potValue <= 20)
  {
    if(stopwatch >= MAX_MILLIS_TO_WAIT)
    {
      Serial.write('L');
      digitalWrite(led_green, HIGH);
      delay(100);
      digitalWrite(led_green, LOW);
    }
  }
  else
  {
    resetMillis();
    digitalWrite(led_yellow, HIGH);
    delay(100);
    potValue_safe = potValue;
    //Serial.write(potValue/256);
    //Serial.write(potValue%256);
    //byte get1 = potValue/256;
    //byte get2 = potValue%256;
    //int value = (int((int)get1) * 256 + get2);
    Serial.write("<1:");
    Serial.write(potValue);
    Serial.write(">");
    
    Serial.write("<2:");
    Serial.write(potValue);
    Serial.write(">");
    
    Serial.println(potValue);
    digitalWrite(led_yellow, LOW);
  } 
}

int newMillis() 
{
  return (millis() - bias);
}

void resetMillis() 
{
  bias = millis();
}

Receive:

#include <LiquidCrystal.h>
#include <Servo.h>
#define MAX_MILLIS_TO_WAIT 2500
#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;
bool convert = false;
int convCount = 0;
char inData[80];
char convData[10];
byte index;

unsigned long stopwatch;
unsigned long bias;
int ledPin_fail = 13;
int ledPin_ok = 12;
int ledPin_wait = 11;
int servoValue = 0;

Servo servo1, servo2;

void setup()
{
  servo1.attach(10);
  servo2.attach(9);
  pinMode(ledPin_fail, OUTPUT);
  pinMode(ledPin_ok, OUTPUT);
  pinMode(ledPin_wait, OUTPUT);
  resetMillis();
  Serial.begin(9600);
}

void loop()
{
  stopwatch = newMillis();
  if(stopwatch >= MAX_MILLIS_TO_WAIT)
  {
    digitalWrite(ledPin_wait, LOW);
    digitalWrite(ledPin_fail, HIGH);
    digitalWrite(ledPin_ok, LOW);
  }
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       digitalWrite(ledPin_wait, LOW);
       digitalWrite(ledPin_fail, LOW);
       digitalWrite(ledPin_ok, HIGH);
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
       resetMillis();
    }
    else if(inChar == EOP)
    {
       for(int i = 0; i <10; i++)
       {
         if(inData[i] == '\0')
          break;
         else
         {
           if(convert)
           {
             convData[convCount] = inData[i];
             convCount++;
           }
           if(inData[i] == ':')
           {
             convert = true;
             convCount = 0;
           }
         }
       }
       int n = atoi(convData);
       servo1.write(n);
       servo2.write(n);
       //Serial.println(n);
       ended = true;
       resetMillis();
       break;
    }
    else if(inChar == 'L')
    {
      Serial.println("L");
      resetMillis();
      digitalWrite(ledPin_wait, HIGH);
      digitalWrite(ledPin_fail, LOW);
      digitalWrite(ledPin_ok, LOW);
    }
    else
    {
      if(index < 79)
      {
        Serial.print(inChar);
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
      resetMillis();
    }
  }

  // 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';
    convert = false;
  }
}

int newMillis() 
{
  return (millis() - bias);
}

void resetMillis() 
{
  bias = millis();
}

use Serial.print instead of Serial.write

Serial.print(potValue, DEC); // for forcing decimal notation

Serial.write() does only one byte at a time

robtillaart:
use Serial.print instead of Serial.write

Serial.print(potValue, DEC); // for forcing decimal notation

Serial.write() does only one byte at a time

perfect, now the Servo moves! Thanks

But the Servo stucks time to time, when i send an value of 60 he moves but sometimes it stops and goes backwards ...

is there a better way to move a Servo?

this is how i do it actually
(my slide-poti only goes from 0 to 500)

int n = atoi(convData);
n = map(n, 0, 500, 0, 220);
Serial.println(n);
servo1.write(n);
servo2.write(n);

You are looking for the start and end of packet markers, and dealing with them correctly. The code to deal with the data that arrives between them is misplaced. It belongs in the if(started && ended) block.

You are sending 1:nnn or 2:nnn, and yet you are moving both servos to position nnn. Why? You are sending the same data twice, too. Why?