Hi Guys,
I want to read analog senor datas and send them wirelessly over XBee to my computer.
My Setup:
Transmitter:
Arduino Nano (powerd by battery), which reads two Sensors (later I want to expand to 6 Sensors), and send them via XBee series 1, baudrate: 57600. The Transmitter Code:
void setup() {
Serial.begin(57600);
}
void loop() {
int sensor0 = analogRead(A0);
int sensor1 = analogRead(A1);
Serial.print("<");
Serial.print(sensor0);
Serial.print("/");
Serial.print(sensor1);
Serial.print(">");
delay(10);
}
Receiver:
Arduino Mega, which reads Serial1 (connected to XBee) and print the values to serial monitor. I'm using a code I found in a post from Paul S in this forum. But the code was only made to receive data from one sensor. So I extended it for two sensors. The Code:
void setup() {
Serial.begin(57600);
Serial1.begin(57600);
}
char inData[2][5];
int zeile;
int spalte;
boolean started = false;
boolean ended = false;
void loop()
{
while(Serial1.available() > 0)
{
char aChar = Serial1.read();
if(aChar == '<')
{
started = true;
zeile = 0;
spalte = 0;
inData[spalte][zeile] = '\0';
}
else if(aChar == '>')
{
ended = true;
}
else if(started)
{
if(aChar != '/')
{
inData[spalte][zeile] = aChar;
zeile++;
inData[spalte][zeile] = '\0';
}
if(aChar == '/')
{
zeile = 0;
spalte++;
inData[spalte][zeile] = '\0';
}
}
}
if(started && ended)
{
// Convert the string to an integer
char spalte0[4] = {inData[0][0], inData[0][1], inData[0][2], inData[0][3]};
unsigned int sensor0 = atoi(spalte0);
char spalte1[4] = {inData[1][0], inData[1][1], inData[1][2], inData[1][3]};
unsigned int sensor1 = atoi(spalte1);
// Use the value
Serial.print("Sensor0 = ");
Serial.println(sensor0);
Serial.print("Sensor1 = ");
Serial.println(sensor1);
// Get ready for the next time
started = false;
ended = false;
zeile = 0;
spalte = 0;
inData[spalte][zeile] = '\0';
}
}
My Problem:
The code works fine, if the incomming value is not higher than 999. If it is 1000 to 1023, the first sensor value is displayed correctly, the second one is much too high...it seems as there is a kind of overflow...
This is a copy from the serial monitor:
Sensor0 = 874
Sensor1 = 879 --> perfect
Sensor0 = 1012
Sensor1 = 7396 --> wrong
Any Ideas? Is there a logical mistake in the receiving code?