-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmas_soundboard.pde
160 lines (123 loc) · 4.14 KB
/
xmas_soundboard.pde
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
/*
Melody
Plays a melody
circuit:
* 8-ohm speaker on digital pin 8
created 21 Jan 2010
modified 14 Oct 2010
by Tom Igoe
This example code is in the public domain.
http://arduino.cc/en/Tutorial/Tone
*/
#include "pitches.h"
// Silent night
/* G A G E x 2
DDB CCG
*/
// Tannenbaum
int melody[] = {NOTE_C3, NOTE_F3, NOTE_F3, NOTE_F3, NOTE_G3, NOTE_A3, NOTE_A3, NOTE_A3,
NOTE_A3, NOTE_G3, NOTE_A3, NOTE_AS3, NOTE_E3, NOTE_G3, NOTE_F3};
// Stille Nacht
// int melody[] = {NOTE_G3, NOTE_A3, NOTE_G3, NOTE_E3, NOTE_G3, NOTE_A3, NOTE_G3, NOTE_E3,
// NOTE_D4, NOTE_D4, NOTE_D4, NOTE_B3, NOTE_C4, NOTE_C4, NOTE_C4, NOTE_G3};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
2, 2, 4, 2, 2, 2, 4, 2,
4, 4, 4, 1, 2, 2, 2 };
// Stille Nacht
/* 1.385, 4, 2, 1.2,
1.385, 4, 2, 1.2,
2, 1, 2, 1,
2, 1, 2, 1 }; */
int buttonState = 0;
int sensorPin = A0;
int sensorValue = 0;
void setup() {
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(5, INPUT);
Serial.begin(9600);
}
void loop() {
// no need to repeat the melody.
sensorValue = analogRead(sensorPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (sensorValue < 100) {
// turn LED on:
playSong();
}
}
void playSong() {
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 4; thisNote++) {
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000/noteDurations[thisNote];
tone(8, melody[thisNote],noteDuration);
digitalWrite(7, HIGH);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes / 2);
// stop the tone playing:
noTone(8);
digitalWrite(7, LOW);
delay(pauseBetweenNotes / 2);
}
delay(500);
// iterate over the notes of the melody:
for (int thisNote = 4; thisNote < 8; thisNote++) {
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000/noteDurations[thisNote];
tone(8, melody[thisNote],noteDuration);
digitalWrite(6, HIGH);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes / 2);
// stop the tone playing:
noTone(8);
digitalWrite(6, LOW);
delay(pauseBetweenNotes / 2);
}
delay(500);
// iterate over the notes of the melody:
for (int thisNote = 8; thisNote < 12; thisNote++) {
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000/noteDurations[thisNote];
tone(8, melody[thisNote],noteDuration);
digitalWrite(7, HIGH);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes / 2);
// stop the tone playing:
noTone(8);
digitalWrite(7, LOW);
delay(pauseBetweenNotes / 2);
}
// delay(0);
// iterate over the notes of the melody:
for (int thisNote = 12; thisNote < 15; thisNote++) {
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000/noteDurations[thisNote];
tone(8, melody[thisNote],noteDuration);
digitalWrite(6, HIGH);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes / 2);
// stop the tone playing:
noTone(8);
digitalWrite(6, LOW);
delay(pauseBetweenNotes / 2);
}
}