Plays the happy birthday song music on the speaker.
Dependencies: DebounceInterrupts mbed
Revision 0:33ad8330e62d, committed 2014-06-10
- Comitter:
- bhakti08
- Date:
- Tue Jun 10 21:53:29 2014 +0000
- Commit message:
- Happy Birthday Song using mbed;
Changed in this revision
diff -r 000000000000 -r 33ad8330e62d DebounceInterrupts.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DebounceInterrupts.lib Tue Jun 10 21:53:29 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/bhakti08/code/DebounceInterrupts/#96a51b236ba0
diff -r 000000000000 -r 33ad8330e62d DebouncedIn.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DebouncedIn.cpp Tue Jun 10 21:53:29 2014 +0000 @@ -0,0 +1,93 @@ +#include "DebouncedIn.h" +#include "mbed.h" + +/* + * Constructor + */ +DebouncedIn::DebouncedIn(PinName in) + : _in(in) { + + // reset all the flags and counters + _samples = 0; + _output = 0; + _output_last = 0; + _rising_flag = 0; + _falling_flag = 0; + _state_counter = 0; + + // Attach ticker + _ticker.attach(this, &DebouncedIn::_sample, 0.005); +} + +void DebouncedIn::_sample() { + + // take a sample + _samples = _samples >> 1; // shift left + + if (_in) { + _samples |= 0x80; + } + + // examine the sample window, look for steady state + if (_samples == 0x00) { + _output = 0; + } + else if (_samples == 0xFF) { + _output = 1; + } + + + // Rising edge detection + if ((_output == 1) && (_output_last == 0)) { + _rising_flag++; + _state_counter = 0; + } + + // Falling edge detection + else if ((_output == 0) && (_output_last == 1)) { + _falling_flag++; + _state_counter = 0; + } + + // steady state + else { + _state_counter++; + } + + // update the output + _output_last = _output; + +} + + + +// return number of rising edges +int DebouncedIn::rising(void) { + int return_value = _rising_flag; + _rising_flag = 0; + return(return_value); +} + +// return number of falling edges +int DebouncedIn::falling(void) { + int return_value = _falling_flag; + _falling_flag = 0; + return(return_value); +} + +// return number of ticsk we've bene steady for +int DebouncedIn::steady(void) { +return(_state_counter); +} + +// return the debounced status +int DebouncedIn::read(void) { + return(_output); +} + +// shorthand for read() +DebouncedIn::operator int() { + return read(); +} + +
diff -r 000000000000 -r 33ad8330e62d DebouncedIn.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DebouncedIn.h Tue Jun 10 21:53:29 2014 +0000 @@ -0,0 +1,31 @@ +#include "mbed.h" + + class DebouncedIn { + public: + DebouncedIn(PinName in); + + int read (void); + operator int(); + + int rising(void); + int falling(void); + int steady(void); + + private : + // objects + DigitalIn _in; + Ticker _ticker; + + // function to take a sample, and update flags + void _sample(void); + + // counters and flags + int _samples; + int _output; + int _output_last; + int _rising_flag; + int _falling_flag; + int _state_counter; + + }; + \ No newline at end of file
diff -r 000000000000 -r 33ad8330e62d main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Jun 10 21:53:29 2014 +0000 @@ -0,0 +1,47 @@ +/****************************************************************************************/ +/* IO DEVICES ASSIGNMENT 6 */ +/*This program uses the PWM output to generate a "HAPPY BIRTHDAY" Tone on the speaker */ +/****************************************************************************************/ + +#include "mbed.h" + +// ms +#define A4 440 +#define B4b 446 +#define C4 261 +#define C4_1 130 +#define C5 523 +#define D4 293 +#define D4b 277 +#define E4 329 +#define F4 349 +#define G4 392 + +PwmOut buzzer(p26); + + +float notes[] = {C4_1,C4,D4,C4,F4,E4,C4_1,C4,D4,C4,G4,F4,C4_1,C4,C5,A4,F4,E4,D4,B4b,B4b,A4,F4,G4,F4}; + + +float interval[] = {4, 4, 8, 8, 8, 10, 4, 4, 8, 8, 8, 10, 4, 4, 8, 8, 8, 8, 8, 4, 4, 8, 8, 8, 12}; + +int main() { + while (1) { + for (int i=0;i<=24;i++) { + buzzer.period((1/notes[i])); // set PWM period + buzzer=0.5; // set duty cycle + wait(0.8*interval[i]/10); // hold for beat period + } + buzzer = 0; + wait(5); + } +} + + +/****************************************************************************************/ +/* TESTING */ +/****************************************************************************************/ + +/*1. Power up the mbed, load the program and press the reset button. The tone should play*/ +/* as long as the mbed is powered up. PASS*/ +/*****************************************************************************************/ \ No newline at end of file
diff -r 000000000000 -r 33ad8330e62d mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue Jun 10 21:53:29 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/0b3ab51c8877 \ No newline at end of file