Serial Communication: Can't Latch the readings in an order

Hello folks,

I’m a newbie to the micro-controller stuffs and now sucked with a problem on serial communication.
My idea is to connect 7 arduinos (6 slaves and a master) in series using RS485 and take some analog and digital readings from every individuals to the master arduino which will be connected to a GUI via Ethernet.

Sending a slave ID from the master and latch the inputs from the called slave is my idea of communication. I’ve attached the Proteus Schematics which is similar to my project where I get from another site. The only difference there is, I’m using a Mega as a master and Serial1 for the communication.

To test my setup, as a first step, I tried with a single slave using normal serial communication. Please refer the code. I’m just sending integers of 10, 20, 30, 40, 50, 60 considering them as my readings to the master when it is called.

Slave program code:

#define MasterID 0
#define myID 1

#define byte_1_recieved 100
#define byte_2_recieved 101
#define byte_3_recieved 102
#define byte_4_recieved 103
#define byte_5_recieved 104
#define byte_6_recieved 105

int called_ID;


void setup() {

  Serial.begin(9600);
  while(!Serial){};

}

void loop() {


  called_ID = Serial.read();
  delay(10);
  

  if(called_ID = myID){
    Serial.write(10);
    Serial.flush();
    
    Serial.write(20);
    Serial.flush();

    Serial.write(30);
    Serial.flush();

    Serial.write(40);
    Serial.flush();

    Serial.write(50);
    Serial.flush();

    Serial.write(60);
    Serial.flush();

    called_ID = 0;
  }

Master Program code:

#define MasterID 0

#define slave_1 1
#define slave_2 2
#define slave_3 3
#define slave_4 4
#define slave_5 5

#define byte_1_recieved 100
#define byte_2_recieved 101
#define byte_3_recieved 102
#define byte_4_recieved 103
#define byte_5_recieved 104
#define byte_6_recieved 105



void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
  while(!Serial1);

}

void loop() {
  
  Read_slave_1();
  delay(100);
  
}

The Read_save_1() method is:

int slave_1_read[6];

int val[5];

void Read_slave_1(){
  int i = 0;
  while(!Serial1.available()){
    
  }
  Serial1.write(slave_1);
  //Serial1.flush();

  while (i<=5){
  if (Serial1.available()) {

    slave_1_read[i]= Serial1.read();
    delay(0.5);
    i++;
  } 
  }
  
  Serial.println("The values are: " + Serial(slave_1_read[0]) + "|" +Serial(slave_1_read[1]) + "|" +Serial(slave_1_read[2]) + "|" +Serial(slave_1_read[3]) + "|" +Serial(slave_1_read[4]) + "|" +Serial(slave_1_read[5]));
  
  Serial.println("**************************************************************");

  for(int i = 0; i<6; i++){
    slave_1_read[i] = 0;
  }
    
  }

The snap of serial monitor output is attached with this post for reference. Pls check that also.

The problem is, I can’t get them in a fixed order for further manipulations.

First time it’ll be 10, 20, 30, 40, 50 and few seconds after, it’ll be like 30, 40, 50, 60, 10, 20 and this constantly changes.

Please help me to fix this. Your support is highly appreciated. May be my code is totally wrong on this. Kindly, correct my code and reply me if so.

Thank You very much

Proteus.zip (29.2 KB)

Proteus.zip (29.2 KB)

  if(called_ID = myID){

= is the assignment operator. == is the equality operator.

Your slave code is fatally flawed, so the rest of the communication process is undefined.

You should NOT be reading serial data unless you KNOW that there is data to read.

Yep, start over. Read Serial Input Basics.

Cheers, /dev

Thanks @PaulS, I change that and checked, still having the same issue...

And, @/dev, thank you so much for the link, I'll try that way and let you guys know :)

Again, Thank you for both..

Thank you @/dev… Finally, I found the answer :slight_smile: That link really helped me.

And thanks for every one.
Have a nice day.

Cheers (Y)