forked from tardate/LittleArduinoProjects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElectretTrigger.ino
48 lines (33 loc) · 1 KB
/
ElectretTrigger.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
/*
ElectretTrigger
Triggers a tone sequence when mic input above predetermined threshold
For info and circuit diagrams see https://github.com/tardate/LittleArduinoProjects/tree/master/playground/ElectretTrigger
*/
#define MELODY_PIN 3
#include "melody.h"
const int preamp_pin = A0; // the pin we'll read preamp output value from
#define TRIGGER_THRESHOLD 250
void setup() {
pinMode(preamp_pin, INPUT); // enable preamp read
}
void loop() {
if(sample() < TRIGGER_THRESHOLD) {
playMelody();
}
delay(1);
}
int sample() {
return analogRead(preamp_pin);
}
void playMelody() {
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < noteCount; thisNote++) {
// calculate the note duration based on BPM
int noteDuration = 60000 * 4.0 / BPM / noteDurations[thisNote];
tone(MELODY_PIN, melody[thisNote], noteDuration);
// play with staccato ~ half beat
delay(noteDuration / 2);
noTone(MELODY_PIN);
delay(noteDuration / 2);
}
}