Concept:
For this assignment, we wanted to go with something simple. Rashed is known for his love for music so I was excited to see what we would come up with. During our brainstorming session, Rashed was doing his hobby – which is music production- on his phone on GarageBand and he noticed how he had to click on the “arrow” button to change the pitch of the piano and we decided we wanted to do something like that but with the potentiometer.
The pitch-changing arrow I was referring to on GarageBand:
Materials used:
- x11 Wires
- x4 Buttons
- x4 10k Resistors
- x1 Buzzer
- x1 Potentiometer
Production:
Writing this code was pretty challenging for us as both of us are still adapting to this new language.
We used the toneMelody example from the examples folder that we used in class to take the “pitches.h” tab from it in order to give a specific range. We collectively decided to have the buttons, by default, be the most basic notes “Do, Re, Me, Fa” also known as “NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4” which was pretty simple to implement.
However, we faced one major issue:
For some reason, the potentiometer would not cooperate at all. The buttons were working fine, but the pitch won’t budge. We, unfortunately, had to use ChatGPT to help us but it was still very helpful in helping us understand what we were doing wrong.
Here’s the code:
#include "pitches.h" const int speakerPin = 9; // Speaker connected to pin 9 int buttonPins[] = {2, 3, 4, 5}; // Button pins for notes int potPin = A0; // Potentiometer connected to A0 // Initialize an array of notes that will be used depending on the button press int noteIndices[] = {NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4}; // Starting notes for each button void setup() { for (int i = 0; i < 4; i++) { pinMode(buttonPins[i], INPUT_PULLUP); // Setup button pins as input with pull-up resistor } pinMode(speakerPin, OUTPUT); // Set speaker pin as output } void loop() { int potValue = analogRead(potPin); // Read the current value from the potentiometer int noteRange = NOTE_C6 - NOTE_C4; // Define the range of notes to span int noteOffset = map(potValue, 0, 1023, 0, noteRange); // Map potentiometer value to note range for (int i = 0; i < 4; i++) { if (digitalRead(buttonPins[i]) == LOW) { int noteToPlay = noteIndices[i] + noteOffset; tone(speakerPin, noteToPlay); // Play the note on the speaker delay(200); // A short delay to help debounce the button while (digitalRead(buttonPins[i]) == LOW); // Wait for button release noTone(speakerPin); // Stop the note } } }
Here’s the video where we use it:
Reflection:
I think, despite chatGPT’s involvement in this, we did pretty well; the concept was there, the energy and effort that was put into this is there and we’re both proud of our little creation. However, we both do with we took it a step further and maybe added more buttons or tried to use the sensor instead of the potentiometer. We really hope to get used to and adapt to this language.