Sign up ×
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

I have a setup where two XBEES modules are talking to each other, both using XBEE shields, one attached to an UNO ARDUINO and the other a MEGA ARDUINO. One XBEE sends the data and the other receives it and switch on or off a LED. The problem is the receiver one don't work. This is the sender code

int led = 13;
const int bouton = 2; String inputString;
void setup() {
pinMode(led, OUTPUT);
Serial1.begin(9600);
Serial.begin(9600);
digitalWrite(led, LOW);
}
void loop() {
while (Serial.available() ) {
// get the new byte:
delay(3);  
char inChar = Serial.read();
// add it to the inputString:
inputString += inChar;
}
if (inputString.length() >0) {
Serial.println(inputString);
Serial1.println(inputString);
inputString=""; 
}
} 

This the receiver one

int led = 13;
String inputString;
void setup() {
// put your setup code here, to run once:
pinMode(led, OUTPUT);
Serial.begin(9600);
}
void loop() {
while (Serial.available() ) {
// get the new byte:
delay(3);  
char inChar = Serial.read();
// add it to the inputString:
inputString += inChar;   
}

if (inputString.length() >0) {
Serial.println(inputString);
if (inputString == "on"){
digitalWrite(led,HIGH);
Serial.println("LED ON");
}

if (inputString == "off"){
digitalWrite(led,LOW);
   Serial.println("LED OFF");
}
inputString=""; 
}
}

I get on the serial monitor of ARDUINO UNO what i wrote in the serial monitor of ARDUINO MEGA but the LED don't switch on or off :/

share|improve this question

migrated from electronics.stackexchange.com May 11 at 21:07

This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts.

    
This question is purely on Arduino, and should be (/have been) posted on Arduino.stackexchange.com. It has nothing to do with "electronics design". And the only relevancy is due to it being a microcontroller, but Arduino actually has it's own StackExchange page. – FuaZe May 11 at 13:07

1 Answer 1

up vote 0 down vote accepted

Probably you are getting a new character/carrier return (\n or \n\r) in your receivers' string, that is why it won't match exactly "on" or "off", it will be "on\n", "on\r\n" or similar.

share|improve this answer
    
"on\r\n" this is make it works thank you – sawi May 11 at 20:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.