Using the data transferred via serial communication

Hey guys, is it possible to use the data that is being transferred from one Arduino board to the other? I've got a load of measurments from SR-04 modules on one board and transferring them to another. I want to deterimine the functions on the secondary board with the measurments that are coming through.

Any previous topics or help is much appreciated,
Thanks

Why not do both measuring and calculating in the same controller? Communication is quite special, often difficult to make reliable.

I recommend that you read the serial input basics tutorial.

Railroader:
Why not do both measuring and calculating in the same controller? Communication is quite special, often difficult to make reliable.

The script running and outputting 6 SR-04 measurments and commands (to the Blynk app) isn't quick enough to keep up with the ability to control the model car with my controller. I've now got a Mega running the sensor script and an Uno running the conroller script. I'd like to tell the car to stop when certain sensors measure determined distances that are being transferred from the Mega to the Uno via one of the serial ports

Yeah, this has been a pretty common method in the embedded systems world for doing what you are trying to accomplish. Use one piece of low-cost hardware to collect the data and another one for making decisions and driving outputs. However, you might consider a more integrated approach by going to a Due, an ESP8266/32, an STM32, or some other 32 bit variant, most likely an ARM of some kind. There are lots of Arduino-compatible 32 bit SBCs out there for cheap. You get a more potent hardware platform and the benefits of putting everything under one roof.

You can use SerialTransfer.h to automatically packetize and parse your data for inter-Arduino communication without the headace. The library is installable through the Arduino IDE and includes many examples.

Here are the library's features:

This library:

  • can be downloaded via the Arduino IDE's Libraries Manager (search "SerialTransfer.h")
  • works with "software-serial" libraries
  • is non blocking
  • uses packet delimiters
  • uses consistent overhead byte stuffing
  • uses CRC-8 (Polynomial 0x9B with lookup table)
  • allows the use of dynamically sized packets (packets can have payload lengths anywhere from 1 to 254 bytes)
  • can transfer bytes, ints, floats, and even structs!!

Example TX Arduino Sketch:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  char buff[] = "hi";

  myTransfer.txObj(buff, sizeof(buff));
  myTransfer.sendData(sizeof(buff));
  delay(100);
}

Example RX Arduino Sketch:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  if(myTransfer.available())
  {
    char buff[40];
    
    myTransfer.rxObj(buff, sizeof(buff));
    
    Serial.println("New Data: ");
    Serial.write(buff, sizeof(buff));
    Serial.println();
  }
  else if(myTransfer.status < 0)
  {
    Serial.print("ERROR: ");

    if(myTransfer.status == -1)
      Serial.println(F("CRC_ERROR"));
    else if(myTransfer.status == -2)
      Serial.println(F("PAYLOAD_ERROR"));
    else if(myTransfer.status == -3)
      Serial.println(F("STOP_BYTE_ERROR"));
  }
}

For theory behind robust serial communication, check out the tutorials Serial Input Basics and Serial Input Advanced.

There are lots of ways to do it. The format I usually follow is master/slave. Master sends over some sort of command and waits for the slave to reply. Slaves don't talk unless they are commanded to. This keeps everything in sync. You could use an off the shelf library, or just roll your own. If you roll your own, you will have to come up with some sort of message format that they both can agree on.

I wrote my own. It was kind'a fun to write. I use it all the time for talking between Arduino kinds of things. Very handy!

-jim lee

Yeah, I ended up using mostly HDLC with a JSON payload package. My HDLC parser ignores all the cool sub-commands of real HDLC, but those aren't needed in a modern async serial comms mechanism anyway.

Anybody interested ask away and I'll shoot you some docs and source.