Switch a LED by using external interrupts created by a pushbutton. Note that button bounces will create unwanted interrupts as well...

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 InterruptIn button(BUTTON1);       // Pusbutton input (PC_13)
00004 DigitalOut led(LED1);              // LED output (PA_5)
00005 
00006 void button_pressed() {
00007     led = !led;                    // LED state changed at every button press
00008 }
00009 
00010 int main() {
00011     button.mode(PullUp);           // Enable internal pullup
00012     button.fall(&button_pressed);  // Attach function to falling edge
00013     while (true) {
00014         wait(0.2f);                // Nothing to do. Just wait
00015     }
00016 }