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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
| #include "pitches.h"
// Firstly: Thanks @ Arduino toneMelody() tutorial
/*************************************************
* Songs and Tempos
*************************************************/
// Left non-const because I want to try changing these via button in the future.
// Song tempos may not be totally accurate because I made these by ear.
int zeldasSong[] = {
NOTE_E4, NOTE_G4, NOTE_D4, NOTE_C4, NOTE_D4, NOTE_E4, NOTE_G4, NOTE_D4
};
int sariasSong[] = {
NOTE_F4, NOTE_A4, NOTE_B4, NOTE_F4, NOTE_A4, NOTE_B4,
NOTE_F4, NOTE_A4, NOTE_B4, NOTE_E5, NOTE_D5
};
int eponasSong[] {
NOTE_D5, NOTE_B4, NOTE_A4, R, NOTE_D5, NOTE_B4, NOTE_A4, R,
NOTE_D5, NOTE_B4, NOTE_A4, NOTE_B4, NOTE_A4
};
int marioSong[] = {
NOTE_E4, NOTE_E4, NOTE_E4,
NOTE_C4, NOTE_E4, NOTE_G4, R, NOTE_G3
};
int zeldaTempo[] = {
2,4,2,8,8,2,4,2
};
int sariaTempo[] = {
8,8,4,8,8,4,8,8,8,8,4
};
int eponaTempo[] {
3,5,1,12,3,5,1,12,3,5,2,2,1
};
int marioTempo[] = {
8,4,4,8,4,3,6,3
};
/*************************************************
* Pins
*************************************************/
// These are a bit more organic as that's how I've best memorized variables in past.
// Speaker
const int speakerBoy = 3;
// LEDs
const int blueDude = 5;
const int greenDude = 6;
const int redDude = 7;
// Buttons
const int blueHome = 8;
const int greenHome = 9;
const int redHome = 10;
int redToggle = 0;
/*************************************************
* Setup
*************************************************/
void setup() {
// setting up pins
pinMode(speakerBoy, OUTPUT);
pinMode(blueDude, OUTPUT);
pinMode(redDude, OUTPUT);
pinMode(greenDude, OUTPUT);
pinMode(blueHome,INPUT);
pinMode(greenHome,INPUT);
pinMode(redHome,INPUT);
// this is just for aesthetics
delay(500);
digitalWrite(greenDude, HIGH);
delay(500);
digitalWrite(redDude, HIGH);
delay(500);
digitalWrite(blueDude, LOW);
digitalWrite(greenDude, LOW);
digitalWrite(redDude, LOW);
// Serial.begin(9600);
}
/*************************************************
* Loop
*************************************************/
void loop() {
// assign variables based on button state
int blueState = digitalRead(blueHome);
int greenState = digitalRead(greenHome);
int redState = digitalRead(redHome);
// FIRST: first button, first song, blue LED
// If button is pressed, light LED and play song
// Otherwise, turn off LED when song ends
if (blueState == HIGH) {
digitalWrite(blueDude, HIGH);
// first melody
// run through lullaby while changing note and duration
for (int thisNote = 0; thisNote < 8; thisNote++) {
int noteDuration = 1000/zeldaTempo[thisNote];
tone(speakerBoy, zeldasSong[thisNote], noteDuration);
int notePause = noteDuration * 1.30;
delay(notePause);
}
} else {
digitalWrite(blueDude, LOW);
}
// SECOND: second button, second song, green LED
if (greenState == HIGH) {
digitalWrite(greenDude, HIGH);
// second melody
for (int thisNote = 0; thisNote < 11; thisNote++) {
int noteDuration = 1000/sariaTempo[thisNote];
tone(speakerBoy, sariasSong[thisNote], noteDuration);
int notePause = noteDuration * 1.30;
delay(notePause);
}
} else {
digitalWrite(greenDude, LOW);
}
// THIRD: Third button, last two songs, mix of LEDs :^)
// Uses a toggle based on 'red' button state for a bit more complexity
// If button is pressed, first check toggle to decide what LEDs and song to run
if (redState == HIGH) {
if (redToggle == 0) {
digitalWrite(redDude, HIGH);
redToggle++;
// third melody
for (int thisNote = 0; thisNote < 13; thisNote++) {
int noteDuration = 1000/eponaTempo[thisNote];
tone(speakerBoy, eponasSong[thisNote], noteDuration);
int notePause = noteDuration * 1.30;
delay(notePause);
}
} else {
// third alt melody
digitalWrite(redDude, HIGH);
digitalWrite(blueDude, HIGH);
redToggle = 0;
for (int thisNote = 0; thisNote < 8; thisNote++) {
int noteDuration = 1000/marioTempo[thisNote];
tone(speakerBoy, marioSong[thisNote], noteDuration);
int notePause = noteDuration * 1.30;
delay(notePause);
}
}
} else {
digitalWrite(redDude, LOW);
digitalWrite(blueDude, LOW);
}
}
|