back
Written by Arthur Jensen — November 20th 2024

Creating a Music Machine

Create a music-making system using Arduino to connect triggers and controls and Processing to play sounds.
Two drums on a blue background.
Overview of Project

This project focuses on creating an interactive music-making system that combines physical hardware, software programming, and creative design. By integrating an Arduino-based control interface with a Processing-based sound system, you will build a functional tool for generating and manipulating music, enhanced with dynamic visuals.

Final Outcome: Creating a Drum

Will this be used for live performance or pre-recorded music?
This instrument will be used for pre-recorded music.

How will use make the noise - by triggering sampled audio files or by making synthetic sounds yourself?
My synthetic drum triggers a sampled audio file.

What kind of visuals will accompany your music?
For the visuals, I plan to display a square in a neutral color on a colored background. When the drum is triggered, the background will change from black to white. This change serves as a visual cue to indicate to the user that the drum has been activated.

Consider the interface: Which sensors will you use and which parameters will they control? How will the controls be laid out?
I incorporated a button sensor that activates when the user presses the top of the drum.

Creating the code for Arduino

This Arduino code reads input values from three sensors—a potentiometer (connected to analog pin A0), a photoresistor (connected to analog pin A1), and a button (connected to digital pin 12)—and sends the data to the serial monitor. The setup() function configures the pins as inputs and initializes serial communication at a baud rate of 9600. In the loop(), it continuously reads the sensor values, formats them as a comma-separated string, and sends them to the serial monitor, updating every 50 milliseconds.

1#define POT_PIN A0 
2#define BUTTON 12 
3#define PHOTO A1 
4
5
6void setup() { 
7// put your setup code here, to run once' 
8pinMode(POT_PIN, INPUT); 
9pinMode(BUTTON, INPUT); 
10pinMode(PHOTO, INPUT); 
11Serial.begin(9600); } void loop() { 
12
13// put your main code here, to run repeatedly; 
14float potValue = analogRead(POT_PIN); 
15float photoValue = analogRead(PHOTO); 
16int buttonValue = digitalRead(BUTTON); 
17Serial.print(potValue); Serial.print(","); 
18Serial.print(photoValue); 
19Serial.print(","); 
20Serial.println(buttonValue); delay(50); 
21}
22
Code that reads sensor values and sends them to the serial monitor.
Creating Code for Open Processing

This OpenProcessing code creates an interactive visual and audio system that responds to inputs from an Arduino. It uses serial communication to receive sensor data from a potentiometer, photoresistor, and button. The draw() function adjusts the visuals based on sensor values: the button toggles the background between black and white while also triggering a kick drum sound, the photoresistor affects the square's color, and the potentiometer controls the square's size. The serialEvent() function processes incoming serial data, mapping the sensor values to usable ranges for size, color, and the button's state. The program integrates sound and dynamic visuals for an engaging interactive experience.

1import processing.serial.*;
2import processing.sound.*;
3SoundFile kickDrum;
4
5Serial myConnection;
6String incomingData = "";
7float pot = 0;
8float photo = 0;
9float button = 0;
10
11void setup(){
12  size(600, 600);
13  printArray(Serial.list());  // List all available ports to verify the correct one
14  
15  //kick drum:
16  kickDrum = new SoundFile(this, "BT7AADA.WAV");
17  
18  // Open serial connection; adjust index if needed
19  myConnection = new Serial(this, Serial.list()[3], 9600);
20  myConnection.bufferUntil('\n');  // Buffer until newline character
21}
22
23// Change Color Visual
24void draw() {
25  // Change background based on button state
26  if (button > 0){
27    background(255);  // White when button is pressed
28    kickDrum.play();
29  } else {
30    background(0);  // Black otherwise
31  }
32  
33  // Use photoresistor value to influence color of the square
34  fill(photo, 255 - photo, 100 + photo); // Color changes with light sensor
35  
36  // Draw square shape using pot as the "size"
37  float halfSize = pot / 2;
38  rectMode(CENTER);  // Draw rectangle from the center
39  rect(width / 2, height / 2, pot, pot);  // Square with side length 'pot'
40}
41
42void serialEvent(Serial conn){
43  incomingData = conn.readString();  // Read the incoming serial data
44  String[] values = split(trim(incomingData), ",");  // Split the data
45  println(values);  // Print received values for debugging
46  
47  // Map the received values for use in Processing
48  pot = map(float(values[0]), 0, 4095, 50, width); // Potentiometer to size
49  photo = map(float(values[1]), 0, 4095, 0, 255);  // Photoresistor to color
50  button = float(values[2]);  // Button state
51  
52  printArray(values);  // For debugging, print received values
53}
Interactive visuals and sound react to Arduino sensor inputs dynamically.
Capturing the Hardware

I built a drum using a button sensor attached to a cutout taped on top of a paper cup. I chose a simple paper cup because its cylindrical shape resembles a drum. One leg of the button is connected to a digital pin on the Arduino, while the other leg is connected to power. A resistor is connected to the button to complete the circuit, ensuring that when the button is not pressed, the circuit remains connected to the ground through the resistor.

Drum built with button sensor on a cylindrical paper cup.
Reflection

Through this assignment, I learned how to integrate hardware and software to create an interactive system that combines sound, visuals, and physical controls. I gained a deeper understanding of using Arduino for sensor-based inputs and Processing to interpret data and generate dynamic outputs. Designing the drum interface taught me how to apply creative problem-solving, such as repurposing everyday materials, while also reinforcing the importance of circuit design and communication between devices. Overall, this project enhanced my skills in coding, electronics, and interactive design.