In this recitation, I attended the serial communications workshop. In this workshop, I learned how to send multiple values from Arduino to Processing. The values were read from 3 different inputs: 2 potentiometers and 1 push button. The potentiometers sent analog values and the push button sent digital values. I learned that the way to send both types of values is the same. Then I learned how to use these values sent from the sensors, send it to Processing, and apply in the sketch. The two potentiometers change the x and y value of an ellipse. Whenever the push button was pressed, a rectangle would show up where the mouse is. This is one of the many different applications of serial communications possible with Processing and I will use what I learned today to help me send analog values from a pulse sensor to Processing for my final project.
Processing code:
// IMA NYU Shanghai
// Interaction Lab
// For receiving multiple values from Arduino to Processing
/*
* Based on the readStringUntil() example by Tom Igoe
* https://processing.org/reference/libraries/serial/Serial_readStringUntil_.html
*/
import processing.serial.*;
String myString = null;
Serial myPort;
int NUM_OF_VALUES = 3; /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/
int[] sensorValues; /** this array stores values from Arduino **/
void setup() {
size(500, 500);
background(0);
setupSerial();
}
void draw() {
updateSerial();
printArray(sensorValues);
background(#ffffff);
fill(#000000);
ellipse(map(sensorValues[0], 0, 1023, 0, width), map(sensorValues[1], 0, 1023, 0, height), 50, 50);
if (sensorValues[2] == 1) {
fill(#ff0000);
rect(mouseX, mouseY, 100, 100);
}
}
void setupSerial() {
printArray(Serial.list());
myPort = new Serial(this, Serial.list()[3], 9600);
// WARNING!
// You will definitely get an error here.
// Change the PORT_INDEX to 0 and try running it again.
// And then, check the list of the ports,
// find the port “/dev/cu.usbmodem—-” or “/dev/tty.usbmodem—-”
// and replace PORT_INDEX above with the index number of the port.
myPort.clear();
// Throw out the first reading,
// in case we started reading in the middle of a string from the sender.
myString = myPort.readStringUntil( 10 ); // 10 = ‘\n’ Linefeed in ASCII
myString = null;
sensorValues = new int[NUM_OF_VALUES];
}
void updateSerial() {
while (myPort.available() > 0) {
myString = myPort.readStringUntil( 10 ); // 10 = ‘\n’ Linefeed in ASCII
if (myString != null) {
String[] serialInArray = split(trim(myString), “,”);
if (serialInArray.length == NUM_OF_VALUES) {
for (int i=0; i<serialInArray.length; i++) {
sensorValues[i] = int(serialInArray[i]);
}
}
}
}
}
Arduino code:
// IMA NYU Shanghai
// Interaction Lab
// For sending multiple values from Arduino to Processing
int buttonPin = 2; // digital pin 2
int buttonState = 0;
int ledPin = 13;
void setup() {
Serial.begin(9600);
pinMode(2, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
int sensor1 = analogRead(A0);
int sensor2 = analogRead(A1);
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
// keep this format
Serial.print(sensor1);
Serial.print(“,”); // put comma between sensor values
Serial.print(sensor2);
Serial.print(“,”); // put comma between sensor values
Serial.print(buttonState); // 1 or 0
Serial.println(); // add linefeed after sending the last sensor value
// too fast communication might cause some latency in Processing
// this delay resolves the issue.
delay(100);
}