Exercise 1: Sketch
Diagram:
Video:
Code:
Processing:
import processing.serial.*;
String myString = null;
Serial myPort;
int NUM_OF_VALUES = 2;
int[] sensorValues;
void setup() {
size(500, 500);
background(0);
setupSerial();
}
void draw() {
updateSerial();
printArray(sensorValues);
fill(sensorValues[0], sensorValues[1], 20);
ellipse(sensorValues[0], sensorValues[1], 20, 20);
}
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]);
}
}
}
}
}
Arduino:
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(A0)/ 4;
int sensorValue2 = analogRead(A1)/ 4;
Serial.print(sensorValue);
Serial.println(“,”);
Serial.print(sensorValue2);
Serial.println();
delay(10);
}
Process: I started this exercise from the circuit. At first, I kind of forgot which legs of the potentiometers should be connected with which pin on the Arduino, so I sought help from my deskmate and soon solved this question. Then I began to work on the code part based on the sample code. It was not hard to find out which parts in the sample code should be modified into the required function, such as the port number and the value number. I also add the variation of colors following the value variation from potentiometers. The effect was pretty.
Exercise 2: Music Instrument
Diagram:
Video:
Code:
Arduino:
#define NUM_OF_VALUES 2
int tempValue = 0;
int valueIndex = 0;
int values[NUM_OF_VALUES];
void setup() {
Serial.begin(9600);
pinMode(9, OUTPUT);
}
void loop() {
getSerialData();
tone(9,values[0],values[1]);
}
void getSerialData() {
if (Serial.available()) {
char c = Serial.read();
switch (c) {
case ‘0’…’9′:
tempValue = tempValue * 10 + c – ‘0’;
break;
case ‘,’:
tempValue = 0;
valueIndex++;
break;
case ‘n’:
values[valueIndex] = tempValue;
tempValue = 0;
valueIndex = 0;
break;
case ‘e’:
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;
}
}
}
Processing:
import processing.serial.*;
int NUM_OF_VALUES = 2;
Serial myPort;
String myString;
int values[] = new int[NUM_OF_VALUES];
void setup() {
size(500, 500);
background(0);
printArray(Serial.list());
myPort = new Serial(this, Serial.list()[ 1 ], 9600);
myPort.clear();
myString = myPort.readStringUntil( 10 );
myString = null;
}
void draw() {
background(0);
for (int i=0; i<values.length; i++) {
values[0] = mouseX;
values[1] = mouseY;
}
sendSerialData();
echoSerialData(200);
}
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 );
}
Process: Exercise two were made very smoothly after the experience of exercise 1. I found the parts in the sample codes which needed to be rewritten very fast and changed them to fit the requirement. And my first try succeeded.