0

I'm trying to send data from Arduino Mega to UNO. The master code sends characters 'x' and 'a' every half second to the slave Arduino UNO. Slave receives the characters and switches the state of the onboard LED. Here I'm trying to print some text in the beginning of the loop and the characters received. Instead of the letters it is printing numbers as given below. Why this line Serial.println('Soft serial avaialble') prints numbers? How to print real text received from the master?

> 27749
> 97 
> 27749 
> 120 
> 27749

Master code:

// Include the Software Serial library 
#include <SoftwareSerial.h>
// Define a Software Serial object and the used pins 
SoftwareSerial softSerial(8, 9); // RX, TX 
void setup() {
 Serial.begin(9600); 
 softSerial.begin(9600);
} 
void loop() { 
 // Sends characters 'x' and 'a' every half second.
 if (Serial.available()) { 
 softSerial.write('x');
 delay(500);
 digitalWrite(LED, LOW); 
 delay(500);
 } 
}

Slave code:

// Include the Software Serial library 
#include <SoftwareSerial.h>
// Define a Software Serial object and the used pins 
SoftwareSerial softSerial(8, 9);
// LED Pin 
int LED = 13; 
void setup() { 
 softSerial.begin(9600); 
 Serial.begin(9600); 
 pinMode(LED, OUTPUT);
 
} 
void loop() { 
 Serial.println('Soft serial avaialble');
 if (softSerial.available()) { 
 int com = softSerial.read(); 
 if (com == 'x') { 
 Serial.println(com);
 digitalWrite(LED, LOW); 
 } else if (com == 'a'){ 
 Serial.println(com);
 digitalWrite(LED, HIGH); 
 } 
 } 
}
asked May 17, 2021 at 12:43

2 Answers 2

1

In your slave, you've declared com as an int, so Serial.println() interprets it that way. If you want to keep it as int except for printing, cast Serial.println()'s argument to char:

Serial.println((char)com);
answered May 17, 2021 at 12:49
1
  • Thanks. It works! Commented May 17, 2021 at 13:48
0

In addition to JRobert's remark, note that strings in C++ are delimited by double quotes, thus:

Serial.println("Soft serial avaialble");
answered May 17, 2021 at 13:36
2
  • Thanks a lot. I wasn't aware of that. Commented May 17, 2021 at 13:49
  • Good catch, @EdgarBonet - I missed that one. Commented May 17, 2021 at 13:52

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.