-
Notifications
You must be signed in to change notification settings - Fork 1
/
sketch_melody_player.ino
159 lines (138 loc) · 3.75 KB
/
sketch_melody_player.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Define the pins
#include "pitches.h"
int speakerPin = 11;
int BlueLED = 10;
int RedLED = 5;
int GreenLED = 9;
int YellowLED = 3;
const int buttonPin = 1;
const int buttonPin2 = 4;
const int buttonPin3 = 8;
const int buttonPin4 = 12;
const int buttonPin5 = 13;
// Define the melody
int melody[] = {
// Super Mario Bros theme
NOTE_E7, NOTE_E7, 0, NOTE_E7,
0, NOTE_C7, NOTE_E7, 0,
NOTE_G7, 0, 0, 0,
NOTE_G6, 0, 0, 0,
NOTE_C7, 0, 0, NOTE_G6,
0, 0, NOTE_E6, 0,
0, NOTE_A6, 0, NOTE_B6,
0, NOTE_AS6, NOTE_A6, 0,
NOTE_G6, NOTE_E7, NOTE_G7,
NOTE_A7, 0, NOTE_F7, NOTE_G7,
0, NOTE_E7, 0, NOTE_C7,
NOTE_D7, NOTE_B6, 0, 0,
NOTE_C7, 0, 0, NOTE_G6,
0, 0, NOTE_E6, 0,
0, NOTE_A6, 0, NOTE_B6,
0, NOTE_AS6, NOTE_A6, 0,
NOTE_G6, NOTE_E7, NOTE_G7,
NOTE_A7, 0, NOTE_F7, NOTE_G7,
0, NOTE_E7, 0, NOTE_C7,
NOTE_D7, NOTE_B6, 0, 0
};
// Define the note durations
int noteDuration[] = {
// Super Mario Bros theme
120, 120, 120, 120,
120, 120, 120, 120,
120, 120, 120, 120,
120, 120, 120, 120,
120, 120, 120, 120,
120, 120, 120, 120,
120, 120, 120, 120,
120, 120, 120, 120,
120, 120, 120,
120, 120, 120, 120,
120, 120, 120, 120,
120, 120, 120, 120,
120, 120, 120, 120,
120, 120, 120, 120,
120, 120, 120, 120,
120, 120, 120, 120,
120, 120, 120,
120, 120, 120, 120,
120, 120, 120, 120,
120, 120, 120, 120
};
void setup() {
// Set the speaker pin as an output
pinMode(speakerPin, OUTPUT);
pinMode(BlueLED, OUTPUT);
pinMode(RedLED, OUTPUT);
pinMode(GreenLED, OUTPUT);
pinMode(YellowLED, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
pinMode(buttonPin4, INPUT_PULLUP);
pinMode(buttonPin5, INPUT_PULLUP);
}
void loop() {
// Loop through the melody and play each note
if (digitalRead(buttonPin)== LOW){
tone(speakerPin, NOTE_E7, 10); // Play the note on the piezo
digitalWrite(YellowLED, HIGH);
}
else{
digitalWrite(YellowLED, LOW);
}
if (digitalRead(buttonPin2)== LOW){
tone(speakerPin, NOTE_C7, 10); // Play the note on the piezo
digitalWrite(RedLED, HIGH);
}
else{
digitalWrite(RedLED, LOW);
}
if (digitalRead(buttonPin3)== LOW){
tone(speakerPin, NOTE_G7, 10); // Play the note on the piezo
digitalWrite(GreenLED, HIGH);
}
else{
digitalWrite(GreenLED, LOW);
}
if (digitalRead(buttonPin4)== LOW){
tone(speakerPin, NOTE_G6, 10); // Play the note on the piezo
digitalWrite(BlueLED, HIGH);
}
else{
digitalWrite(BlueLED, LOW);
}
if (digitalRead(buttonPin5)== LOW){
for (int i = 0; i < sizeof(melody) / sizeof(int); i++) {
if (melody[i] != 0) {
tone(speakerPin, melody[i], noteDuration[i]); // Play the note on the piezo
// Turn on the corresponding LED
switch (i % 4) {
case 0:
digitalWrite(BlueLED, HIGH);
delay(noteDuration[i]);
digitalWrite(BlueLED, LOW);
break;
case 1:
digitalWrite(RedLED, HIGH);
delay(noteDuration[i]);
digitalWrite(RedLED, LOW);
break;
case 2:
digitalWrite(GreenLED, HIGH);
delay(noteDuration[i]);
digitalWrite(GreenLED, LOW);
break;
case 3:
digitalWrite(YellowLED, HIGH);
delay(noteDuration[i]);
digitalWrite(YellowLED, LOW);
break;
}
}
else {
// Add a small delay between notes for a smoother sound
delay(noteDuration[i] * 1.3);
}
}
}
}