by Rob Toulson and Tim Wilmshurst from textbook "Fast and Effective Embedded Systems Design: Applying the ARM mbed"

Dependencies:   mbed

Committer:
robt
Date:
Fri May 24 22:12:18 2013 +0000
Revision:
0:a890f3cd9d1c
by Rob Toulson and Tim Wilmshurst from textbook "Fast and Effective Embedded Systems Design: Applying the ARM mbed"

Who changed what in which revision?

UserRevisionLine numberNew contents of line
robt 0:a890f3cd9d1c 1 /* Program Example 10.3: Interrupt toggle switch with formatted data logging to text file
robt 0:a890f3cd9d1c 2 */
robt 0:a890f3cd9d1c 3
robt 0:a890f3cd9d1c 4 #include "mbed.h"
robt 0:a890f3cd9d1c 5
robt 0:a890f3cd9d1c 6 InterruptIn button(p30); // Interrupt on digital input 20
robt 0:a890f3cd9d1c 7 DigitalOut led1(LED1); // digital out to onboard LED1
robt 0:a890f3cd9d1c 8 Timer debounce; // define debounce timer
robt 0:a890f3cd9d1c 9 LocalFileSystem local("local"); // define local file system
robt 0:a890f3cd9d1c 10
robt 0:a890f3cd9d1c 11 void toggle(void); // function prototype
robt 0:a890f3cd9d1c 12
robt 0:a890f3cd9d1c 13 int main() {
robt 0:a890f3cd9d1c 14 debounce.start();
robt 0:a890f3cd9d1c 15 button.rise(&toggle); // attach the address of the toggle function to the rising edge
robt 0:a890f3cd9d1c 16 }
robt 0:a890f3cd9d1c 17
robt 0:a890f3cd9d1c 18 void toggle() {
robt 0:a890f3cd9d1c 19 if (debounce.read_ms()>200)
robt 0:a890f3cd9d1c 20 led1=!led1;
robt 0:a890f3cd9d1c 21 FILE* Logfile = fopen ("/local/log.txt","a"); // open file for reading
robt 0:a890f3cd9d1c 22 fprintf(Logfile,"time=%.3fs: button pressed so setting led=%d\n\r",debounce.read(),led1.read());
robt 0:a890f3cd9d1c 23 fclose(Logfile);
robt 0:a890f3cd9d1c 24 debounce.reset();
robt 0:a890f3cd9d1c 25 }