Kevin Abraham / Mbed 2 deprecated 4180Lab1-2

Dependencies:   PinDetect mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "PinDetect.h"
00003 // must import Cookbook PinDetct library into project
00004 // URL: http://mbed.org/users/AjK/libraries/PinDetect/lkyxpw
00005 
00006 PwmOut myled(p23);
00007 
00008 PinDetect pb1(p8);
00009 PinDetect pb2(p7);
00010 // SPST Pushbutton debounced count demo using interrupts and callback
00011 // no external PullUp resistor needed
00012 // Pushbutton from P8 to GND.
00013 // Second Pushbutton from P7 to GND.
00014 // A pb hit generates an interrupt and activates the callback function
00015 // after the switch is debounced
00016 
00017 // Global pwm variable
00018 float volatile pwm=1.0f;
00019 
00020 // Callback routine is interrupt activated by a debounced pb1 hit
00021 void pb1_hit_callback (void)
00022 {
00023     pwm -= 0.1f;
00024     if (pwm < 0.0f) {
00025         pwm = 0.0f;
00026     }
00027 }
00028 
00029 // Callback routine is interrupt activated by a debounced pb2 hit
00030 void pb2_hit_callback (void)
00031 {
00032     pwm += 0.1f;
00033     if (pwm > 1.0f) {
00034         pwm = 1.0f;
00035     }
00036 }
00037 
00038 int main()
00039 {
00040 
00041     // Use internal pullups for pushbutton
00042     pb1.mode(PullUp);
00043     pb2.mode(PullUp);
00044     // Delay for initial pullup to take effect
00045     wait(.01);
00046     // Setup Interrupt callback functions for a pb hit
00047     pb1.attach_deasserted(&pb1_hit_callback);
00048     pb2.attach_deasserted(&pb2_hit_callback);
00049     // Start sampling pb inputs using interrupts
00050     pb1.setSampleFrequency();
00051     pb2.setSampleFrequency();
00052 
00053     while (1) {
00054         myled = pwm;
00055     }
00056 
00057 }