Serial Communication Between 3 X Arduino Uno

Hi,

I'm not a pro programmer just hobbyist from time to time.

Project includes: 3 x Arduino Uno

They are called:
DaisyChain Master (Lets Call it DC Master)
DaisyChain Middle (DC Middle)
DaisyChain End (DC End)

So the idea is that on all arduinos im using the TX and RX built in and I am also using software serial

step 1 - Idea is that DC Master sends a start data like a 0 using software serial tx and that is received by DC Middle on the Default RX

step 2 - Once DC Middle receives this start data it then sends back some data (default TX) which is fixed for now, currently sending a number 9

step 3 - DC Middle then sends DC End the same start data like a 0 using software serial tx and that is received by DC End on the Default RX

step 4 - DC End now receives this start data it then sends back some data (default TX) which is fixed for now, currently sending a number 62

step5 - Dc Middle receives this data and relays it back to DC Master

step6 - DC Master displays on LCD or Serial monitor screen

Hope this makes sense.

I have a few questions...

1, Am i using the correct monitoring of of serial by using serial.avaible() because i also need to do other things later and cant be stuck waiting for communication to come in.

2, Why does my code send back 9,62,62 its sending back the 62 two times??? why???

EXTRA NOTES:
These Arduino's will be about 100m apart on a farm fence. And there could be up too 10 of them each 100m apart totaling of 1km distance all in all.

<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>

CODE: DaisyChain Master

//----------------------------------------------x-------------------------------------
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);

const int ledPin = 13; // 13 for standard arduino

int start, incoming, counter = 0;
int statusStore[100] ;
//----------------------------------------------x-------------------------------------
void setup() {
  // initialize the LCD
  lcd.begin();

  // Turn on the blacklight and print a message.
  lcd.backlight();

  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) 
  {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  
  Serial.println("Coms 1 - Ready for Action!");
  lcd.print("Coms 1 - Ready for Action!");
  delay(5000);
  
  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
 
  // make the pins outputs:
  pinMode(ledPin, OUTPUT);
  digitalWrite( ledPin, LOW );
  delay(500);
  lcd.clear();

  mySerial.print(start); //SEND start CODE TO  DaisyChain Middle !!!// - //"start = 0"//
  Serial.print("start");  ///just for debug 
  Serial.print(",");///just for debug 
  // this code above will be moved to void loop, just here to send 1 test run
}
//----------------------------------------------x-------------------------------------
void loop()
{
    if (mySerial.available()>0)
    {  //waits  until data recieves
      counter = counter+1; // counter starts at 0, increrment straight away so we start with #1
      incoming = mySerial.read(); //read return data from DaisyChain_Middle
      statusStore[counter] = incoming;
      Serial.print(statusStore[counter]);
      lcd.print(statusStore[counter]);  
      lcd.print(",");
      Serial.print(",");
     }
  
}
//----------------------------------------------x-------------------------------------

<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>

CODE: DaisyChain Middle

//----------------------------------------------x-------------------------------------
#include <Wire.h> 
#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX

const int ledPin = 13; // 13 for standard arduino

int IntTheNumber, incoming = 0;
int start = 0;
//----------------------------------------------x-------------------------------------

void setup() {
 
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
 
  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
  
  // make the pins outputs:
  pinMode(ledPin, OUTPUT);
  digitalWrite( ledPin, LOW );
}

//----------------------------------------------x-------------------------------------

void loop()
{
  if (Serial.available() > 0 )
  { 
    //----x--------x--------x----
    digitalWrite( ledPin, LOW );
    // LED LED LED LED LED delay start
    //----x--------x--------x----

    start = Serial.read();
    //send start signal to next one Variable start is = 0
    
    //send data back to home base
    Serial.write(9);

    delay(10);  

    mySerial.print(start);     

    //----x--------x--------x----
    // LED LED LED LED LED delay end
    digitalWrite( ledPin, HIGH );
    //----x--------x--------x----
  }
  if (mySerial.available() == 1 )
  {
    incoming = mySerial.read();
    delay(50);
    Serial.write(incoming);
  }
}
//----------------------------------------------x-------------------------------------

<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>

CODE: DaisyChain End

//----------------------------------------------x-------------------------------------
#include <Wire.h> 
#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX

const int ledPin = 13; // 13 for standard arduino

int IntTheNumber = 0;
int start = 0;
//----------------------------------------------x-------------------------------------

void setup() {
 
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
 
  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
  
  // make the pins outputs:
  pinMode(ledPin, OUTPUT);
  digitalWrite( ledPin, LOW );
}

//----------------------------------------------x-------------------------------------

void loop()
{
  if (Serial.available() == 1 )
  { 
    //----x--------x--------x----
    digitalWrite( ledPin, LOW );
    // LED LED LED LED LED delay start
    //----x--------x--------x----

    start = Serial.read();
    
    //----x----

    //
    //send data back to home base
    delay(50);
    Serial.write(62);
    
    //----x--------x--------x----
    // LED LED LED LED LED delay end
    digitalWrite( ledPin, HIGH );
    //----x--------x--------x----
  }
  
}
//----------------------------------------------x-------------------------------------

Welcome to the forum, would you please mind to use code tags (the </> button above the editing field) to format your code? It is unreadable as it is and you will probably not get any suggestions otherwise.

1 Like

Thank you for the welcome.... Hope i have done it right now?

I would think having three different sets of code would quickly become very cumbersome, and unnecessary. I would have a single piece of code, as follows:

  1. Each device has it's own numeric ID, 0-n.
  2. Each message contains a sequence of bytes or characters:
    Destination device ID
    Source device ID
    Some number of "payload" bytes/chars
    Message End marker
  3. Each device reads the first character of message:
    If DestID == DeviceID, read the rest of the message, and process it, without forwarding to other devices
    else simply forward message to next device, without processing it

This lets you handle any number of devices with the exact same code. You can also implement a special device ID that indicates ALL devices should process the message.

1 Like

why not use an rs-485 interface that supports multiple end points but requires a master slave protocol? rs-485 is also good for 400ft

2 Likes

Hello
If do you have free line of sight you may check the application of a wire less solution.
There are a lot of tutorial available.
Check out this to get an idea.

1 Like

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