for testing

Dependencies:   FXOS8700CQ mbed

Fork of 4_accelerometer by MakingMusicWorkshop

Committer:
maclobski
Date:
Tue May 10 03:32:46 2016 +0000
Revision:
1:01baa9666217
Parent:
0:c482a7d7d833
for testing

Who changed what in which revision?

UserRevisionLine numberNew 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
maclobski 1:01baa9666217 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
maclobski 1:01baa9666217 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 0:c482a7d7d833 23 buzzer.period_us(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
maclobski 1:01baa9666217 34 // YOUR CODE HERE
maclobski 1:01baa9666217 35 //REMOVE
maclobski 1:01baa9666217 36 static void play_note3() {
maclobski 1:01baa9666217 37 play_tone(NOTE_E4);
maclobski 1:01baa9666217 38
maclobski 1:01baa9666217 39 tone_timeout.attach(&silence, 0.2); // setup tone_timeout to call silence after 200 duration ms
maclobski 1:01baa9666217 40 }
maclobski 1:01baa9666217 41 //END REMOVE
maclobdell 0:c482a7d7d833 42 // this code runs when the microcontroller starts up
maclobdell 0:c482a7d7d833 43 int main() {
maclobdell 0:c482a7d7d833 44 // play note when we fall
maclobdell 0:c482a7d7d833 45 btn2.fall(play_note1);
maclobdell 0:c482a7d7d833 46 btn3.fall(play_note2);
maclobdell 0:c482a7d7d833 47
maclobdell 0:c482a7d7d833 48 // silence when we rise
maclobdell 0:c482a7d7d833 49 btn2.rise(silence);
maclobdell 0:c482a7d7d833 50 btn3.rise(silence);
maclobdell 0:c482a7d7d833 51
maclobdell 0:c482a7d7d833 52 // accelerometer setup
maclobdell 0:c482a7d7d833 53 accel_interrupt_pin.fall(play_note3);
maclobdell 0:c482a7d7d833 54 accel_interrupt_pin.mode(PullUp);
maclobdell 0:c482a7d7d833 55
maclobdell 0:c482a7d7d833 56 accel.config_int(); // enabled interrupts from accelerometer
maclobdell 0:c482a7d7d833 57 accel.config_feature(); // turn on motion detection
maclobdell 0:c482a7d7d833 58 accel.enable(); // enable accelerometer
maclobdell 0:c482a7d7d833 59
maclobdell 0:c482a7d7d833 60 }