Final project essay by Steve Sun

project title: Collab Drawer

purpose: we want to highlight human connection, but this time not through making contact but through collaborating and communicating with each other. in this project, through collaborating with the teammates, the users as a team can draw a complete picture using this drawer. however the point is not drawing the perfect picture, but is the communication and collaboration that is happening in the users a s a team.

plan: in our final layout of the project, there will be several balls (nur depend on the number of the users) connected with lines. each player is in charge of the rotation of one ball. The circle on the end of the whole thing is a pen, where another player can decide if it draws lines or to go back to the former step. through conrtoling the rotation of each balls and collabratively positioning the pen, the users will be able to draw pictures with this tool. we need to have several controllers and a button to control the draw function. 

we plan to have at least one rotation good to use before user testing, and immediately finish the rotation of the other controllers after that. then while Kris is diong the final wiring, I will begin the fabrication

significance: This project encourages communication and promotes team collaboration. we didn’t build this idea upon anyone else’s project, but this idea of interaction that “it should not be ‘input output that’s it’ kind of thing but the output actually promotes the user to do more with the project” was generated from the article The Art of Interactive Design by Crawford. 

Recitation 8 by Hangkai Qian

Exercise 1: Make a Processing Etch A Sketch

Step 1: draw with ellipse

I don’t have any difficulty in step 1, I just copy the code by IMA Lab to import data from Arduino.  And I found if I draw it with ellipse, we have to move extremely slow to ensure the graph is a line but no a bunch of circles. 

Step2: draw with lines

I was a little confused at the first time because pmouseX and pmouseY cannot be applied to the position of the variables from Arduino. However,after asking the fellow, I figured it out. What I need is just make a variable to store the previous number from Arduino. The code is attached:

fill(0);
line(a, b, sensorValues[0], sensorValues[1]);
a=sensorValues[0];
b=sensorValues[1];

The whole code is under:

Processing :

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 **/
int a;
int b;

void setup() {
size(500, 500);
background(255);
setupSerial();
}

void draw() {
updateSerial();
printArray(sensorValues);
fill(0);
line(a, b, sensorValues[0], sensorValues[1]);
a=sensorValues[0];
b=sensorValues[1];
}

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]);
}
}
}
}
}

Arduino:

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.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);
}

schematic :

Exercise 2: Make a musical instrument with Arduino

I have no difficulty in building this, but I really don’t know how to make different sounds through the code…   Sadly, because of the time limit, I couldn’t figure it out, but I will ask fellows when I do my final project. The interaction in this project is simple: when you click the mouse, the buzzer make sound.

Here are my codes:

Arduino:

define NUM_OF_VALUES 2 /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/

/** DO NOT REMOVE THESE **/
int tempValue = 0;
int valueIndex = 0;

/* This is the array of values storing the data from Processing. */
int values[NUM_OF_VALUES];

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

void loop() {
getSerialData();

// add your code here
// use elements in the values array
// values[0] // values[1]

tone(11,values[0]);
noTone(11);

}

//recieve serial data from Processing
void getSerialData() {
if (Serial.available()) {
char c = Serial.read();
//switch – case checks the value of the variable in the switch function
//in this case, the char c, then runs one of the cases that fit the value of the variable
//for more information, visit the reference page: https://www.arduino.cc/en/Reference/SwitchCase
switch (c) {
//if the char c from Processing is a number between 0 and 9
case ‘0’…’9′:
//save the value of char c to tempValue
//but simultaneously rearrange the existing values saved in tempValue
//for the digits received through char c to remain coherent
//if this does not make sense and would like to know more, send an email to me!
tempValue = tempValue * 10 + c – ‘0’;
break;
//if the char c from Processing is a comma
//indicating that the following values of char c is for the next element in the values array
case ‘,’:
values[valueIndex] = tempValue;
//reset tempValue value
tempValue = 0;
//increment valuesIndex by 1
valueIndex++;
break;
//if the char c from Processing is character ‘n’
//which signals that it is the end of data
case ‘n’:
//save the tempValue
//this will b the last element in the values array
values[valueIndex] = tempValue;
//reset tempValue and valueIndex values
//to clear out the values array for the next round of readings from Processing
tempValue = 0;
valueIndex = 0;
break;
//if the char c from Processing is character ‘e’
//it is signalling for the Arduino to send Processing the elements saved in the values array
//this case is triggered and processed by the echoSerialData function in the Processing sketch
case ‘e’: // to echo
for (int i = 0; i < NUM_OF_VALUES; i++) {
Serial.print(values[i]);
if (i < NUM_OF_VALUES – 1) {
Serial.print(‘,’);
}
else {
Serial.println();
}
}
break;
}
}
}

schemes:exercise 2

Processing:

import processing.serial.*;

int NUM_OF_VALUES = 1; /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/

Serial myPort;
String myString;

// This is the array of values you might want to send to Arduino.
int values[] = new int[NUM_OF_VALUES];

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

printArray(Serial.list());
myPort = new Serial(this, Serial.list()[ 4 ], 9600);
// check the list of the ports,
// find the port “/dev/cu.usbmodem—-” or “/dev/tty.usbmodem—-”
// and replace PORT_INDEX above with the index 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;
}

void draw() {
background(0);

value[0]=mouseX
}

// sends the values to Arduino.
sendSerialData();

// This causess the communication to become slow and unstable.
// You might want to comment this out when everything is ready.
// The parameter 200 is the frequency of echoing.
// The higher this number, the slower the program will be
// but the higher this number, the more stable it will be.
echoSerialData(200);
}

void sendSerialData() {
String data = “”;
for (int i=0; i<values.length; i++) {
data += values[i];
//if i is less than the index number of the last element in the values array
if (i < values.length-1) {
data += “,”; // add splitter character “,” between each values element
}
//if it is the last element in the values array
else {
data += “n”; // add the end of data character “n”
}
}
//write to Arduino
myPort.write(data);
}

void echoSerialData(int frequency) {
//write character ‘e’ at the given frequency
//to request Arduino to send back the values array
if (frameCount % frequency == 0) myPort.write(‘e’);

String incomingBytes = “”;
while (myPort.available() > 0) {
//add on all the characters received from the Arduino to the incomingBytes string
incomingBytes += char(myPort.read());
}
//print what Arduino sent back to Processing
print( incomingBytes );
}

Final Project – Essay – Audrey Samuel

Title: Pirate Chase! – A Tragedy of the Commons

Project Statement of Purpose: My partner and I decided to expand upon our midterm project and focus more on the psychological/experimental aspect of it. Our updated project: Pirate Chase! is a boat chasing game between four people. The participants will be positioned on four different corners of the circle (which will serve as the ocean) and have to blow their boats to the island in the middle. There will be a few obstacles in between which will serve to push the boat beneath the water or drive it in the opposite direction. Through previous research, we would like to use the ‘Tragedy of the Commons’ scenario to see if individuals will blow their opponents boats away from each other or collaborate to go onto the island together and share the treasure.

Project Plan: We would like to use our project as a psychological experiment to test the ‘Tragedy of the Commons’ economic concept with students as our target. We will first begin by building our ‘ocean’ by purchasing a miniature kiddie pool (61cm wide 12cm high) and filling it with blue water. We will then 3D print four boats, making sure they are all in different colors in order to clearly distinguish between the four players. Next we will build our island and the obstacles we plan on using. We hope to build a bridge across the circular pool in order to keep the electronic objects (ie. arduino and sensors) away from the water. This bridge will be made from clear, see-through material for now (material used for laminating). We will then fix the island in the middle of this bridge therefore placing the island right in the middle of the circular pool. We will attach an infra-red sensor to this which will point downwards and will detect when the winning boat floats under the island through an LED light which we will position on top of the island. The island will be designed by us and 3D printed. We will also attach a buzzer to emit sound when the winner wins. Through serial communication, the infra-red sensor will transmit from Arduino to Processing and thereby project a graphic on a laptop screen displaying who won and marking the end of the game. We will also time this and monitor how students drive their opponents away. The first boat to reach the island directly without being thrown off by the obstacles is the winner. Students can only move their boats forwards not backwards or sideways. If students miss their first try to reach the island due to an obstacle they cannot move their boats backwards and so will have to rotate around the circle, thereby forcing them to move physically move to where their opponents are standing. 

In order to empathize with our target group we will make sure the circle is big enough and students have enough space to walk around the circle. In addition to this, since it is an experiment we will try this with groups of friends, a mix of students and faculty and a number of mixed combinations to see how the outcome might change. By the end of this week we hope to build our circuits and ensure that our sensor is working. We also hope to finish designing the graphics for Processing and connecting Arduino to Processing through serial communication on Thursday. On Friday we hope to 3D print our boats and island. Next week, we will begin building the bridge and putting the components together. We hope to be done with the main components before Thanksgiving and leave the week before the Final to designing and improving the start and end sounds for the game etc.

Context and Significance: Most of the research I did focused on educational projects that could be used either to convey information to children or older students. After taking two Psychology courses as well as Economics courses here at NYU Shanghai, my Partner and I thought of how we could merge these together in a fun and interactive way while still contributing to the field of Psychology and Economics. We then stumbled across the “Tragedy of the Commons” concept first introduced by American ecologist Garrett Hardin which looks at how people will react in a situation where there exists limited resources in a specific area. This concept further aims to understand the individualistic and collective cultures of people (ie. two psychological concepts). Depending on an individual’s culture or country, some may care only for themselves whereas others may have to consider their family or others before themselves. Through this we hope to focus on how students interact with each other in our game/experiment and whether they will adopt an individualistic approach or collectivist approach in getting the treasure on the island. We are re-creating our midterm project and improving upon its design as well as concept. This could be helpful for students as they re-evaluate their roles in their day to day lives and what approach they choose. After successful completion, we could contribute to the first hand data we gather to a Social Science Psychology Research Journal. Our population/demographic will be unique because our school has a mixture of people from a huge number of countries each with their own unique culture. We could take this further and display various scenarios besides the “Pirate capturing treasure on an island” and look at how countries may fight over a specific resource in the future after climate change completely limits/reduces the availability of that specific resource. 

Most importantly, we wanted our project to be as interactive as possible. Referring back to my definition of interaction, I stated that I had originally seen interaction as a form of communication, however I added to this definition stating that it is not only a form of communication, but also a way of blending technology and human abilities together in the most natural way without undervaluing the capabilities of humans, to therefore fulfill a greater aim. After reading “A Brief Rant on the Future of Interaction Design” by Bret Victor, I began to think about the ways in which technology of today is fully set on only fulfilling the needs of humans. Bret Victor on his blog encourages us to think outside the box and go one step ahead in terms of how technology can be revolutionary. He encourages us to not restrict interaction to the use of a single finger on a touch screen which is why we decided to incorporate “blowing” and physical movement in our project. This therefore allows individuals to truly feel as though they are not being undervalued, with the computer doing all the work, but rather sets an equal balance between man and machine.

Recitation 8 Serial Communication by Barry Wang

Recitation 8 Serial Communication

In this week, I learded the serial communication between Arduino and Processing. During the recitation, it is required to create a Processing Etch which sends data from Arduino to Processing  and a musical instrument which sends data from Processing to Arduino.

1. Processing Etch

Step 1. Controlling an Ellipse

Step 2. The Drawing Machine

The interaction here is to control the “pen” using two knobs which control x and y coordinate seperately. It’s an interesting but challenging process since it really difficult to create an exquisite picture.

Code on 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(255);
  setupSerial();
}


void draw() {
  float x_prev = sensorValues[0];
  float y_prev = sensorValues[1];
  updateSerial();
  printArray(sensorValues);
  float x = sensorValues[0];
  float y = sensorValues[1];
  strokeWeight(5);
  line(map(x_prev,0,1023,0,500),map(y_prev,0,1023,0,500),map(x,0,1023,0,500),map(y,0,1023,0,500));


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

  // add your code

  //
}



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

Code on 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);
// int sensor3 = analogRead(A2);

// 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(10);
}

The test video:

2. Musical Instrument

In this little project, the interaction is to map the position of mouse into the pitch and duration of the tone made by the buzzer. It’s interesting to hear how the note changes linearly according to the movement of the mouse. This simple idea can be furtherly developed into some music game, especially on a touch screen, where we can easily detect the position of the finger by the position of mouse.

Code on Processing:

// IMA NYU Shanghai
// Interaction Lab


/**
 * This example is to send multiple values from Processing to Arduino.
 * You can find the arduino example file in the same folder which works with this Processing file.
 * Please note that the echoSerialData function asks Arduino to send the data saved in the values array
 * to check if it is receiving the correct bytes.
 **/


import processing.serial.*;

int NUM_OF_VALUES = 2;  /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/


Serial myPort;
String myString;
boolean state = true;

// This is the array of values you might want to send to Arduino.
int values[] = new int[NUM_OF_VALUES];

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

  printArray(Serial.list());
  myPort = new Serial(this, "COM10", 9600);
  // check the list of the ports,
  // find the port "/dev/cu.usbmodem----" or "/dev/tty.usbmodem----" 
  // and replace PORT_INDEX above with the index 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;
}


void draw() {
  background(0);

  // changes the values
  //for (int i=0; i<values.length; i++) {
  //  values[i] = i;  /** Feel free to change this!! **/
  values[0] = mouseX;
  values[1] = mouseY;
  //}
  if (state){
  // sends the values to Arduino.
  sendSerialData();

  // This causess the communication to become slow and unstable.
  // You might want to comment this out when everything is ready.
  // The parameter 200 is the frequency of echoing. 
  // The higher this number, the slower the program will be
  // but the higher this number, the more stable it will be.
  echoSerialData(200);}
  else{
  myPort.write("pn");
  }
}

void sendSerialData() {
  String data = "";
  for (int i=0; i<values.length; i++) {
    data += values[i];
    //if i is less than the index number of the last element in the values array
    if (i < values.length-1) {
      data += ","; // add splitter character "," between each values element
    } 
    //if it is the last element in the values array
    else {
      data += "n"; // add the end of data character "n"
    }
  }
  //write to Arduino
  myPort.write(data);
}


void echoSerialData(int frequency) {
  //write character 'e' at the given frequency
  //to request Arduino to send back the values array
  if (frameCount % frequency == 0) myPort.write('e');

  String incomingBytes = "";
  while (myPort.available() > 0) {
    //add on all the characters received from the Arduino to the incomingBytes string
    incomingBytes += char(myPort.read());
  }
  //print what Arduino sent back to Processing
  print( incomingBytes );
}
void keyPressed(){
  if (key == ' '){
    if(state){
    state = false;}
    else{
    state = true;
    }
  }
}

Here, I added a simple improvement, which is to pause when pressing SPACEBAR.

Code on Arduino:

[code] // IMA NYU Shanghai
// Interaction Lab

/**
This example is to send multiple values from Processing to Arduino.
You can find the Processing example file in the same folder which works with this Arduino file.
Please note that the echo case (when char c is ‘e’ in the getSerialData function below)
checks if Arduino is receiving the correct bytes from the Processing sketch
by sending the values array back to the Processing sketch.
**/

#define NUM_OF_VALUES 2 /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/

/** DO NOT REMOVE THESE **/
int tempValue = 0;
int valueIndex = 0;
/* This is the array of values storing the data from Processing. */
int values[NUM_OF_VALUES];

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

void loop() {
getSerialData();
tone(8,map(values[0],0,500,0,1000),values[1]);

// add your code here
// use elements in the values array
// values[0] // values[1] }

//recieve serial data from Processing
void getSerialData() {
if (Serial.available()) {
char c = Serial.read();
//switch – case checks the value of the variable in the switch function
//in this case, the char c, then runs one of the cases that fit the value of the variable
//for more information, visit the reference page: https://www.arduino.cc/en/Reference/SwitchCase
switch (c) {
case ‘p’:
values[0] = 0;
break;
//if the char c from Processing is a number between 0 and 9
case ‘0’…’9′:
//save the value of char c to tempValue
//but simultaneously rearrange the existing values saved in tempValue
//for the digits received through char c to remain coherent
//if this does not make sense and would like to know more, send an email to me!
tempValue = tempValue * 10 + c – ‘0’;
break;
//if the char c from Processing is a comma
//indicating that the following values of char c is for the next element in the values array
case ‘,’:
values[valueIndex] = tempValue;
//reset tempValue value
tempValue = 0;
//increment valuesIndex by 1
valueIndex++;
break;
//if the char c from Processing is character ‘n’
//which signals that it is the end of data
case ‘n’:
//save the tempValue
//this will b the last element in the values array
values[valueIndex] = tempValue;
//reset tempValue and valueIndex values
//to clear out the values array for the next round of readings from Processing
tempValue = 0;
valueIndex = 0;
break;
//if the char c from Processing is character ‘e’
//it is signalling for the Arduino to send Processing the elements saved in the values array
//this case is triggered and processed by the echoSerialData function in the Processing sketch
case ‘e’: // to echo
for (int i = 0; i < NUM_OF_VALUES; i++) {
Serial.print(values[i]);
if (i < NUM_OF_VALUES – 1) {
Serial.print(‘,’);
}
else {
Serial.println();
}
}
break;
}
}
}
[/code]

Recitation 8: Serial Communication

Introduction

The purpose of this recitation was to use serial communication in order to communicate between processing and arduino with more than one variable.

Etch-a-sketch

We were tasked with creating an etch-a-sketch using values taken from potentiometers on arduino then translating it to a line being drawn on processing. I first started by creating a code on processing in which I could draw a line using my mouse, and clear the line by pressing the enter key.

  stroke(100);
  strokeWeight(4);
  line(mouseX,mouseY,pmouseX,pmouseY);
  if (keyPressed) {
    if (key == ENTER) {
      background(200);

Afterwards, I created a circuit on arduino with two potentiometers, and created code in order to print the values of both the potentiometers.

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

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

  Serial.print(sensor1); 
  Serial.print(",");
  Serial.print(sensor2);
  Serial.println();

  delay(100);
}

After creating the two separate codes, I used the serial communication template for communication between arduino to processing. I had to tweak the values on the line() function so that instead of reading the x and y values from the mouse in order to draw the line, the values from the potentiometers on arduino were used instead. 

// 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 **/
int previousSensorValues1;
int previousSensorValues2;


void setup() {
  size(1023, 1023);
  background(200);
  setupSerial();
}


void draw() {
  updateSerial();
  printArray(sensorValues);

  stroke(100);
  strokeWeight(4);
  line(previousSensorValues1,previousSensorValues2,sensorValues[0],sensorValues[1]);
  previousSensorValues1 = sensorValues[0];
  previousSensorValues2 = sensorValues[1];
  
   if (keyPressed) {
    if (key == ENTER) {
      background(200);
    }
   }
}

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

Musical Instrument

The coding for the second task was a little more straightforward. I started with the arduino coding and creating the circuit. Using the serial communication template, I created the code so that if the value of an array sent from processing was 1, then a tone would play from the pitches.h file, and if the the value was 0, then nothing would play. From processing, if the mouse were clicked, then a value of 1 would be sent.

// IMA NYU Shanghai
// Interaction Lab


/**
  This example is to send multiple values from Processing to Arduino.
  You can find the Processing example file in the same folder which works with this Arduino file.
  Please note that the echo case (when char c is 'e' in the getSerialData function below)
  checks if Arduino is receiving the correct bytes from the Processing sketch
  by sending the values array back to the Processing sketch.
 **/

#define NUM_OF_VALUES 2    /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/
#include "pitches.h"

/** DO NOT REMOVE THESE **/
int tempValue = 0;
int valueIndex = 0;

/* This is the array of values storing the data from Processing. */
int values[NUM_OF_VALUES];


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

void loop() {
  getSerialData();

  // add your code here
  // use elements in the values array
  // values[0]
  // values[1]
if (values[0] == 1) {
  tone(10, values[1]);
}
else {
  noTone(10);
}

}


//recieve serial data from Processing
void getSerialData() {
  if (Serial.available()) {
    char c = Serial.read();
    //switch - case checks the value of the variable in the switch function
    //in this case, the char c, then runs one of the cases that fit the value of the variable
    //for more information, visit the reference page: https://www.arduino.cc/en/Reference/SwitchCase
    switch (c) {
      //if the char c from Processing is a number between 0 and 9
      case '0'...'9':
        //save the value of char c to tempValue
        //but simultaneously rearrange the existing values saved in tempValue
        //for the digits received through char c to remain coherent
        //if this does not make sense and would like to know more, send an email to me!
        tempValue = tempValue * 10 + c - '0';
        break;
      //if the char c from Processing is a comma
      //indicating that the following values of char c is for the next element in the values array
      case ',':
        values[valueIndex] = tempValue;
        //reset tempValue value
        tempValue = 0;
        //increment valuesIndex by 1
        valueIndex++;
        break;
      //if the char c from Processing is character 'n'
      //which signals that it is the end of data
      case 'n':
        //save the tempValue
        //this will b the last element in the values array
        values[valueIndex] = tempValue;
        //reset tempValue and valueIndex values
        //to clear out the values array for the next round of readings from Processing
        tempValue = 0;
        valueIndex = 0;
        break;
      //if the char c from Processing is character 'e'
      //it is signalling for the Arduino to send Processing the elements saved in the values array
      //this case is triggered and processed by the echoSerialData function in the Processing sketch
      case 'e': // to echo
        for (int i = 0; i < NUM_OF_VALUES; i++) {
          Serial.print(values[i]);
          if (i < NUM_OF_VALUES - 1) {
            Serial.print(',');
          }
          else {
            Serial.println();
          }
        }
        break;
    }
  }
}

I then went to make the processing code. I made it so that if the mouse was pressed, the x value of the mouse would be recorded and sent to arduino in order to play a pitch corresponding to that x value.

// IMA NYU Shanghai
// Interaction Lab


/**
 * This example is to send multiple values from Processing to Arduino.
 * You can find the arduino example file in the same folder which works with this Processing file.
 * Please note that the echoSerialData function asks Arduino to send the data saved in the values array
 * to check if it is receiving the correct bytes.
 **/


import processing.serial.*;

int NUM_OF_VALUES = 2;  /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/


Serial myPort;
String myString;

// This is the array of values you might want to send to Arduino.
int values[] = new int[NUM_OF_VALUES];

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

  printArray(Serial.list());
  myPort = new Serial(this, Serial.list()[7], 9600);
  // check the list of the ports,
  // find the port "/dev/cu.usbmodem----" or "/dev/tty.usbmodem----" 
  // and replace PORT_INDEX above with the index 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;
}


void draw() {
  background(0);

if (mousePressed) {
  values[0] = 1;
}
else {
  values[0] = 0;
  }
  
  values [1] = mouseX;


  // sends the values to Arduino.
  sendSerialData();

  // This causess the communication to become slow and unstable.
  // You might want to comment this out when everything is ready.
  // The parameter 200 is the frequency of echoing. 
  // The higher this number, the slower the program will be
  // but the higher this number, the more stable it will be.
  echoSerialData(200);
}

void sendSerialData() {
  String data = "";
  for (int i=0; i<values.length; i++) {
    data += values[i];
    //if i is less than the index number of the last element in the values array
    if (i < values.length-1) {
      data += ","; // add splitter character "," between each values element
    } 
    //if it is the last element in the values array
    else {
      data += "n"; // add the end of data character "n"
    }
  }
  //write to Arduino
  myPort.write(data);
}


void echoSerialData(int frequency) {
  //write character 'e' at the given frequency
  //to request Arduino to send back the values array
  if (frameCount % frequency == 0) myPort.write('e');

  String incomingBytes = "";
  while (myPort.available() > 0) {
    //add on all the characters received from the Arduino to the incomingBytes string
    incomingBytes += char(myPort.read());
  }
  //print what Arduino sent back to Processing
  print( incomingBytes );
}