I am very new to coding with Arduino and I am experimenting with sending multiple ints from an Uno to a nano.
Backstory I'm trying to create a rov with two joysticks and an Uno at the top of a 20M cable and parsing those values onto a nano with a servo and a few escs at the bottom. using two max485 boards
I have been able to send one value of a pot down the link using the code below but I am struggling to understand how to send and receive two or more.
looks like you're sending an integer value as an ascii string (e.g "123"), 3 ascii characters vs a single 8-bit byte, 0x7b.
parseInt() reads a number of ascii decimal digit characters ('0' - '9') and returns a values after a timeout period expires.
the timeout is on way to delimit a message. Another approach is to recognize a line termination chars (e.g. linefeed '\n')
you could format an ascii string with multiple values separated with a space and terminated, read an ascii string up to the line terminated and then translate the values in the string to two integer values using sscanf()
// ---------------------------------------------------------
void
setup (void)
{
Serial.begin (9600);
}
// ---------------------------------------------------------
void
process (
int a,
int b )
{
Serial.print (__func__);
Serial.print (" ");
Serial.print (a);
Serial.print (" ");
Serial.println (b);
}
// ---------------------------------------------------------
void
loop (void)
{
static char s [80];
static int idx = 0;
while (Serial.available ()) {
char c = Serial.read ();
s [idx++] = c;
if ('\n' == c) {
s [idx] = 0;
idx = 0;
int a, b;
sscanf (s, "%d %d", &a, & b);
process (a, b);
}
}
}
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!!