-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuzzer.cpp
59 lines (54 loc) · 1.34 KB
/
Buzzer.cpp
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
#include "Buzzer.h"
#include "Arduino.h"
#include "Structure.h"
Buzzer::Buzzer(int pin) {
this->pin = pin;
pinMode(pin, OUTPUT);
}
void Buzzer::play(int melody) {
if (currentMelody != melody) {
updateMelody(melody);
}
if (nextToneAt < millis()) {
if (pgm_read_word_near(melodyTones + tonePointer) != 0) {
tone(pin, pgm_read_word_near(melodyTones + tonePointer), pgm_read_word_near(melodyDelays + tonePointer) );
} else {
noTone(pin);
}
nextToneAt = millis() + pgm_read_word_near(melodyDelays + tonePointer);
tonePointer = (tonePointer + 1) % toneCount;
}
}
void Buzzer::stop() {
currentMelody = 0;
int tonePointer = 0;
noTone(pin);
}
void Buzzer::updateMelody(int melody) {
currentMelody = melody;
int tonePointer = 0;
noTone(pin);
nextToneAt = millis();
switch (melody) {
case 1:
toneCount = MEL_1_COUNT;
melodyTones = MEL_1_TONE_ARR;
melodyDelays = MEL_1_DELAY_ARR;
break;
case 2:
toneCount = MEL_2_COUNT;
melodyTones = MEL_2_TONE_ARR;
melodyDelays = MEL_2_DELAY_ARR;
break;
case 3:
toneCount = MEL_3_COUNT;
melodyTones = MEL_3_TONE_ARR;
melodyDelays = MEL_3_DELAY_ARR;
break;
case 4:
toneCount = MEL_4_COUNT;
melodyTones = MEL_4_TONE_ARR;
melodyDelays = MEL_4_DELAY_ARR;
break;
}
}