Serial.read and Serial.Write Between 2 Arduino Uno

Good Day.

I'd like to know what is wrong with this code. I'd like the first arduino to get datas from the sensor and then send it to the next arduino via Serial TxRx pins, and then Make arduino 2 Receive first the data from the first arduino and then send the confirmation back to the first arduino to make the necessary action.

I'm planning to do this using 4 Arduino uno as Slaves and 1 arduino Mega as master for it has 4 Serial TxRx Pins and simultaneously get datas from the 4 arduinos. But For now I'd like to know what's wrong with this 1:1 setup first. Thanks

Slave.ino (657 Bytes)

Master.ino (447 Bytes)

First read How to use this forum - please read., specifically point 7 about posting code. Both codes are small enough; you're now missing out on people that use cell phones and can't open ino files.

I've done it this time for you (see below).

Next give a proper description of what it actually does and how that differs from what you expect.

OPs codes
Master

//Arduino Mega
//Multiple Serial

char OpenOne = '1';
char Close = '0';

char mystr;


void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
}

void loop() 
{
  while(Serial1.available() > 0)
  {
    mystr = Serial1.read();
  }

  if(mystr == '1')
  {
    Serial.println("Open Motor");
    Serial1.write(OpenOne);
  }
  if(mystr == '0')
  {
    Serial.println("Close Motor");
    Serial1.write(Close);
  }
}

Slave

//Arduino Uno

char mystr;
char mystrOn = '1';
char mystrOff = '0';
char DetectSensor;

const int SensorIn = 5;

void setup()
{
  Serial.begin(9600);

  pinMode(SensorIn, INPUT);
}

void loop()
{
  DetectSensor = digitalRead(SensorIn);

  if (DetectSensor == HIGH)
  {
    Serial.write(mystrOn);
  }
  if (DetectSensor == LOW)
  {
    Serial.write(mystrOff);
  }

  receive();
}

void receive()
{
  while (Serial.available() > 0)
  {
    mystr = Serial.read();

    if (mystr == '1')
    {
      Serial.println("Open Valve");
    }
    if (mystr == '0')
    {
      Serial.println("Close Valve");
    }
  }
}

in this function

void receive()
{
  while (Serial.available() > 0)
  {
    mystr = Serial.read();

    if (mystr == '1')
    {
      Serial.println("Open Valve");
    }
    if (mystr == '0')
    {
      Serial.println("Close Valve");
    }
  }
}

you are reading the 'echo' back from 'Serial' and then writing a message to the Serial port, which seems meant for human eyes, do you still have the USB port plugged into the UNO-slave ?
Since you are using only a single char to be sent over, for now it is not so important, but to send a byte at BAUD 9600 takes about 1200 microseconds, your 'while(Serial.available())' statements may as well be 'if'. I suggest as an easy solution for now you let the UNO send back 'K", let the Mega also test for that, and let it print out a to the Serial-port a confirmation.

@OP (Original Poster)

(1) For testing purposes and to debug your codes, you may proceed with the following hardware setup. Leave the hardware UART Port of the UNO-slave be connected with the Serial Monitor/IDE; use software UART Port (SUART) to communicate with MEGA-Master.

(2) In your original codes, everything was alright except that your slave was not receiving the echo from the Master. See the changes that I have made in your slave codes. Now, MEGA and UNO exchanging data very well.

MEGA-Master Codes:

//Arduino Mega
//Multiple Serial

char OpenOne = '1';
char Close = '0';

char mystr;


void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
}

void loop()
{
  while (Serial1.available() > 0)
  {
    mystr = Serial1.read();
  }

  if (mystr == '1')
  {
    Serial.println("Open Motor");
    digitalWrite(13, HIGH);
    Serial1.write(OpenOne);
  }
  if (mystr == '0')
  {
    Serial.println("Close Motor");
    Serial1.write(Close);
  }
}

UNO-Slave Codes:

//Arduino Uno
#include<SoftwareSerial.h>
SoftwareSerial mySUART(2, 3); //SRX, STX

char mystr;
char mystrOn = '1';
char mystrOff = '0';
char DetectSensor;

const int SensorIn = 5;

void setup()
{
  Serial.begin(9600);
  mySUART.begin(9600);
  pinMode(SensorIn, INPUT);
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  Serial.println("Slave");
}

void loop()
{
  DetectSensor = digitalRead(SensorIn);

  if (DetectSensor == HIGH)
  {
    mySUART.write(mystrOn);//)Serial.write(mystrOn);
  }
  if (DetectSensor == LOW)
  {
    mySUART.write(mystrOff);//Serial.write(mystrOff);
  }

  // receive();
  //}

  //void receive()
  //{
  if (mySUART.available() > 0); //((Serial.available() > 0)
  {
    mystr = mySUART.read();//Serial.read();

    if (mystr == '1')
    {
      Serial.println("Open Valve");
      digitalWrite(13, HIGH);
    }
    if (mystr == '0')
    {
      Serial.println("Close Valve");
    }
  }
}