
for testing
Fork of 4_accelerometer by
main.cpp@0:c482a7d7d833, 2016-05-09 (annotated)
- Committer:
- maclobdell
- Date:
- Mon May 09 19:51:02 2016 +0000
- Revision:
- 0:c482a7d7d833
- Child:
- 1:01baa9666217
initial version - modified from https://github.com/janjongboom/sxsw
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 | 0:c482a7d7d833 | 3 | #include "fxos8700cq/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 | AnalogIn pad(A0); // connect a pad to the analog input |
maclobdell | 0:c482a7d7d833 | 10 | |
maclobdell | 0:c482a7d7d833 | 11 | Ticker readTicker; |
maclobdell | 0:c482a7d7d833 | 12 | |
maclobdell | 0:c482a7d7d833 | 13 | // Set up the accelerometer (this is specific to the onboard one) |
maclobdell | 0:c482a7d7d833 | 14 | InterruptIn accel_interrupt_pin(PTC13); |
maclobdell | 0:c482a7d7d833 | 15 | FXOS8700CQ accel(PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); |
maclobdell | 0:c482a7d7d833 | 16 | |
maclobdell | 0:c482a7d7d833 | 17 | static void silence() { |
maclobdell | 0:c482a7d7d833 | 18 | buzzer.write(0.0f); // silence! |
maclobdell | 0:c482a7d7d833 | 19 | } |
maclobdell | 0:c482a7d7d833 | 20 | |
maclobdell | 0:c482a7d7d833 | 21 | // this is our function that plays a tone. |
maclobdell | 0:c482a7d7d833 | 22 | // Takes in a tone frequency, and after duration (in ms.) we stop playing again |
maclobdell | 0:c482a7d7d833 | 23 | static void play_tone(int tone) { |
maclobdell | 0:c482a7d7d833 | 24 | buzzer.period_us(tone); |
maclobdell | 0:c482a7d7d833 | 25 | buzzer.write(0.10f); // 10% duty cycle, otherwise it's too loud |
maclobdell | 0:c482a7d7d833 | 26 | } |
maclobdell | 0:c482a7d7d833 | 27 | |
maclobdell | 0:c482a7d7d833 | 28 | static bool is_pad_high = false; |
maclobdell | 0:c482a7d7d833 | 29 | static void read_pad() { |
maclobdell | 0:c482a7d7d833 | 30 | // YOUR CODE HERE (2) |
maclobdell | 0:c482a7d7d833 | 31 | } |
maclobdell | 0:c482a7d7d833 | 32 | |
maclobdell | 0:c482a7d7d833 | 33 | static void play_note1() { |
maclobdell | 0:c482a7d7d833 | 34 | play_tone(NOTE_C4); |
maclobdell | 0:c482a7d7d833 | 35 | } |
maclobdell | 0:c482a7d7d833 | 36 | static void play_note2() { |
maclobdell | 0:c482a7d7d833 | 37 | play_tone(NOTE_D4); |
maclobdell | 0:c482a7d7d833 | 38 | } |
maclobdell | 0:c482a7d7d833 | 39 | |
maclobdell | 0:c482a7d7d833 | 40 | // YOUR CODE HERE (1) |
maclobdell | 0:c482a7d7d833 | 41 | |
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 | readTicker.attach(&read_pad, 0.03); // the address of the function to be attached (flip) and the interval (in seconds) |
maclobdell | 0:c482a7d7d833 | 61 | |
maclobdell | 0:c482a7d7d833 | 62 | } |