Arduino library to transfer arrays of data from Processing.
This is a simple one file Arduino library to allow accurate transmission of data. Serial
read
and write
are useful enough to transfer just one variable. More than that and the Arduino may loose packets of data or mix values up. Consider the following situation:
Processing reads data from joystick controller and sends two values every 20ms (leftMotorPower, rightMotorPower)
. The Arduino will receive both values, but how will it differentiate between the two? Setting the right motor power to leftMotorPower
would be disastrous in most cases. To solve this problem, many have used letter tags to help the Arduino tell which value is for which variable. This library provides an easy to use object that will keep track of multiple values of data in a single array.
-
Include the RoboLib library:
#include <RoboLib.h>
-
Create the RoboLib object:
RoboLib myBot(dataSizeConst, totalchar);
dataSizeConst
is the number of variables you want to transfertotalchar
is the number of characters being transmitted at one time
-
In
void Setup()
, initialize RoboLib:myRobot.begin(baudRate);
-
In
void serialEvent()
, save the data:myBot.saveData();
-
In
void loop()
, get the data and save it to a local variable:processedData = myBot.getData();
Send all your data at once semicolon delimited:
string = var1 + ";" + var2 + ";" + var3 + ";" + var4 + ";" + var5 + ";\n";
port.write(string);
Note: There can be more than five variables. Example of sending data.
A full example of RoboLib at work.
- Arduino reads all the data at once until a newline.
- Using string split and
toCharArray
C commands, each variable is isolated and casted to a double. - Methods used to get processed values.
- Arduino reads all the data at once until a newline is reached.