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); 
        } 
    } 
}
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);
1
  • Thanks. It works!
    – Shyam3089
    May 17 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");
2
  • Thanks a lot. I wasn't aware of that.
    – Shyam3089
    May 17 at 13:49
  • Good catch, @EdgarBonet - I missed that one.
    – JRobert
    May 17 at 13:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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