forked from drinks-wallet/DrinksRfidTerminal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sound.cpp
executable file
·49 lines (36 loc) · 999 Bytes
/
Sound.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
/*
* "Drinks" RFID Terminal
* Buy sodas with your company badge!
*
* Benoit Blanchon 2014 - MIT License
* https://github.com/bblanchon/DrinksRfidTerminal
*/
#include <Arduino.h>
#include "Pins.h"
#include "Sound.h"
void Sound::begin()
{
pinMode(PIN_BUZZER, OUTPUT);
}
void Sound::play(const char* melody)
{
int frequencies [] = { 4186, 4699, 4699, 5274, 6271, 7040, 7902 };
while (true)
{
byte note = *melody;
if (note >= 'a' && note <= 'g')
tone(PIN_BUZZER, frequencies[note - 'a']);
if (note >= 'A' && note <= 'G')
tone(PIN_BUZZER, frequencies[note - 'A'] * 2);
melody++;
if (*melody == 0) break;
byte duration = *melody - '0';
if (duration<0 || duration>9) break;
duration = 1 << (duration - 1);
delay(75 * duration);
noTone(PIN_BUZZER);
melody++;
if (*melody == 0) break;
delay(10);
}
}