Exercise 1: Make a Processing Etch A Sketch
This portion of the recitation did not give me much trouble. After setting up the correct port, I knew that I would need to map the values from the potentiometers into the variables which would be the x and y coordiates of my ellipse. After asking where I should put the ellipse and the variables in the sketch, I wrote the code fairly quickly. I asked for some advice from Kate on making sure that the mapping was done correctly, and it worked fine with a test.
I do wish that I could make the “etch a sketch” smoother, but I felt rushed to get to the next exercise (as I knew it would be more difficult).
Exercise 2: Make a musical instrument with Arduino
This portion of the recitation gave me a lot of trouble. I had no idea where to start — I had to ask several times where I would define the values of the array. After figuring that out, I quickly wrote a code stating that if (keyPressed == true), one of several keys could fill in a value in the array:
if (keyPressed == true){
if (key == 'a'){
values[1] = 'a';
}
and so on. For the arduino part of the project, I used simple if statements along with the tone function.
This didn’t work.
It took me a long time to figure out why it wasn’t working — I messed with the delay on the arduino side, eventually taking it out altogether. I also tried many combinations of if/else statements, as well as adding another variable to stand for “no tone”. I also moved the processing portion of the code from void draw to void keypressed. I’m not sure if this made any difference, but I ended up leaving it in the final product.
Finally, I simplified the code in processing as much as I could and used if and if/else statements in arduino. This finally made the project work — by pressing a,s,d or f on the keyboard, the buzzer plays a different tone.
Code for Processing:
import processing.serial.*;
int NUM_OF_VALUES = 4;
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()[ 0 ], 9600);
myPort.clear();
myString = myPort.readStringUntil( 10 ); // 10 = '\n' Linefeed in ASCII
myString = null;
}
void keyPressed() {
if (key == 'a') {
values[0] = 'a';
}
if (key == 's') {
values[1] = 's';
}
if (key == 'd') {
values[2] = 'd';
}
if (key == 'f') {
values[3] = 'f';
}
}
void draw() {
background(0);
sendSerialData();
echoSerialData(200);
}
void sendSerialData() {
String data = "";
for (int i=0; i<values.length; i++) {
data += values[i];
if (i < values.length-1) {
data += ","; // add splitter character "," between each values element
}
else {
data += "n"; // add the end of data character "n"
}
}
myPort.write(data);
}
void echoSerialData(int frequency) {
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( incomingBytes );
}
Code for Arduino (including just the void loop):
void loop() {
getSerialData();if (values[0] == 'a'){
tone(6, 262);
} else if (values[1] == 's'){
tone(6,196);
} else if (values[2] == 'd'){
tone(6,220);
} else if (values[3] == 'f'){
tone(6,247);
}