Go Down

Topic: simple serial communication using pro mini and xbee s1 (Read 2132 times) previous topic - next topic

gibbysoo

Jan 03, 2013, 04:34 am Last Edit: Jan 03, 2013, 02:06 pm by gibbysoo Reason: 1
hi, i am using 2 arduino pro mini 3.3v, 8mhz and 2 xbee series 1. now i am able to transmit something but not the things i want. the receiver cant read the transmitting side. for example, the receiver keep read the else part "both led are on" rather than the IF part. it is reading only X but not H and L. the baud rate for both xbee is 19200 which is same as the pro mini. i do not know whats wrong with it =(

transmitter side
Code: [Select]
void setup() {
Serial.begin(19200);
}
void loop() {
Serial.print('L');
delay(2000);
Serial.print('H');
delay(2000);
Serial.print('X');
delay(2000);
}



receiver side
Code: [Select]
int LEDredPin =10;
int LEDgreenPin = 11;

void setup() {
Serial.begin(19200);
pinMode(LEDredPin, OUTPUT);
pinMode(LEDgreenPin, OUTPUT);
}
void loop() {
while(Serial.available() == 0);
char dataByte = Serial.read();
Serial.write(byte(dataByte));
if(dataByte == 'H'){
digitalWrite(LEDredPin, HIGH);
digitalWrite(LEDgreenPin, LOW);
Serial.println("Red LED on");
}
else if(dataByte == 'L') {
digitalWrite(LEDgreenPin, HIGH);
digitalWrite(LEDredPin, LOW);
Serial.println("Green LED on");
}
else{
digitalWrite(LEDredPin, HIGH);
digitalWrite(LEDgreenPin, HIGH);
Serial.println("Both LED on");
}
Serial.flush();
}

PaulS

Serial.write() is for binary data. Do you see any binary data for the sender to send? If not, why are you reading ASCII data and writing binary data to the serial port? Why are you using the same serial port to talk to the XBee and the PC? You MUST stop doing that.

Oh, and GET RID OF THE FLUSH() CALL!

nickgammon

Read this before posting a programming question


Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the # button above the posting area.

Not quote tags.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

gibbysoo

@ nick gammon

noted.

@PaulS

then is there any sample/simple program code i can use for wireless push button with an led for pro mini? =(

PaulS

Quote
then is there any sample/simple program code i can use for wireless push button with an led for pro mini?

Plenty. You'll learn more writing your own, though.

You need to read about SoftwareSerial, and use that to communicate with the XBee, so that you can continue to use Serial to communicate with the PC.

You are trying to send ASCII data. So, forget that the write() method is in the Serial and SoftwareSerial classes. Use the print() methods, instead.

Go Up