Exercise 1: Make a Processing Etch-A-Sketch
#1 Arduino Code // IMA NYU Shanghai // Interaction Lab // For sending multiple values from Arduino to Processing 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 }
#1 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.*; int NUM_OF_VALUES_FROM_ARDUINO = 3; /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/ int sensorValues[]; /** this array stores values from Arduino **/ String myString = null; Serial myPort; float x = 0; float y = 0; void setup() { size(1000, 800); background(92, 167, 186); setupSerial(); stroke(255, 66, 93); strokeWeight(6); } void draw() { getSerialData(); printArray(sensorValues); float posx = map(sensorValues[0], 0, 1023, 0, width); float posy = map(sensorValues[1], 0, 1023, 0, width); line(posx, posy, x, y); x = posx; y = posy; // use the values like this! // sensorValues[0] // add your code // } void setupSerial() { printArray(Serial.list()); myPort = new Serial(this, Serial.list()[ 2 ], 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_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++) { sensorValues[i] = int(serialInArray[i]); } } } } }
Exercise 2:
#2 Arduino Code
Servo servo1; Servo servo2; void setup() { Serial.begin(9600); servo1.attach(9); servo2.attach(10); } void loop() { getSerialData(); if (processing_values[0] == 1) { servo1.write(90); } else { servo1.write(0); } if (processing_values[1] == 1) { servo2.write(90); } else { servo2.write(0); } }
#2 Processing Code
void setup() { background(0); setupSerial(); fullScreen(); } void draw() { background(0); noStroke(); fill(0, 255,255); circle(x, height/2, 100); x += step; if (x<0 || x>width){ step = -step; } if (x< 40){ processing_values[0] = 1; } else{ processing_values[0] = 0; } if (x> width-40){ processing_values[1] = 1; } else{ processing_values[1] = 0; } sendSerialData(); }
Additional Homework
#3 Arduino Code
void setup() { Serial.begin(9600); } void loop() { int sensor1 = digitalRead(10); int sensor2 = digitalRead(11); int sensor3 = digitalRead(A2); Serial.print(sensor1); Serial.print(","); Serial.print(sensor2); Serial.print(","); Serial.print(sensor3); Serial.println(); delay(100); } #3 Processing Code
import processing.serial.*; int NUM_OF_VALUES_FROM_ARDUINO = 3; /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/ int sensorValues[]; /** this array stores values from Arduino **/ String myString = null; Serial myPort; boolean Star1; boolean Star2; int stat1; int stat2; void setup() { size(1000, 800); setupSerial(); } void draw() { getSerialData(); printArray(sensorValues); background(200); fill(255); if (Star1){ star(width*0.3, height*0.3, 30, 70, 5); } if (stat1==0 && sensorValues[0]==1){ Star1 = !Star1; } if (Star2){ star(width*0.7, height*0.7, 80, 100, 40); } if (stat2==0 && sensorValues[1]==1){ Star2 = !Star2; } stat1 = sensorValues[0]; stat2 = sensorValues[1]; if (mousePressed){ background(255); } // use the values like this! // sensorValues[0] // add your code // } 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); } void setupSerial() { printArray(Serial.list()); myPort = new Serial(this, Serial.list()[ 2 ], 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_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++) { sensorValues[i] = int(serialInArray[i]); } } } } }
Leave a Reply