![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
4 songs: rain, william, sun, and circus
Buzzer.cpp@0:c02f01feda7b, 2015-05-09 (annotated)
- Committer:
- Luminoscity
- Date:
- Sat May 09 21:29:00 2015 +0000
- Revision:
- 0:c02f01feda7b
Successful Music Test Code!!!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Luminoscity | 0:c02f01feda7b | 1 | #include "Buzzer.h" |
Luminoscity | 0:c02f01feda7b | 2 | |
Luminoscity | 0:c02f01feda7b | 3 | |
Luminoscity | 0:c02f01feda7b | 4 | /* |
Luminoscity | 0:c02f01feda7b | 5 | Buzzer |
Luminoscity | 0:c02f01feda7b | 6 | The example use a buzzer to play melodies. It sends a square wave of the |
Luminoscity | 0:c02f01feda7b | 7 | appropriate frequency to the buzzer, generating the corresponding tone. |
Luminoscity | 0:c02f01feda7b | 8 | |
Luminoscity | 0:c02f01feda7b | 9 | The circuit: |
Luminoscity | 0:c02f01feda7b | 10 | * Buzzer attached to pin39 (J14 plug on Grove Base BoosterPack) |
Luminoscity | 0:c02f01feda7b | 11 | * one side pin (either one) to ground |
Luminoscity | 0:c02f01feda7b | 12 | * the other side pin to VCC |
Luminoscity | 0:c02f01feda7b | 13 | * LED anode (long leg) attached to RED_LED |
Luminoscity | 0:c02f01feda7b | 14 | * LED cathode (short leg) attached to ground |
Luminoscity | 0:c02f01feda7b | 15 | |
Luminoscity | 0:c02f01feda7b | 16 | * Note: |
Luminoscity | 0:c02f01feda7b | 17 | |
Luminoscity | 0:c02f01feda7b | 18 | |
Luminoscity | 0:c02f01feda7b | 19 | This example code is in the public domain. |
Luminoscity | 0:c02f01feda7b | 20 | |
Luminoscity | 0:c02f01feda7b | 21 | http://www.seeedstudio.com/wiki/index.php?title=GROVE_-_Starter_Kit_v1.1b#Grove_-_Buzzer |
Luminoscity | 0:c02f01feda7b | 22 | |
Luminoscity | 0:c02f01feda7b | 23 | */ |
Luminoscity | 0:c02f01feda7b | 24 | static DigitalOut Buzzer(BUZZER_PIN); |
Luminoscity | 0:c02f01feda7b | 25 | |
Luminoscity | 0:c02f01feda7b | 26 | /* play tone */ |
Luminoscity | 0:c02f01feda7b | 27 | void playTone(int tone, int duration) { |
Luminoscity | 0:c02f01feda7b | 28 | for (long i = 0; i < duration * 1000L; i += tone * 2) { |
Luminoscity | 0:c02f01feda7b | 29 | Buzzer = 1; |
Luminoscity | 0:c02f01feda7b | 30 | wait_us(tone); |
Luminoscity | 0:c02f01feda7b | 31 | Buzzer = 0; |
Luminoscity | 0:c02f01feda7b | 32 | wait_us(tone); |
Luminoscity | 0:c02f01feda7b | 33 | } |
Luminoscity | 0:c02f01feda7b | 34 | } |
Luminoscity | 0:c02f01feda7b | 35 | |
Luminoscity | 0:c02f01feda7b | 36 | void playNote(char note, int range, int duration) { |
Luminoscity | 0:c02f01feda7b | 37 | int cTones[] = { 3822, 1911, 956, 478}; |
Luminoscity | 0:c02f01feda7b | 38 | int CTones[] = { 3608, 1803, 902 }; |
Luminoscity | 0:c02f01feda7b | 39 | int dTones[] = { 3405, 1702, 851 }; |
Luminoscity | 0:c02f01feda7b | 40 | int DTones[] = { 3214, 1607, 803 }; |
Luminoscity | 0:c02f01feda7b | 41 | int eTones[] = { 3034, 1517, 758 }; |
Luminoscity | 0:c02f01feda7b | 42 | int fTones[] = { 2863, 1432, 716 }; |
Luminoscity | 0:c02f01feda7b | 43 | int FTones[] = { 2702, 1351, 675 }; |
Luminoscity | 0:c02f01feda7b | 44 | int gTones[] = { 2551, 1275, 638 }; |
Luminoscity | 0:c02f01feda7b | 45 | int GTones[] = { 2408, 1204, 602 }; |
Luminoscity | 0:c02f01feda7b | 46 | int aTones[] = { 2273, 1136, 568 }; |
Luminoscity | 0:c02f01feda7b | 47 | int ATones[] = { 2145, 1072, 536 }; |
Luminoscity | 0:c02f01feda7b | 48 | int bTones[] = { 2025, 1013, 506 }; |
Luminoscity | 0:c02f01feda7b | 49 | |
Luminoscity | 0:c02f01feda7b | 50 | // play the tone corresponding to the note name |
Luminoscity | 0:c02f01feda7b | 51 | switch (note) { |
Luminoscity | 0:c02f01feda7b | 52 | case 'a': |
Luminoscity | 0:c02f01feda7b | 53 | playTone(aTones[range - RANGE_OFFSET], duration); |
Luminoscity | 0:c02f01feda7b | 54 | break; |
Luminoscity | 0:c02f01feda7b | 55 | |
Luminoscity | 0:c02f01feda7b | 56 | case 'A': |
Luminoscity | 0:c02f01feda7b | 57 | playTone(ATones[range - RANGE_OFFSET], duration); |
Luminoscity | 0:c02f01feda7b | 58 | break; |
Luminoscity | 0:c02f01feda7b | 59 | |
Luminoscity | 0:c02f01feda7b | 60 | case 'b': |
Luminoscity | 0:c02f01feda7b | 61 | playTone(bTones[range - RANGE_OFFSET], duration); |
Luminoscity | 0:c02f01feda7b | 62 | break; |
Luminoscity | 0:c02f01feda7b | 63 | |
Luminoscity | 0:c02f01feda7b | 64 | case 'c': |
Luminoscity | 0:c02f01feda7b | 65 | playTone(cTones[range - RANGE_OFFSET], duration); |
Luminoscity | 0:c02f01feda7b | 66 | break; |
Luminoscity | 0:c02f01feda7b | 67 | |
Luminoscity | 0:c02f01feda7b | 68 | case 'C': |
Luminoscity | 0:c02f01feda7b | 69 | playTone(CTones[range - RANGE_OFFSET], duration); |
Luminoscity | 0:c02f01feda7b | 70 | break; |
Luminoscity | 0:c02f01feda7b | 71 | |
Luminoscity | 0:c02f01feda7b | 72 | case 'd': |
Luminoscity | 0:c02f01feda7b | 73 | playTone(dTones[range - RANGE_OFFSET], duration); |
Luminoscity | 0:c02f01feda7b | 74 | break; |
Luminoscity | 0:c02f01feda7b | 75 | |
Luminoscity | 0:c02f01feda7b | 76 | case 'D': |
Luminoscity | 0:c02f01feda7b | 77 | playTone(DTones[range - RANGE_OFFSET], duration); |
Luminoscity | 0:c02f01feda7b | 78 | break; |
Luminoscity | 0:c02f01feda7b | 79 | |
Luminoscity | 0:c02f01feda7b | 80 | case 'e': |
Luminoscity | 0:c02f01feda7b | 81 | playTone(eTones[range - RANGE_OFFSET], duration); |
Luminoscity | 0:c02f01feda7b | 82 | break; |
Luminoscity | 0:c02f01feda7b | 83 | |
Luminoscity | 0:c02f01feda7b | 84 | case 'f': |
Luminoscity | 0:c02f01feda7b | 85 | playTone(fTones[range - RANGE_OFFSET], duration); |
Luminoscity | 0:c02f01feda7b | 86 | break; |
Luminoscity | 0:c02f01feda7b | 87 | |
Luminoscity | 0:c02f01feda7b | 88 | case 'F': |
Luminoscity | 0:c02f01feda7b | 89 | playTone(FTones[range - RANGE_OFFSET], duration); |
Luminoscity | 0:c02f01feda7b | 90 | break; |
Luminoscity | 0:c02f01feda7b | 91 | |
Luminoscity | 0:c02f01feda7b | 92 | case 'g': |
Luminoscity | 0:c02f01feda7b | 93 | playTone(gTones[range - RANGE_OFFSET], duration); |
Luminoscity | 0:c02f01feda7b | 94 | break; |
Luminoscity | 0:c02f01feda7b | 95 | |
Luminoscity | 0:c02f01feda7b | 96 | case 'G': |
Luminoscity | 0:c02f01feda7b | 97 | playTone(GTones[range - RANGE_OFFSET], duration); |
Luminoscity | 0:c02f01feda7b | 98 | break; |
Luminoscity | 0:c02f01feda7b | 99 | |
Luminoscity | 0:c02f01feda7b | 100 | default: |
Luminoscity | 0:c02f01feda7b | 101 | playTone(cTones[0], duration); |
Luminoscity | 0:c02f01feda7b | 102 | } |
Luminoscity | 0:c02f01feda7b | 103 | } |