Dependencies:   mbed

Committer:
bmol
Date:
Thu Aug 06 15:20:15 2020 +0000
Revision:
0:49e2a9f9bb16
IT Tralee

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bmol 0:49e2a9f9bb16 1 #include "mbed.h"
bmol 0:49e2a9f9bb16 2
bmol 0:49e2a9f9bb16 3 InterruptIn joystickcenter(p14);
bmol 0:49e2a9f9bb16 4 InterruptIn button(p9);
bmol 0:49e2a9f9bb16 5 DigitalOut led(LED1);
bmol 0:49e2a9f9bb16 6 DigitalOut flash(LED4);
bmol 0:49e2a9f9bb16 7 Timer debounce; // define debounce timer
bmol 0:49e2a9f9bb16 8
bmol 0:49e2a9f9bb16 9
bmol 0:49e2a9f9bb16 10
bmol 0:49e2a9f9bb16 11 void flip()
bmol 0:49e2a9f9bb16 12 {
bmol 0:49e2a9f9bb16 13 if (debounce.read_ms()>200){// only allow toggle if debounce timer ahs passed 200ms
bmol 0:49e2a9f9bb16 14
bmol 0:49e2a9f9bb16 15 led=!led;
bmol 0:49e2a9f9bb16 16 debounce.reset(); // restart timer when the toggle is performed
bmol 0:49e2a9f9bb16 17 }
bmol 0:49e2a9f9bb16 18
bmol 0:49e2a9f9bb16 19
bmol 0:49e2a9f9bb16 20
bmol 0:49e2a9f9bb16 21
bmol 0:49e2a9f9bb16 22 }
bmol 0:49e2a9f9bb16 23 int main()
bmol 0:49e2a9f9bb16 24 {
bmol 0:49e2a9f9bb16 25 debounce.start();// This enables the timer
bmol 0:49e2a9f9bb16 26 joystickcenter.rise(&flip); // attach the function address to the rising edge
bmol 0:49e2a9f9bb16 27 button.mode(PullUp); // With this, no external pullup resistor needed
bmol 0:49e2a9f9bb16 28 button.rise(&flip); // attach the function address to the rising edge
bmol 0:49e2a9f9bb16 29
bmol 0:49e2a9f9bb16 30 while(1) { // wait around, interrupts will interrupt this!
bmol 0:49e2a9f9bb16 31
bmol 0:49e2a9f9bb16 32 flash = !flash; // turns LED4 on if off, off if on
bmol 0:49e2a9f9bb16 33 wait(0.25); // the instruction to wait for a quarter-second
bmol 0:49e2a9f9bb16 34 }
bmol 0:49e2a9f9bb16 35 }