So after trying for hours i've finally figured out how to send multiple integers and long variables between Arduinos over a serial connection and have written a fairly simple code for both sender and receiver:
Sender
#include <AltSoftSerial.h>
long val = 88888888;
byte valueinbytes[4];
int val2 = 30888;
byte value2inbytes[2];
int val3 = 333;
byte value3inbytes[2];
AltSoftSerial mySerial;
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
}
void loop() {
//values are split into multiple bytes using bitshift
valueinbytes[0] = val >> 24; //highest byte stored here
valueinbytes[1] = val >> 16;
valueinbytes[2] = val >> 8;
valueinbytes[3] = val; //lowest byte stored here
value2inbytes[0] = val2 >> 8;
value2inbytes[1] = val2;
value3inbytes[0] = val3 >> 8;
value3inbytes[1] = val3;
//0xAB is the header/start byte and 0xCD the footer
//So the receiver knows when the packet starts and ends
//They can be any value but must be defined the same on sender and receiver
byte packet[10] = {0xAB, valueinbytes[0], valueinbytes[1], valueinbytes[2], valueinbytes[3], value2inbytes[0], value2inbytes[1], value3inbytes[0], value3inbytes[1], 0xCD};
mySerial.write(packet, 10);
delay(50);
}
Receiver
#include <AltSoftSerial.h>
AltSoftSerial mySerial;
long val;
int val2;
int val3;
void setup() {
Serial.begin(9600); //must be the same baudrate
mySerial.begin(9600); //must be the same baudrate
}
void loop() {
if (mySerial.available() > 10 ) { //wait for 10 bytes in serial buffer
byte head = mySerial.read(); //read fisrt byte in buffer
if (head != 0xAB) return; //check if this byte is the predefined header/start byte - if not return to loop
byte b0 = mySerial.read(); //read 4 bytes of user data (long)
byte b1 = mySerial.read();
byte b2 = mySerial.read();
byte b3 = mySerial.read();
byte i0 = mySerial.read(); //read 2 bytes of user data (int)
byte i1 = mySerial.read();
byte z0 = mySerial.read(); //read 2 bytes of user data (int)
byte z1 = mySerial.read();
byte foot = mySerial.read(); //read the 6th and last byte of the serial buffer
if (foot != 0xCD) return; //if this byte does not match the predefined footer then something is wrong with this package
val = b0; //put first byte in long variable
val <<= 8; //shift the bits in the variable by 8 bits to make room for next byte
val |= b1; //put second byte in the long variable
val <<= 8; //shift the bits in the variable by 8 bits to make room for next byte
val |= b2; //put third byte in the long variable
val <<= 8; //shift the bits in the variable by 8 bits to make room for next byte
val |= b3; //put fourth and last byte into long variable
val2 = (i0 << 8) + i1;
val3 = (z0 << 8) + z1;
Serial.println(val, DEC);
Serial.println(val2, DEC);
Serial.println(val3, DEC);
}
}
I tested the codes with the AltSoftwareSerial library on two Arduino nanos and confirmed it working. AltSoftSerial only allows Pin 8 as RX and Pin 9 as TX on Arduino nanos.
Setup:
Sender Pin 9 TX --------> Receiver Pin 8 RX
Sender Pin 8 RX --------> Receiver Pin 9 TX
Common Ground and power via USB for both Nanos
I hope this example is helpful for people looking to reliably transfer variables between Arduinos.