Go Down

Topic: Serial.read and Serial.Write Between 2 Arduino Uno (Read 481 times) previous topic - next topic

WiltandFlow

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

sterretje

#1
Dec 08, 2018, 06:34 am Last Edit: Dec 08, 2018, 06:35 am by sterretje
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
Code: [Select]
//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
Code: [Select]
//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");
    }
  }
}
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Deva_Rishi

in this function
Code: [Select]
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.
To 'Correct' you have to be Correct. (and not be condescending..)

GolamMostafa

#3
Dec 08, 2018, 01:16 pm Last Edit: Dec 08, 2018, 01:21 pm by GolamMostafa
@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:
Code: [Select]
//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:
Code: [Select]
//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");
    }
  }
}





Go Up