This week we worked with the sound on Arduino, and I played around and tried to create a “musical instrument.” To mention before looking at my work: I have (had) zero knowledge of musical theory, and I had a hard time checking whether notes that play are the right ones. Also, I didn’t know how tempo works.
Let’s look at my “instrument”!
It consists of 4 buttons, buzzer, photoresistor, and servo. Initially, there were three buttons, but I added the yellow one so “twinkle twinkle little star” can play. I spent too much time figuring out how to make these arrays work not to include them. However, I still haven’t figured out how to use it without “delay” and gave up.
The other three buttons are responsible for one note each, namely C4, F4, and G4. Simultaneously, by blocking the light from the photoresistor (this example was cool), the servo moves accordingly. I tried to make it sound louder by placing it on my RedBull can.
Result:
Code:
#include "pitches.h" #include <Servo.h> const int tonePin = 4; const int buttonPin1 = 12; const int buttonPin2 = 2; const int buttonPin3 = 5; const int buttonPin4 = 3; const int servoPin = 9; Servo servo; const int rows = 6; const int columns = 7; int notes[rows][columns] = {{NOTE_C4, NOTE_C4, NOTE_G4, NOTE_G4, NOTE_A4, NOTE_A4, NOTE_G4}, {NOTE_F4, NOTE_F4, NOTE_E4, NOTE_E4, NOTE_D4, NOTE_D4, NOTE_C4}, {NOTE_G4, NOTE_G4, NOTE_F4, NOTE_F4, NOTE_E4, NOTE_E4, NOTE_D4}, {NOTE_G4, NOTE_G4, NOTE_F4, NOTE_F4, NOTE_E4, NOTE_E4, NOTE_D4}, {NOTE_C4, NOTE_C4, NOTE_G4, NOTE_G4, NOTE_A4, NOTE_A4, NOTE_G4}, {NOTE_F4, NOTE_F4, NOTE_E4, NOTE_E4, NOTE_D4, NOTE_D4, NOTE_C4} }; int tempo[rows][columns] = {4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 2}; void setup() { Serial.begin (9600); servo.attach(servoPin); pinMode(tonePin, OUTPUT); pinMode(buttonPin1, INPUT); pinMode(buttonPin2, INPUT); pinMode(buttonPin3, INPUT); pinMode(buttonPin4, INPUT); } void loop() { int prValue = analogRead (A1); int mapValue = map(prValue, 185, 610, 0, 180); servo.write(mapValue); if (digitalRead (buttonPin1) == HIGH) { int noteLength = 250; tone(4, NOTE_C4, noteLength); } if (digitalRead (buttonPin2) == HIGH) { int noteLength = 250; tone(4, NOTE_F4, noteLength); } if (digitalRead (buttonPin3) == HIGH) { int noteLength = 250; tone(4, NOTE_G4, noteLength); } if (digitalRead (buttonPin4) == HIGH){ for (int i = 0; i < rows; ++i) { for (int j = 0; j < columns; ++j) { int noteLength = 1000 / tempo[i][j]; int pausee = noteLength * 1.30; tone(4, notes[i][j], noteLength); delay(pausee); } } } } #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
Not bad for not knowing anything about music! I want to see what is happening with the servo though, it’s not really in the camera frame. Let’s meet and figure out how to blink without delay, it’s an important concept to wrap your mind around. Take a look at how we did it in class again: https://intro.nyuadim.com/2021/03/27/blink-without-delay/ See if you can change your sketch to play twinkle twinkle without dealy. And, if not, let me know and we can work on it together.