This week, we were supposed to make a musical instrument which would involve both, a digital and an analog sensor.
For the most part, I faced a bad creative block. I knew I didn’t want anything extremely complicated, but I wanted it to be cool and fun to work with.
Hence, I first created something which wasn’t fully a musical instrument, but it helped me get a good idea about working with sounds in Arduino. I created an “instrument” consisting of a switch (digital sensor) and a photo resistor (analog sensor). The main idea here was to let changes in the button state and in the input generated by the photo resistor to play the intro song of Game of Thrones (9 out of 10 people who listened to it said it was close to the real intro song, so I’m hoping it’s not very disappointing).
I’ll be very honest, the GoT thing hit me before I did any research about it. Luckily, Google helped me find the notes which could be used to produce the song, and boom, a lot of my work was done. I coded the Arduino in a way where I stored all of the notes in an array (in order) and whenever the button was pressed or light was flashed (for the photo resistor), the next note of the song would be played. This allowed me to play the entire song in one go using ninety-five notes, where each note was played by either pressing the button or flashing light. The song could also be played any number of times. The video is attached below.
However, this wasn’t my best work and I knew I could do more. I wasn’t entirely satisfied with it because although I had used a digital and an analog sensor to create sound and make an “instrument”, I coded it in a way where any button press/flashing of light would only play the next note in a sequence which was already coded. Although playing the GoT intro song using the Arduino was something I found very cool, I felt the user didn’t have the liberty to play whatever note they wanted to as they usually do in a normal instrument.
So, I modified my work in the following way: I kept the photo resistor but instead of having one button, I now had 3. The combination of buttons and the photo resistor allowed me to implement the 7 most basic notes; Do, Re, Mi, Fa, Sol, La, Ti. I implemented them in the following manner.
- Pressing the first button (red) without light plays Do.
- Pressing the second button (yellow) without light plays Re.
- Pressing the third button (blue) without light plays Mi.
- Flashing the light/turning it on plays Fa.
- Pressing the red button with the light on plays Sol.
- Pressing the yellow button with the light on plays La.
- Pressing the blue button with the light on plays Ti.
This implementation allowed me to have an instrument which could play the 7 fundamental notes using 7 different combinations of the buttons and photo resistor. The use of relevant conditional statements allowed me to achieve this behaviour.
The video below shows me using the instrument by working with the buttons and flashing lights while singing “D0-Re-Mi: The Sound of Music (1965)”; a song which was taught to us in elementary school. I am sorry for my horrible voice but years later, I was really vibing with the song.
The code for both, the GoT intro song and the main instrument can be found below.
#define NOTE_B0 31 #define NOTE_C1 33 #define NOTE_CS1 35 #define NOTE_D1 37 #define NOTE_DS1 39 #define NOTE_E1 41 #define NOTE_F1 44 #define NOTE_FS1 46 #define NOTE_G1 49 #define NOTE_GS1 52 #define NOTE_A1 55 #define NOTE_AS1 58 #define NOTE_B1 62 #define NOTE_C2 65 #define NOTE_CS2 69 #define NOTE_D2 73 #define NOTE_DS2 78 #define NOTE_E2 82 #define NOTE_F2 87 #define NOTE_FS2 93 #define NOTE_G2 98 #define NOTE_GS2 104 #define NOTE_A2 110 #define NOTE_AS2 117 #define NOTE_B2 123 #define NOTE_C3 131 #define NOTE_CS3 139 #define NOTE_D3 147 #define NOTE_DS3 156 #define NOTE_E3 165 #define NOTE_F3 175 #define NOTE_FS3 185 #define NOTE_G3 196 #define NOTE_GS3 208 #define NOTE_A3 220 #define NOTE_AS3 233 #define NOTE_B3 247 #define NOTE_C4 262 #define NOTE_CS4 277 #define NOTE_D4 294 #define NOTE_DS4 311 #define NOTE_E4 330 #define NOTE_F4 349 #define NOTE_FS4 370 #define NOTE_G4 392 #define NOTE_GS4 415 #define NOTE_A4 440 #define NOTE_AS4 466 #define NOTE_B4 494 #define NOTE_C5 523 #define NOTE_CS5 554 #define NOTE_D5 587 #define NOTE_DS5 622 #define NOTE_E5 659 #define NOTE_F5 698 #define NOTE_FS5 740 #define NOTE_G5 784 #define NOTE_GS5 831 #define NOTE_A5 880 #define NOTE_AS5 932 #define NOTE_B5 988 #define NOTE_C6 1047 #define NOTE_CS6 1109 #define NOTE_D6 1175 #define NOTE_DS6 1245 #define NOTE_E6 1319 #define NOTE_F6 1397 #define NOTE_FS6 1480 #define NOTE_G6 1568 #define NOTE_GS6 1661 #define NOTE_A6 1760 #define NOTE_AS6 1865 #define NOTE_B6 1976 #define NOTE_C7 2093 #define NOTE_CS7 2217 #define NOTE_D7 2349 #define NOTE_DS7 2489 #define NOTE_E7 2637 #define NOTE_F7 2794 #define NOTE_FS7 2960 #define NOTE_G7 3136 #define NOTE_GS7 3322 #define NOTE_A7 3520 #define NOTE_AS7 3729 #define NOTE_B7 3951 #define NOTE_C8 4186 #define NOTE_CS8 4435 #define NOTE_D8 4699 #define NOTE_DS8 4978 // code for GoT intro song is below (commented) //int sensorPin = 2; //int speakerPin = 4; //int photoPin = A0; // //// setting the current and previous states for the button and (values) for the photoresistor //boolean buttonState = LOW; //boolean previousButtonState = LOW; // //int photoValue; //int previousPhotoValue = 0; // //int i = 0; // //int notes[95] = {NOTE_G4, NOTE_C4, NOTE_DS4, NOTE_F4, NOTE_G4, NOTE_C4, NOTE_DS4, NOTE_F4, NOTE_G4, NOTE_C4, NOTE_DS4, NOTE_F4, NOTE_G4, NOTE_C4, NOTE_DS4, NOTE_F4, NOTE_G4, NOTE_C4, NOTE_E4, // NOTE_F4, NOTE_G4, NOTE_C4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_C4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_C4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_C4, NOTE_DS4, NOTE_F4, NOTE_D4, NOTE_G3, // NOTE_AS3, NOTE_C4, NOTE_D4, NOTE_G3, NOTE_AS3, NOTE_C4, NOTE_D4, NOTE_G3, NOTE_AS3, NOTE_C4, NOTE_D4, NOTE_G3, NOTE_AS3, NOTE_C4, NOTE_D4, NOTE_F4, NOTE_AS3, NOTE_DS4, // NOTE_D4, NOTE_F4, NOTE_AS3, NOTE_DS4, NOTE_D4, NOTE_C4, NOTE_GS3, NOTE_AS3, NOTE_C4, NOTE_F3, NOTE_GS3, NOTE_AS3, NOTE_C4, NOTE_F3, NOTE_GS3, NOTE_AS3, NOTE_C4, NOTE_F3, // NOTE_G4, NOTE_C4, NOTE_DS4, NOTE_F4, NOTE_G4, NOTE_C4, NOTE_DS4, NOTE_F4, NOTE_D4, NOTE_G3, NOTE_AS3, NOTE_C4, NOTE_D4, NOTE_G3, NOTE_AS3, NOTE_C4, NOTE_D4, NOTE_G3, NOTE_AS3, // NOTE_C4, NOTE_D4}; // //void setup() // { // pinMode(speakerPin,OUTPUT); // pinMode(sensorPin,INPUT); // Serial.begin(9600); // } // //void loop(){ // buttonState = digitalRead(sensorPin); // photoValue = analogRead(photoPin); // if(buttonState == HIGH && previousButtonState == LOW) // { // // the %95 allows us to play notes all over again // tone(speakerPin, notes[i%95], 250); // i++; // } // previousButtonState = buttonState; // // if (photoValue >= 400 && previousPhotoValue < 400) { // tone(speakerPin, notes[i%95], 250); // // i++ allows us to move to the next note in the array // i++; // } // previousPhotoValue = photoValue; // // Serial.println(photoValue); // } // code for main instrument is below // assigning the pin for each component to a variable int button1Pin = 7; int button2Pin = 2; int button3Pin = 8; int photoPin = A0; int speakerPin = 4; // setting the current and previous state variables for all 3 buttons and the photoresistor value boolean button1state = LOW; boolean button1previousState = LOW; boolean button2state = LOW; boolean button2previousState = LOW; boolean button3state = LOW; boolean button3previousState = LOW; int photoValue; int previousPhotoValue = 0; // creating an array called notes which stores the notes Do,Re,Mi,Fa,Sol,La,TI int notes[7] = {NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4}; void setup() { // assigning the pin modes to the components pinMode(button1Pin, INPUT); pinMode(button2Pin, INPUT); pinMode(button3Pin, INPUT); pinMode(speakerPin, OUTPUT); Serial.begin(9600); } void loop() { // reading in a digital manner the state of the button and in an analog manner the input generated by the photoresistor button1state = digitalRead(button1Pin); button2state = digitalRead(button2Pin); button3state = digitalRead(button3Pin); photoValue = analogRead(photoPin); // specifying conditional statements for different combinations of components to play different notes if (button1state == HIGH && button1previousState == LOW && button2state == LOW && button3state == LOW && photoValue < 400){ tone(speakerPin, notes[0], 250); } if (button2state == HIGH && button2previousState == LOW && button1state == LOW && button3state == LOW && photoValue < 400){ tone(speakerPin, notes[1], 250); } if (button3state == HIGH && button3previousState == LOW && button1state == LOW && button2state == LOW && photoValue < 400){ tone(speakerPin, notes[2], 250); } if (button3state == LOW && button2state == LOW && button1state == LOW && photoValue >= 400 && previousPhotoValue < 400){ tone(speakerPin, notes[3], 250); } if (button1state == HIGH && button1previousState == LOW && button2state == LOW && button3state == LOW && photoValue >= 400){ tone(speakerPin, notes[4], 250); } if (button2state == HIGH && button2previousState == LOW && button1state == LOW && button3state == LOW && photoValue >= 400){ tone(speakerPin, notes[5], 250); } if (button3state == HIGH && button3previousState == LOW && button1state == LOW && button2state == LOW && photoValue >= 400){ tone(speakerPin, notes[6], 250); } button1previousState = button1state; button2previousState = button2state; button3previousState = button3state; previousPhotoValue = photoValue; }
I love both versions, nice work Maaz.