Assignment 11: What Does Your Painting Sound Like?

Hello, Arduino, I’ve missed you! After having spent a few weeks on Processing now, struggling with code and math and logic, I was so happy to return to a project that required LED lights and buzzers and potentiometers. This week, I decided to make a program that would let users make two art pieces at once: sound art and visual art. Users move the mouse around the screen, making whatever kind of visual elliptic art they want, but at the same time also controlling whichever (pained, tortured) note the buzzer is sounding. Thus, the user can hear their painting, and see their music! Here is how it looks/sounds:

What I like about how the project turned out is the interaction between sound and visual. I love the idea of listening to my painting! I also like the watercolor-like effect created by the borderless ellipses on the Processing screen, giving the illusion of an actual painting.

The hardest part of this project was understanding the logic behind the interaction between Arduino and Processing: I was initially confused about the difference between SerialEvent and other functions, but since my code only needed a one-way interaction (sending mouse position to Arduino from Processing), my code has a function called SendData instead of a SerialEvent. This function is enough to accomplish the interaction that I need, but only because it is super small scale and I am physically here to control both sides.

I thoroughly enjoyed using Arduino again after such a long time, although I had forgotten many of the commands over the weeks. For the future of this particular project, I would want to work further on the details of how the sound is created. I would want it to be more pleasing (pls buzzer), and I would also want the coordination between the mouse and the buzzer to be more intuitive (which it currently is not able to fully be due to the delay function).

Here is the code. As always, thanks to James and Nahil for their patience in answering all my coding questions!

//Arduino:

const int buzzer =3;
int freq = 0;
int duration = 0;

void setup() {
 Serial.begin(9600);
 pinMode(3, OUTPUT);
}

void loop() {
 tone(3, freq, duration);
 delay(200);
}

void serialEvent(){
 while (Serial.available()) {
 freq = Serial.parseInt();
 duration = Serial.parseInt();
 if (Serial.read() == '\n') {
 
 }
 }

}
//Processing:

import processing.serial.*;
Serial myPort;
int rainbowCounter = 0;

void setup () {
 fullScreen();
 background(255);
 printArray(Serial.list());
 String portname=Serial.list()[5];
 println(portname);
 myPort = new Serial(this, portname, 9600);
 myPort.clear();
 myPort.bufferUntil('\n');
}

void draw() {
 int x1 = mouseX;
 int y1 = mouseY;

 noStroke();

 fill(rainbowCounter, 138, 255, random(30, 60));
 if (rainbowCounter == 255) {
 rainbowCounter = 0;
 }
 rainbowCounter++;

 ellipse(x1, y1, random(0, 50), random(0, 40));
 
 sendData();
}


void sendData(){
 int xPos = (int)map(mouseX, 0, width, 400, 900);
 int yPos = (int)map(mouseY, 0, height, 400,900);
 myPort.write(xPos+","+yPos +"\n");
}