Processing Documentations
Exercise 1: Make a Processing Etch A Sketch
For this exercise, I used two potentiometers to control the position of the pen on processing. The Arduino could read the data from potentiometers and send them to the processing.
The interaction in this exercise is pretty intuitive that the user can interact and control the pen by switching the potentiometers.
Exercise 2: Make a musical instrument with Arduino
In this exercise, the buzzer is controlled by the position of the mouse in the processing. The circuit building part isn’t that hard, all I have to do is to connect the buzzer to the Arduino. For the coding part, I struggled sometime trying to figure out how the duration variable of the buzzer works. Finally, I found out one way that only when pressing the mouse would the buzzer work, in this way, the buzzer can show both the frequency and the duration. So, I passed the third variable to the Arduino to show whether the mouse is pressed.
The interaction in this exercise is similar to exercise 1, that user’s interaction is directed showed by the sound of the buzzer.
Homework Documentations
For the homework, since the codes for the star shape as well as the rotating codes are given to us, the processing part code isn’t that hard. For the Arduino part, the difficult part is to detect whether the button is pressed, and how should the press of button operation being illustrated in integer variables. I used HIGH and several if statements to accomplish that. Also, one difficulty I met was how to connect the buttons to the Arduino, I checked the documentations before to help me build the circuits of buttons and resistors.
For the Interaction, by pressing the button, the star would show up or disappear. The interaction here is pretty simple and easy.
Below are the long codes:
Exercise 1:
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); }
import processing.serial.*; float x; float y; float px; float py; String myString = null; Serial myPort; int NUM_OF_VALUES = 2; int[] sensorValues; void setup() { size(600, 600); background(255); setupSerial(); } void draw() { px = sensorValues[0]; py = sensorValues[1]; updateSerial(); x = sensorValues[0]; y = sensorValues[1]; printArray(sensorValues); line(px, py, x, y); } void setupSerial() { printArray(Serial.list()); myPort = new Serial(this, Serial.list()[1], 9600); myPort.clear(); myString = myPort.readStringUntil( 10 ); myString = null; sensorValues = new int[NUM_OF_VALUES]; } void updateSerial() { while (myPort.available() > 0) { myString = myPort.readStringUntil( 10 ); 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:
#define NUM_OF_VALUES 3 int tempValue = 0; int valueIndex = 0; float fre ; float dur ; int pin = 9; int values[NUM_OF_VALUES]; void setup(){ Serial.begin(9600); pinMode(pin, OUTPUT); } void loop() { getSerialData(); fre = map (values[0], 0, 500, 0, 3000); if (values[2] == 1){ tone(pin, fre, values[1]); } } void getSerialData() { if (Serial.available()) { char c = Serial.read(); switch (c) { case '0'...'9': tempValue = tempValue * 10 + c - '0'; break; case ',': values[valueIndex] = tempValue; tempValue = 0; valueIndex++; break; case 'n': values[valueIndex] = tempValue; tempValue = 0; valueIndex = 0; break; 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; } } }
import processing.serial.*; int NUM_OF_VALUES = 3; Serial myPort; String myString; int values[] = new int[NUM_OF_VALUES]; void setup() { size(500, 500); background(255); printArray(Serial.list()); myPort = new Serial(this, Serial.list()[1], 9600); myPort.clear(); myString = myPort.readStringUntil( 10 ); myString = null; } void draw() { background(255); values[0] = mouseX; values[1] = mouseY; if (mousePressed) { values[2] = 1; }else{ values[2] = 0; } line(pmouseX, pmouseY, mouseX, mouseY); sendSerialData(); println(values); } void sendSerialData() { String data = ""; for (int i=0; i<values.length; i++) { data += values[i]; if (i < values.length-1) { data += ","; } else { data += "n"; } } myPort.write(data); } void echoSerialData(int frequency) { if (frameCount % frequency == 0) myPort.write('e'); String incomingBytes = ""; while (myPort.available() > 0) { incomingBytes += char(myPort.read()); } print( incomingBytes ); }
Homework:
int button1; int button2; int prebutton1 = LOW; int prebutton2 = LOW; int values1; int values2; void setup() { Serial.begin(9600); pinMode(9, INPUT); pinMode(11, INPUT); } void loop() { int button1 = digitalRead(9); int button2 = digitalRead(11); if (button1 == HIGH) { if (button1 != prebutton1){ if (values1 == 1){ values1 = 0; }else{ values1 = 1; } } } prebutton1 = button1; if (button2 == HIGH) { if (button2 != prebutton2){ if (values2 == 1){ values2 = 0; }else{ values2 = 1; } } } prebutton2 = button2; Serial.print(values1); Serial.print(","); Serial.print(values2); Serial.println(); delay(100); }
import processing.serial.*; float x; float y; String myString = null; Serial myPort; int NUM_OF_VALUES = 2; int[] sensorValues; void setup() { size(600, 600); background(0); setupSerial(); } void draw() { updateSerial(); printArray(sensorValues); background(0); showImage(); } void setupSerial() { printArray(Serial.list()); myPort = new Serial(this, Serial.list()[1], 9600); myPort.clear(); myString = myPort.readStringUntil( 10 ); myString = null; sensorValues = new int[NUM_OF_VALUES]; } void updateSerial() { while (myPort.available() > 0) { myString = myPort.readStringUntil( 10 ); 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]); } } } } } 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 showImage(){ if (sensorValues[0] == 1){ pushMatrix(); translate(width*0.3, height*0.3); rotate(frameCount / 200.0); star(0, 0, 30, 70, 5); popMatrix(); } if (sensorValues[1] == 1){ pushMatrix(); translate(width*0.7, height*0.7); rotate(frameCount / 100.0); star(0, 0, 80, 100, 40); popMatrix(); } }