Mac Lobdell / Mbed 2 deprecated 4_accelerometer

Dependencies:   FXOS8700CQ mbed

Fork of 4_accelerometer by MakingMusicWorkshop

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"       // this tells us to load mbed related functions
00002 #include "tones.h"                   // list of all the tones and their frequencies
00003 #include "FXOS8700CQ.h"   // library for the accelerometer
00004 
00005 InterruptIn btn2(SW2);               // we create a variable 'btn2', use it as an in port
00006 InterruptIn btn3(SW3);               // we create a variable 'btn3', use it as an in port
00007 
00008 PwmOut buzzer(D3);                   // our buzzer is a PWM output (pulse-width modulation)
00009 
00010 Timeout tone_timeout;  //TimeOut = a interrupt to call a function after a specified delay
00011 
00012 // Set up the accelerometer (this is specific to the onboard one)
00013 InterruptIn accel_interrupt_pin(PTC13);
00014 FXOS8700CQ accel(PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1);
00015 
00016 static void silence() {
00017     buzzer.write(0.0f); // silence!
00018 }
00019 
00020 // this is our function that plays a tone. 
00021 // Takes in a tone frequency, and after duration (in ms.) we stop playing again
00022 static void play_tone(int tone) {
00023     buzzer.period_us(tone);
00024     buzzer.write(0.10f); // 10% duty cycle, otherwise it's too loud
00025 }
00026 
00027 static void play_note1() {
00028     play_tone(NOTE_C4);
00029 }
00030 static void play_note2() {
00031     play_tone(NOTE_D4);
00032 }
00033 
00034 // YOUR CODE HERE
00035 //REMOVE
00036 static void play_note3() {
00037     play_tone(NOTE_E4);
00038     
00039     tone_timeout.attach(&silence, 0.2); // setup tone_timeout to call silence after 200 duration ms
00040 }
00041 //END REMOVE
00042 // this code runs when the microcontroller starts up
00043 int main() {
00044     // play note when we fall
00045     btn2.fall(play_note1);
00046     btn3.fall(play_note2);
00047     
00048     // silence when we rise
00049     btn2.rise(silence);
00050     btn3.rise(silence);
00051     
00052     // accelerometer setup
00053     accel_interrupt_pin.fall(play_note3);
00054     accel_interrupt_pin.mode(PullUp);
00055     
00056     accel.config_int();      // enabled interrupts from accelerometer
00057     accel.config_feature();  // turn on motion detection
00058     accel.enable();          // enable accelerometer
00059 
00060 }