Exercise 1: Make a Processing Etch-A-Sketch
Arduino code:
void setup() {
Serial.begin(9600);
}
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);
// send the values keeping this format
Serial.print(sensor1);
Serial.print(“,”); // put comma between sensor values
Serial.print(sensor2);
//Serial.print(“,”); // put comma between sensor values
// 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);
// end of example sending values
}
Processing code:
import processing.serial.*;
float x, y, px, py;
int NUM_OF_VALUES = 2; /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/
int sensorValues[]; /** this array stores values from Arduino **/
String myString = null;
Serial myPort;
void setup() {
size(500, 500);
background(255);
setupSerial();
if (sensorValues[0]!=0 && sensorValues[1] !=0) {
x=map(sensorValues[0], 0, 1023, 0, width);
y=map(sensorValues[1], 0, 1023, 0, height);
px=x;
py=y;
}
stroke(0, 0, 200);
strokeWeight(3);
line(x, y, px, py);
}
void draw() {
//background(255);
getSerialData();
//printArray(sensorValues);
// use the values like this!
// sensorValues[0]
// add your code
if( px != 0 && py !=0 ){
line(x, y, px, py);
}
px=x;
py=y;
x=map(sensorValues[0], 0, 1023, 0, width);
y=map(sensorValues[1], 0, 1023, 0, height);
fill(200, 0, 0);
//ellipse(x,y,5,5);
stroke(0, 0, 200);
strokeWeight(3);
//
}
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 getSerialData() {
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]);
}
}
}
}
}
At first I encounterd that a line would formed out of no where. Then I solved it by adding a new line of code if (sensorValues[0]!=0 && sensorValues[1] !=0) into the setup. And if( px != 0 && py !=0 ){
line(x, y, px, py); This makes it only draw when the sensor values are not zero.
Exercise 2:
Work with a partner to write a Processing sketch in which a ball bounces from left to right on the screen. (You can use the fullScreen() function, instead of size(), to make the Processing window occupy the full dimensions of your screen.) Send values from Processing to Arduino based on the position of the bouncing ball. These values should somehow give an indication of when the ball hits the edge of the screen. Then, make a circuit with your Arduino and 2 servo motors. The corresponding Arduino code should read the serial values from Processing and translate them into movements, through the appropriate servo motor, as if they are hitting the ball back and forth. See the video below for reference.
After using tape
Exercise 2:
Arduino:
// 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. **/ #define NUM_OF_VALUES_FROM_PROCESSING 3 /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/ #include <Servo.h> /** DO NOT REMOVE THESE **/ int tempValue = 0; int valueIndex = 0; int angle = 0; /* This is the array of values storing the data from Processing. */ int processing_values[NUM_OF_VALUES_FROM_PROCESSING]; Servo L,R; void setup() { Serial.begin(9600); L.attach(8); R.attach(7); pinMode(13, OUTPUT); L.write(0); R.write(0); } void loop() { getSerialData(); // add your code here using elements in the values array //this is an example connecting a buzzer to pin 8 if (processing_values[0] == 1) { moveServo(L); digitalWrite(13, HIGH); } else if (processing_values[1] == 1) { moveServo(R); digitalWrite(13, HIGH); }else{ digitalWrite(13, LOW); } } void moveServo(Servo servo_test){ servo_test.write(90); delay(1); servo_test.write(-90); } //receive serial data from Processing void getSerialData() { while (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 ',': processing_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 processing_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; } } }
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. **/ import processing.serial.*; int NUM_OF_VALUES_FROM_PROCESSING = 3; /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/ int processing_values[] = new int[NUM_OF_VALUES_FROM_PROCESSING]; /** this array stores values you might want to send to Arduino **/ Serial myPort; String myString; float mov=20; float horipos; float rad=100; void setup() { fullScreen(); background(0); setupSerial(); horipos=width/2; } void draw() { background(0); horipos+=mov; if(horipos<=0){ mov=-mov; processing_values[0] = 1; } if(horipos>rad &&horipos<width-rad){ processing_values[0] = 0; processing_values[1] = 0; } if(horipos>=width){ mov=-mov; processing_values[1] = 1; } circle(horipos,height/2,rad); sendSerialData(); } void setupSerial() { printArray(Serial.list()); myPort = new Serial(this, Serial.list()[0], 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; } void sendSerialData() { String data = ""; for (int i=0; i<processing_values.length; i++) { data += processing_values[i]; //if i is less than the index number of the last element in the values array if (i < processing_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 linefeed "\n" } } //write to Arduino myPort.write(data); print(data); // this prints to the console the values going to arduino }
Homework:
Arduino:
// IMA NYU Shanghai // Interaction Lab // For sending multiple values from Arduino to Processing int button1 = 5; int button2 = 6; int buttonState1 = LOW; int previousState1 = LOW; int buttonState2 = LOW; int previousState2 = LOW; //button state : on and off low--off high--on int counter1 = 0; int counter2 = 0; // how many times the button been pressed //int state1, state2;; long time = 0; // the last time the output pin was toggled long debounce = 200; // the debounce time, increase if the output flickers void setup() { Serial.begin(9600); pinMode(button1, INPUT); pinMode(button2, INPUT); } void loop() { // to send values to Processing assign the values you want to send //this is an example int buttonState1 = digitalRead(button1);// digital read here for on and off, analogread is for potentiameter int buttonState2 = digitalRead(button2); // int sensor3 = analogRead(A2); //how many times button1 pressed if (buttonState1 != previousState1 && millis() - time > debounce) { if (buttonState1 == HIGH) { counter1++; time = millis(); } } previousState1 = buttonState1; //how many times button2 pressed if (buttonState2 != previousState2 && millis() - time > debounce) { if (buttonState2 == HIGH) { counter2++; time = millis(); } } previousState2 = buttonState2; // send the values keeping this format Serial.print(counter1); Serial.print(","); // put comma between sensor values Serial.print(counter2); //Serial.print(","); // put comma between sensor values //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); // end of example sending values }
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.*; int NUM_OF_VALUES_FROM_ARDUINO = 2; /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/ int buttonValues[]; /** this array stores values from Arduino **/ String myString = null; Serial myPort; void setup() { size(500, 500); background(0); setupSerial(); } void draw() { getSerialData(); println(buttonValues); if ((buttonValues[0]%2 == 1)&&(buttonValues[1]%2 == 0)) { background(0); // star(width*0.3, height*0.3, 30, 70, 5); pushMatrix(); translate( width*0.3, height*0.3); rotate(frameCount / 200.0); star(0, 0, 30, 70, 5); popMatrix(); } if ((buttonValues[0]%2 == 0)&&(buttonValues[1]%2 == 1)) { background(0); //star(width*0.7, height*0.7, 80, 100, 40); pushMatrix(); translate( width*0.7, height*0.7); rotate(frameCount / 200.0); star(0, 0, 80, 100, 40); popMatrix(); } if ((buttonValues[0]%2 == 1) &&(buttonValues[1]%2 == 1)) { background(0); //star(width*0.7, height*0.7, 80, 100, 40); pushMatrix(); translate( width*0.7, height*0.7); rotate(frameCount / 200.0); star(0, 0, 80, 100, 40); popMatrix(); //star(width*0.3, height*0.3, 30, 70, 5); pushMatrix(); translate( width*0.3, height*0.3); rotate(frameCount / 200.0); star(0, 0, 30, 70, 5); popMatrix(); } if ((buttonValues[0]%2 == 0) &&(buttonValues[1]%2 == 0)) { background(0); } //pushMatrix(); //translate( width*0.3, height*0.3); //rotate(frameCount / 200.0); //star(0, 0, 30, 70, 5); //popMatrix(); } void setupSerial() { printArray(Serial.list()); myPort = new Serial(this, Serial.list()[ 1 ], 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; buttonValues = new int[NUM_OF_VALUES_FROM_ARDUINO]; } void getSerialData() { 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_FROM_ARDUINO) { for (int i=0; i<serialInArray.length; i++) { buttonValues[i] = int(serialInArray[i]); } } } } } void star(float x, float y, float radius1, float radius2, int npoints) { float angle = TWO_PI / npoints; float halfAngle = angle/2.0; beginShape(); for (float a = 0; a < TWO_PI; a += angle) { float sx = x + cos(a) * radius2; float sy = y + sin(a) * radius2; vertex(sx, sy); sx = x + cos(a+halfAngle) * radius1; sy = y + sin(a+halfAngle) * radius1; vertex(sx, sy); } endShape(CLOSE); }
Homework:
Arduino:
// IMA NYU Shanghai // Interaction Lab // For sending multiple values from Arduino to Processing int button1 = 5; int button2 = 6; int buttonState1 = LOW; int previousState1 = LOW; int buttonState2 = LOW; int previousState2 = LOW; //button state : on and off low--off high--on int counter1 = 0; int counter2 = 0; // how many times the button been pressed //int state1, state2;; long time = 0; // the last time the output pin was toggled long debounce = 200; // the debounce time, increase if the output flickers void setup() { Serial.begin(9600); pinMode(button1, INPUT); pinMode(button2, INPUT); } void loop() { // to send values to Processing assign the values you want to send //this is an example int buttonState1 = digitalRead(button1);// digital read here for on and off, analogread is for potentiameter int buttonState2 = digitalRead(button2); // int sensor3 = analogRead(A2); //how many times button1 pressed if (buttonState1 != previousState1 && millis() - time > debounce) { if (buttonState1 == HIGH) { counter1++; time = millis(); } } previousState1 = buttonState1; //how many times button2 pressed if (buttonState2 != previousState2 && millis() - time > debounce) { if (buttonState2 == HIGH) { counter2++; time = millis(); } } previousState2 = buttonState2; // send the values keeping this format Serial.print(counter1); Serial.print(","); // put comma between sensor values Serial.print(counter2); //Serial.print(","); // put comma between sensor values //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); // end of example sending values }
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.*; int NUM_OF_VALUES_FROM_ARDUINO = 2; /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/ int buttonValues[]; /** this array stores values from Arduino **/ String myString = null; Serial myPort; void setup() { size(500, 500); background(0); setupSerial(); } void draw() { getSerialData(); println(buttonValues); if ((buttonValues[0]%2 == 1)&&(buttonValues[1]%2 == 0)) { background(0); // star(width*0.3, height*0.3, 30, 70, 5); pushMatrix(); translate( width*0.3, height*0.3); rotate(frameCount / 200.0); star(0, 0, 30, 70, 5); popMatrix(); } if ((buttonValues[0]%2 == 0)&&(buttonValues[1]%2 == 1)) { background(0); //star(width*0.7, height*0.7, 80, 100, 40); pushMatrix(); translate( width*0.7, height*0.7); rotate(frameCount / 200.0); star(0, 0, 80, 100, 40); popMatrix(); } if ((buttonValues[0]%2 == 1) &&(buttonValues[1]%2 == 1)) { background(0); //star(width*0.7, height*0.7, 80, 100, 40); pushMatrix(); translate( width*0.7, height*0.7); rotate(frameCount / 200.0); star(0, 0, 80, 100, 40); popMatrix(); //star(width*0.3, height*0.3, 30, 70, 5); pushMatrix(); translate( width*0.3, height*0.3); rotate(frameCount / 200.0); star(0, 0, 30, 70, 5); popMatrix(); } if ((buttonValues[0]%2 == 0) &&(buttonValues[1]%2 == 0)) { background(0); } //pushMatrix(); //translate( width*0.3, height*0.3); //rotate(frameCount / 200.0); //star(0, 0, 30, 70, 5); //popMatrix(); } void setupSerial() { printArray(Serial.list()); myPort = new Serial(this, Serial.list()[ 1 ], 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; buttonValues = new int[NUM_OF_VALUES_FROM_ARDUINO]; } void getSerialData() { 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_FROM_ARDUINO) { for (int i=0; i<serialInArray.length; i++) { buttonValues[i] = int(serialInArray[i]); } } } } } void star(float x, float y, float radius1, float radius2, int npoints) { float angle = TWO_PI / npoints; float halfAngle = angle/2.0; beginShape(); for (float a = 0; a < TWO_PI; a += angle) { float sx = x + cos(a) * radius2; float sy = y + sin(a) * radius2; vertex(sx, sy); sx = x + cos(a+halfAngle) * radius1; sy = y + sin(a+halfAngle) * radius1; vertex(sx, sy); } endShape(CLOSE); }