use an accelerometer to generate sounds
Dependencies: FXOS8700CQ mbed
main.cpp@2:cc285ff20aac, 2016-05-12 (annotated)
- Committer:
- maclobdell
- Date:
- Thu May 12 17:34:47 2016 +0000
- Revision:
- 2:cc285ff20aac
- Parent:
- 1:c3cb527a2aa6
fixed play tone function to not set frequency as the period
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
maclobdell | 0:c482a7d7d833 | 1 | #include "mbed.h" // this tells us to load mbed related functions |
maclobdell | 0:c482a7d7d833 | 2 | #include "tones.h" // list of all the tones and their frequencies |
maclobdell | 1:c3cb527a2aa6 | 3 | #include "FXOS8700CQ.h" // library for the accelerometer |
maclobdell | 0:c482a7d7d833 | 4 | |
maclobdell | 0:c482a7d7d833 | 5 | InterruptIn btn2(SW2); // we create a variable 'btn2', use it as an in port |
maclobdell | 0:c482a7d7d833 | 6 | InterruptIn btn3(SW3); // we create a variable 'btn3', use it as an in port |
maclobdell | 0:c482a7d7d833 | 7 | |
maclobdell | 0:c482a7d7d833 | 8 | PwmOut buzzer(D3); // our buzzer is a PWM output (pulse-width modulation) |
maclobdell | 0:c482a7d7d833 | 9 | |
maclobdell | 1:c3cb527a2aa6 | 10 | Timeout tone_timeout; //TimeOut = a interrupt to call a function after a specified delay |
maclobdell | 0:c482a7d7d833 | 11 | |
maclobdell | 0:c482a7d7d833 | 12 | // Set up the accelerometer (this is specific to the onboard one) |
maclobdell | 0:c482a7d7d833 | 13 | InterruptIn accel_interrupt_pin(PTC13); |
maclobdell | 0:c482a7d7d833 | 14 | FXOS8700CQ accel(PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); |
maclobdell | 0:c482a7d7d833 | 15 | |
maclobdell | 0:c482a7d7d833 | 16 | static void silence() { |
maclobdell | 0:c482a7d7d833 | 17 | buzzer.write(0.0f); // silence! |
maclobdell | 0:c482a7d7d833 | 18 | } |
maclobdell | 0:c482a7d7d833 | 19 | |
maclobdell | 0:c482a7d7d833 | 20 | // this is our function that plays a tone. |
maclobdell | 0:c482a7d7d833 | 21 | // Takes in a tone frequency, and after duration (in ms.) we stop playing again |
maclobdell | 0:c482a7d7d833 | 22 | static void play_tone(int tone) { |
maclobdell | 2:cc285ff20aac | 23 | buzzer.period_us(1000000/(tone)); |
maclobdell | 0:c482a7d7d833 | 24 | buzzer.write(0.10f); // 10% duty cycle, otherwise it's too loud |
maclobdell | 0:c482a7d7d833 | 25 | } |
maclobdell | 0:c482a7d7d833 | 26 | |
maclobdell | 0:c482a7d7d833 | 27 | static void play_note1() { |
maclobdell | 0:c482a7d7d833 | 28 | play_tone(NOTE_C4); |
maclobdell | 0:c482a7d7d833 | 29 | } |
maclobdell | 0:c482a7d7d833 | 30 | static void play_note2() { |
maclobdell | 0:c482a7d7d833 | 31 | play_tone(NOTE_D4); |
maclobdell | 0:c482a7d7d833 | 32 | } |
maclobdell | 0:c482a7d7d833 | 33 | |
maclobdell | 1:c3cb527a2aa6 | 34 | // YOUR CODE HERE |
maclobdell | 0:c482a7d7d833 | 35 | |
maclobdell | 0:c482a7d7d833 | 36 | // this code runs when the microcontroller starts up |
maclobdell | 0:c482a7d7d833 | 37 | int main() { |
maclobdell | 0:c482a7d7d833 | 38 | // play note when we fall |
maclobdell | 0:c482a7d7d833 | 39 | btn2.fall(play_note1); |
maclobdell | 0:c482a7d7d833 | 40 | btn3.fall(play_note2); |
maclobdell | 0:c482a7d7d833 | 41 | |
maclobdell | 0:c482a7d7d833 | 42 | // silence when we rise |
maclobdell | 0:c482a7d7d833 | 43 | btn2.rise(silence); |
maclobdell | 0:c482a7d7d833 | 44 | btn3.rise(silence); |
maclobdell | 0:c482a7d7d833 | 45 | |
maclobdell | 0:c482a7d7d833 | 46 | // accelerometer setup |
maclobdell | 0:c482a7d7d833 | 47 | accel_interrupt_pin.fall(play_note3); |
maclobdell | 0:c482a7d7d833 | 48 | accel_interrupt_pin.mode(PullUp); |
maclobdell | 0:c482a7d7d833 | 49 | |
maclobdell | 0:c482a7d7d833 | 50 | accel.config_int(); // enabled interrupts from accelerometer |
maclobdell | 0:c482a7d7d833 | 51 | accel.config_feature(); // turn on motion detection |
maclobdell | 0:c482a7d7d833 | 52 | accel.enable(); // enable accelerometer |
maclobdell | 0:c482a7d7d833 | 53 | |
maclobdell | 0:c482a7d7d833 | 54 | } |