Concept:
The concept is to do a Christmas themed plant because it is almost the season! I originally wanted to it with a tiny Christmas tree but I couldn’t find one so I made it with an ordinary plant. Basically, when you press the button it plays the Christmas cheer and the lights blink according to the rhythm of the song and when the button is not pressed the light keeps on blinking in their default tempo.
Behavior:
The lights blink in their own tempo when nothing is touched. When you press on the button the lights start blinking in a different tempo and music plays. When the music stops, LEDs go back to blinking in their original rhythm.
Project:
Code:
int speakerPin = 8; int length = 26; char notes[] = "eeeeeeegcdefffgfeeeeddedg"; int beats[] = { 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2 }; int tempo = 250; void playTone(int tone, int duration) { for (long i = 0; i < duration * 1000L; i += tone * 2) { digitalWrite(speakerPin, HIGH); digitalWrite(3, HIGH); digitalWrite(4, HIGH); digitalWrite(5, HIGH); digitalWrite(6, HIGH); digitalWrite(7, HIGH); digitalWrite(9, HIGH); delayMicroseconds(tone); digitalWrite(speakerPin, LOW); digitalWrite(3, LOW); digitalWrite(4, LOW); digitalWrite(5, LOW); digitalWrite(6, LOW); digitalWrite(7, LOW); digitalWrite(9, LOW); delayMicroseconds(tone); } } void playNote(char note, int duration) { char names[] = { 'c', 'd', 'e', 'f', 's', 'g', 'a', 'v', 'b', 'C', 'D', 'E' }; int tones[] = { 1915, 1700, 1519, 1432, 1352, 1275, 1136, 1073, 1014, 956, 852, 758 }; // play the tone corresponding to the note name for (int i = 0; i < 8; i++) { if (names[i] == note) { playTone(tones[i], duration); } } } void setup() { pinMode(speakerPin, OUTPUT); pinMode(3, OUTPUT); pinMode(4, OUTPUT); pinMode(5, OUTPUT); pinMode(6, OUTPUT); pinMode(7, OUTPUT); pinMode(9, OUTPUT); pinMode(10, INPUT_PULLUP); } void loop() { int sensorVal = digitalRead(10); Serial.println(sensorVal); if (sensorVal == LOW) { for (int i = 0; i < length; i++) { if (notes[i] == ' ') { delay(beats[i] * tempo); // rest } else { playNote(notes[i], beats[i] * tempo); } // pause between notes delay(tempo / 2); } } else { digitalWrite(13, HIGH); } digitalWrite(3, HIGH); delay(200); digitalWrite(4, HIGH); delay(200); digitalWrite(5, HIGH); delay(200); digitalWrite(6, HIGH); delay(200); digitalWrite(7, HIGH); delay(200); digitalWrite(9, HIGH); delay(200); digitalWrite(9, LOW); delay(200); digitalWrite(7, LOW); delay(200); digitalWrite(6, LOW); delay(200); digitalWrite(5, LOW); delay(200); digitalWrite(4, LOW); delay(200); digitalWrite(3, LOW); delay(200); }
Schematic:
Problems:
I had some problems when I was coding. I couldn’t get the lights to blink differently when nothing happens vs. button is pressed. I got help from a friend and he fixed the problem by fixing if and else statements.
I wanted to use an SD card to play the music essentially but it couldn’t download the library to my computer so I had to change plans last minute. I ended up getting the music notes online because I don’t know how to play the song with notes.
Another silly mistake I did was I placed the buzzer’s legs on the same row instead of the same column so I couldn’t get it to work for a day. Then, figured it out.
Soldering part was not easy, I felt like the job I did was not pretty. I used to much solder to keep its place. In the end, it worked but I think a prettier job could have been made with more practice.
Lessons Learned:
I should have used the different button and not the button from the Arduino kit. Also, before putting it into the box I had everything ready and planned so I didn’t have any problems or difficulties while wiring it. But, if I had to change anything it would have been hard so next time I do a project, I’m going to try design something more easily manipulated.
Nice project. I’m glad that you learned how to connect two LEDs to one pin. That gave you a more festive appearance but still kept the number of pins under control. This is a great observation: “design something more easily manipulated” that I think everyone should follow! I see that you figured out how to use functions (playNote() and playTone()) which we have not yet covered in class. Good for you! Your code though needs comments to explain what is going on.