In this tutorial:

Credits: This tutorial is adapted from a tutorial by Marcela Godoy, that was developed for the Interaction Lab course at NYU Shanghai. It has been altered to use the SerialRecord for Arduino and SerialRecord for Processing libraries.

0. Setup

Follow the instructions to install these two libraries:

1. Arduino to Processing

1.1. Arduino to Processing: Arduino Example Code

Include the SerialRecord library. Create an instance of SerialRecord, that is set up to send or receive three values.

#include "SerialRecord.h"

SerialRecord serialRecord(3);

Start the communication using Serial.begin():

void setup() {
  Serial.begin(9600);
}

First, we will create variables for storing the values from each sensor. In the next example we are using three sensors connected to three different analog pins, so we use analogRead(), If we were reading values from a button, we would use digitalRead() instead. It will depend on what type of sensor you are using, so change the code accordingly.

void loop() {
  // to send values to Processing assign the values you want to send
  //this is an example
  int sensor1 = analogRead(A0);
  int sensor2 = analogRead(A1);
  int sensor3 = analogRead(A2);

Next, we need to find a way to send multiple values to the serial port. We will use the SerialRecord.send() function to do this. It will send them as a string of values separated by commas and terminated by a newline. For example 100,200,300\\n. (In Arduino C++ and Java, as well as many other languages, \\n represents the newline character, which has numeric representation 10.)

  serialRecord[0] = sensor1;
  serialRecord[1] = sensor2;
  serialRecord[2] = sensor3;
  serialRecord.send();

Finally, we might need to add a delay, but it will depend on the sensors you are using in your project.

1.2. Arduino To Processing:  Processing Example Code

Begin the sketch by adding statements to import the Processing Serial Library, and the SerialRecord Library.