Rob Toulson / Mbed 2 deprecated PE_10-03_InterruptDataLog

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* Program Example 10.3: Interrupt toggle switch with formatted data logging to text file
00002                                                                             */
00003 
00004 #include "mbed.h"
00005 
00006 InterruptIn button(p30);    // Interrupt on digital input 20
00007 DigitalOut led1(LED1);        // digital out to onboard LED1
00008 Timer debounce;          // define debounce timer
00009 LocalFileSystem local("local");           // define local file system
00010 
00011 void toggle(void);  // function prototype
00012 
00013 int main() {
00014     debounce.start();
00015     button.rise(&toggle);      // attach the address of the toggle function to the rising edge
00016 }
00017 
00018 void toggle() {
00019 if (debounce.read_ms()>200)
00020     led1=!led1;
00021     FILE* Logfile = fopen ("/local/log.txt","a");       // open file for reading
00022     fprintf(Logfile,"time=%.3fs: button pressed so setting led=%d\n\r",debounce.read(),led1.read());
00023     fclose(Logfile);
00024     debounce.reset();
00025 }