I reused last week's project for this week's assignment but enhanced it by programming the LED lights to interact with music using two different switches.
the software:
thinker cad
Arduino
RDworks
adobe illustrator
the materials:
construction parts
acrylic sheets
plywood
blu tack
ُُElectronic components
breadboard
Arduino uno board
LEDs (12 pcs)
multiple wires (Jumpers)
microwave sensor (only week06 ) no need now
resistors 330ohms (12 pcs)
buzzer (for weeks 07 & 08)
2 slider switch (for week 07& 08)
Bluetooth module (for week08)
the machines:
laser-cut machine
First, I prepared the components in Tinkercad. I gathered an Arduino, a breadboard, 12 LEDs, 12 resistors, a buzzer, and two switches
While coding, I used an array variable, a concept introduced to me by Ahmed Sami when I was exploring music codes. You can check it out here.I also found another helpful resource available here that helped me with LEDS sequence code writing with (for loops). array is a data structure that stores a collection of elements, usually of the same type, in contiguous memory locations. This means you can group multiple values under a single variable name and access each one by its index (with the first element typically at index 0). Arrays are useful for organizing and manipulating lists of data efficiently.
final simulation
First, I need to either download the "pitches.h" library or manually define the musical notes by their frequencies. Then, I define the switches, buzzers LEDs, and the number of LEDs along with their corresponding pins using the const int structure.
Next, I create arrays for both melodies and set the tempo for each, defining the speed for the Harry Potter and Star Wars melodies, respectively. Tempo is measured in beats per minute (BPM), which determines the playback speed of each tune.
int wholenoteHarry = (60000 * 4) / tempoHarry; and int wholenoteStar = (60000 * 4) / tempoStar: These lines calculate the duration of a whole note in milliseconds for each melody. 60000 is the number of milliseconds in a minute. 4 represents the number of beats in a whole note (common in 4/4 time signature). The formula (60000 * 4) / tempo calculates how long a whole note should last in milliseconds for the given tempo. So Harry would be 1666.67 ms, and Star Wars would be 2222.22ms
The setup() function initializes the hardware: The buzzer pin is set as an output to play sounds. The switchHarry and switchStar pins are set as inputs with internal pull-up resistors. This means the switches will read low when not pressed and high when pressed. and a loop sets up the LED pins (stored in the ledPinsHarry and ledPinsStar arrays) as outputs.
The loop() function continuously checks the state of the switches and plays the corresponding melody if a switch is pressed: If switch array is pressed (HIGH), the play melody() function is called with: melodyHarry: The array containing the Harry Potter melody. sizeof(melodyHarry) / sizeof(melodyHarry[0]: Calculates the number of notes in the melody. wholenoteHarry: The duration of a whole note for Harry Potter. ledPinsHarry: The array of LED pins for the Harry Potter sequence. and the same thing with Star Wars
Lastly, No Switch Pressed: If neither switch is pressed, the buzzer is stopped using noTone(buzzer), and all LEDs are turned off.
I had to apply some blu Tack to the buzzer to reduce its loudness.
I connected the LEDs to pins 2, 3, 4, 5, 6, and 7, and the buzzer to pin 9, with its other terminal on GND. The switch had three pin one on 5v, another on GND, and the input wire connected to pins 10 and 11.
I had an issue with the red switch I wasn’t sure why, but when I tested it with an avo meter, it showed no problems. However, I ran out of crocodile wires since I only had five, but I needed six (three for the switch). So, I tried using Scotch tape to secure an ordinary male-to-male wire, but I think that was the problem it was too loose. In the end, I replaced it with a slider switch, which worked better.
Star Wars testing with a button alone
Harry Potter testing with a button alone
Combining the 2 buttons together fails
Everything worked fine when I tested each component separately, but when I tried combining the two buttons, it completely failed. I was exhausted and so frustrated that I was this close to destroying my PC!
So, what was the problem exactly?
First, I used the wrong type of buttons, as you can see in the video, which led me to switch to an on/off button it somehow worked better, though I’m not sure why.
Second, I completely forgot to use INPUT_PULLUP. Whoever is reading this, please don’t make the same mistake!
Third, I was completely dazzled by the overwhelming number of if and else statements, which made debugging even more confusing.
finally worked
I wasn’t sure how to synchronize the LEDs with the melodies I downloaded because I didn’t understand the code. Ahmed Sami looked at it and tried to explain it to me. He suggested using the array function and advised me to research it further.
I had already been struggling with how to integrate an LED sequence with music for my assignment and final project. I searched a lot to understand how music code works so I could properly sync the LEDs, but it was quite difficult for me. Ahmed Sami helped me try to make sense of it.
Then, another idea came to mind: what if I played music using a music module? However, the challenge was detecting the exact duration of each tone. I did a lot of research and found some possible solutions, but I didn’t fully understand them. Ahmed Sami went through it with me and pointed out that it would be a big hassle. He suggested either doing it manually in Adobe Premiere or simply turning both the music and LEDs on randomly.
Later, another idea came up: what if I used a music sensor to detect the sound? to make something like the music visualizer This seemed like a more feasible approach, and Ahmed Sami and Abdelrahman gave me some instructions and things to keep in mind while setting it up.
I faced many problems while fabricating; I talked about them in the previous link above in the final project journal.
at first, I wrote this in void set up for (int i = 0; i < numLeds; i++) which made all LED lights up in a static way
So I write this to select an LED based on note position: int ledIndex = (thisNote / 2) % numLeds; which made only pins on even numbers interact with music so I deleted and went again with the previous line like this in void set up but reused it again in void loops in play melody, which ChatGPT offered me to do it
When integrating multiple buttons, I needed to research how to handle multiple input states without interference.
When adjusting LED interactions, I studied how to distribute timing across all six LEDs rather than just a few.
Debugging timing and delays required learning more about how delays interact with button presses in an interactive loop.
Overlooking Input Pull-ups: Forgetting to enable INPUT_PULLUP for button inputs can cause false readings.
Buzzer & LED Conflicts: If the buzzer and LEDs are running on the same timing logic, it might cause flickering or missed beats.
Searching online and using ChatGPT helped me a lot with debugging, especially when dealing with the two-button state.