SerialSoftware Problem

Hi guys, I am trying to do communication between two Arduino Mega by using SoftwareSerial but It doesn't work. Is there anyone to know that, what is the reason ?

Here is my transmitter code :
#include <SoftwareSerial.h>

SoftwareSerial active(50, 51);
SoftwareSerial passive(52, 53);
SoftwareSerial standby(10, 11);

// Transmitter

int Button1 = 13;
int Button2 = 12;
int Button3 = 7;

void setup() {
// put your setup code here, to run once:

Serial.begin(9600);

active.begin(9600);
passive.begin(9600);
standby.begin(9600);

pinMode(Button1, INPUT_PULLUP); // for read button
pinMode(Button2, INPUT_PULLUP); // for read button
pinMode(Button3, INPUT_PULLUP); // for read button

}

void loop() {
// put your main code here, to run repeatedly:
if (digitalRead(Button1) == 0)
{
active.write('1');
Serial.println("button 1 pressed");
}
if (digitalRead(Button2) == 0)
{
passive.write('2');
Serial.println("button 2 pressed");
}
if (digitalRead(Button3) == 0)
{
standby.write('3');
Serial.println("button 3 pressed");
}
delay(50); // waitting message send
}

And here is my receiver code :

#include <SoftwareSerial.h>

SoftwareSerial active(50, 51);
SoftwareSerial passive(52, 53);
SoftwareSerial standby(10, 11);

//Reciever

int LED1 = 13;
int LED2 = 12;
int LED3 = 7;
char message1;
char message2;
char message3;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);

active.begin(9600);
passive.begin(9600);
standby.begin(9600);

pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);

}

void loop() {
// put your main code here, to run repeatedly:
if (active.available())
{
message1 = active.read();
if (message1 == '1')
{
digitalWrite(LED1, HIGH);
Serial.println("button 1 pressed");
}
}
if (passive.available())
{
message2 = passive.read();
if (message2 == '2')
{
digitalWrite(LED2, HIGH);
Serial.println("button 2 pressed");
}

}
if (standby.available())
{
  message3 = standby.read();
  if (message3 == '3')
  {
    digitalWrite(LED3, HIGH);
    Serial.println("button 3 pressed");
  } }

delay(20);
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
digitalWrite(LED3,LOW);
}

The easier you make it to read and copy your code the more likely it is that you will get help

Please follow the advice given in the link below when posting code , use code tags and post the code here

If you get errors when compiling please copy them from the IDE using the "Copy error messages" button and paste the clipboard here in code tags

So what does it do ?
What should it do ?
How are the 2 Arduino connected ?

Why are you using SoftwareSerial on Megas that have 4 hardware UARTS ?

Firstly, thank you for your answer. In real project I will have use Arduino UNO. Because of this I am using SerialSoftware now. I am trying to transfer data from one Arduino to another Arduino. When I use just 1 SoftwareSerial, I can do that. When I use more than 1, like in the code, it doesn't work. Do you know what is the reason?

See https://www.arduino.cc/en/Reference/SoftwareSerialListen

Read below.
https://www.pjrc.com/teensy/td_libs_AltSoftSerial.html

Okay, thank you for your answer. As far as I can see, only 1 Software Serial can available at the same time. That's why my codes doesn't work. Thank you. I think I have to use hardware Serial.

Only certain pins on the Mega can be used for software serial. See the Software serial reference.

There is absolutely nonsense in use software serial when can use hardware serial. :roll_eyes:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.