-
Notifications
You must be signed in to change notification settings - Fork 4
/
buzzer.cpp
59 lines (50 loc) · 1.03 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"
Buzzer::Buzzer(uint8_t pin): pin(pin) {
pinMode(pin, OUTPUT);
}
void Buzzer::beep(const char* signal) const {
char c;
while(c = *(signal++)){
switch(c){
case '.':
digitalWrite(pin, 1);
delay(30);
digitalWrite(pin, 0);
break;
case '-':
digitalWrite(pin, 1);
delay(150);
digitalWrite(pin, 0);
break;
default:
delay(100);
break;
}
if(*signal != 0)
delay(100);
}
}
void Buzzer::beepNumber(int number) const {
for(int i = pow(10, (int)log10(number)); i > 0; i /= 10){
int digit = number / i;
number -= digit * i;
switch(digit){
case 0: beep("--"); break;
case 1: beep("."); break;
case 2: beep(".."); break;
case 3: beep("..."); break;
case 4: beep(".-"); break;
case 5: beep("-"); break;
case 6: beep("-."); break;
case 7: beep("-.."); break;
case 8: beep("-..."); break;
case 9: beep(".--"); break;
default: beep("----------");
}
if(i > 1)
delay(300);
}
}
void Buzzer::set(bool on) const {
digitalWrite(pin, on);
}