Recitation 7: Serial Communication by Linhui

Lab Date: April 19, 2019
Instructor: Marcela

Exercise 1

At first, when I opened the multiple value serial-communication to the arduino, I found it didn’t work and I asked the Tristan for help. We  checked and tried together and we found a very small mistake, but this mistake stops the whole process. I should use serial. println(); instead of serial.println(‘,’). Otherwise, it will never transform the data into processing because it is still on the loop. It is an important point that I ignored in class. 

[code] Arduino

// IMA NYU Shanghai
// Interaction Lab
// For sending multiple values from Arduino to Processing

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

void loop() {
int sensor1 = analogRead(A0);
int sensor2 = analogRead(A1);

// keep this format
Serial.print(sensor1);
Serial.print(“,”); // put comma between sensor values
Serial.print(sensor2);
//Serial.print(“,”);
//Serial.print(sensor3);
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);
}
[/code]

//Processing

// 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 = 2; /** 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();
float posX=map(sensorValues[0],0,1023,0,500);
float posY=map(sensorValues[1],0,1023,0,500);
noStroke();
fill(255);
ellipse(posX,posY,30,30);
print(posX);
print(“,”);
print(posY);
println();

//printArray(sensorValues);

// use the values like this!
// sensorValues[0]

// add your code

//
}

void setupSerial() {
printArray(Serial.list());
myPort = new Serial(this, Serial.list()[ 4 ], 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]);
}
}
}
}
}

Exercise 2

// IMA NYU Shanghai
// Interaction Lab
// This code receives one value from Processing to Arduino

char valueFromProcessing;
int Pin = 7;

void setup() {
Serial.begin(9600);
pinMode(Pin, OUTPUT);
}

void loop() {
// to receive a value from Processing
while (Serial.available()) {
valueFromProcessing = Serial.read();
}

if (valueFromProcessing == ‘L’) {
noTone(8);
tone(7,350,200);
delay(10);
} if (valueFromProcessing == ‘M’) {
noTone(6);
tone(7,400,500);
delay(10);
} if (valueFromProcessing == ‘N’) {
noTone(7);
tone(7,600,300);
delay(10);
}

// too fast communication might cause some latency in Processing
// this delay resolves the issue.
delay(10);
}

// IMA NYU Shanghai
// Interaction Lab
// This code receives one value from Arduino to Processing

import processing.serial.*;

Serial myPort;
int valueFromArduino;

void setup() {
size(500, 500);
background(0);

printArray(Serial.list());
// this prints out the list of all available serial ports on your computer.

myPort = new Serial(this, Serial.list()[ 4 ], 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.
}

void draw() {
if (pmouseX<width/4){
myPort.write(‘L’);
} if (pmouseX<width/5 && pmouseX > width/5){
myPort.write(‘M’);
} if (pmouseX>width/4){
myPort.write(‘N’);
}
// to read the value from the Arduino
//while ( myPort.available() > 0) {
// valueFromArduino = myPort.read();
//}
// println(valueFromArduino);//This prints out the values from Arduino
}

Leave a Reply