Concept:
For this week’s project, we tried to make a musical instrument using a servo motor and tone. We imagined our project to be something like a keyboard to play music but instead, it would have buttons to make sounds. The servo motor would work as the ‘metronome’ or the ‘beat’. So while the beat is playing constantly in the background, you can create music according to the beat with the buttons.
Schematic:
Program:
https://github.com/ilaydaveziroglu/week4
#include #include "pitches.h" Servo myservo; int pos = 0; void setup() { Serial.begin(9600); pinMode (12, OUTPUT); pinMode (11, OUTPUT); pinMode (10, OUTPUT); myservo.attach(9); pinMode(4, INPUT); } void loop() { int yellowbutton = digitalRead(4); int bluebutton = digitalRead(6); int greenbutton = digitalRead(7); Serial.println(yellowbutton); for (pos = 0; pos = 0; pos -= 1) { myservo.write(pos); delay(3); } }
Behaviour:
The buttons work as the keyboard, the user can play whatever they want but since there are only three buttons the melodies that can be played is limited. Depending on which button you press, the LEDs light up according to their respective colour. The servo motor works as the ‘beat’, so the user can try to play something matching to the beat or can use that beat as the bass for their song.
Problems:
- The first problem we had was whenever we pressed the button, the light would turn off and whenever we released the button the light turn on. Finally, we figured the problem, we accidentally coded LOW, instead of HIGH. But realizing this took some time.
- The second major problem we had was the note for the third button. First two notes will play according to the note that we coded but whatever we did the third one kept on making a different sound. In the end, we kept on trying different notes and finally, an octave higher notes worked perfectly.
- The Third problem we are facing is the delay. When we press the button there is a delay while lighting the LED up and producing the sound. We still haven’t managed to fix the delay.
Well done on your project.
Your schematic has an error: you have the inputs from the switches connected to 5V, so if you had done it this way your digitalRead() would always return HIGH. I know you didn’t make this mistake in your project, so it’s just the schematic, but it’s important that you understand this.
It looks like your code was cut off somehow, probably by a failure in WordPress. You need to document your code better: explain what you are doing and use variables with meaningful names. This will be especially important as you start to work with Processing and develop more complex programs.