button interrupt with LED's and slider/LCD display demo
Fork of blink_kl46z_button_nowait by
main.cpp@3:7e9670be412e, 2015-09-24 (annotated)
- Committer:
- scohennm
- Date:
- Thu Sep 24 15:00:13 2015 +0000
- Revision:
- 3:7e9670be412e
- Parent:
- 2:b49e5adf84df
- Child:
- 4:db8f1d2ff809
NMHU, KL46Z, buttons, leds HW 5.2 starting
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
scohennm | 0:e23fffd4b9a7 | 1 | #include "mbed.h" |
scohennm | 2:b49e5adf84df | 2 | #include "SLCD.h" |
scohennm | 2:b49e5adf84df | 3 | |
scohennm | 0:e23fffd4b9a7 | 4 | #define LEDON false |
scohennm | 0:e23fffd4b9a7 | 5 | #define LEDOFF true |
scohennm | 1:2688f68df85d | 6 | #define NUMBUTS 2 |
scohennm | 3:7e9670be412e | 7 | #define LBUT PTC12 // port addresses for buttons |
scohennm | 1:2688f68df85d | 8 | #define RBUT PTC3 |
scohennm | 3:7e9670be412e | 9 | #define BLINKTIME 0.3 // in seconds |
scohennm | 3:7e9670be412e | 10 | #define BUTTONTIME 0.2 |
scohennm | 2:b49e5adf84df | 11 | #define LCDCHARLEN 10 |
scohennm | 2:b49e5adf84df | 12 | #define NUMMESS 2 |
scohennm | 3:7e9670be412e | 13 | |
scohennm | 3:7e9670be412e | 14 | #define PROGNAME "blink_kl46z_buttton no wait v2\r\n" |
scohennm | 0:e23fffd4b9a7 | 15 | |
scohennm | 0:e23fffd4b9a7 | 16 | // slightly more interesting blinky 140814 sc |
scohennm | 2:b49e5adf84df | 17 | SLCD slcd; //define LCD display |
scohennm | 0:e23fffd4b9a7 | 18 | |
scohennm | 3:7e9670be412e | 19 | // Timer to elliminate wait() function |
scohennm | 3:7e9670be412e | 20 | Timer LEDTimer; // for blinking LEDs |
scohennm | 3:7e9670be412e | 21 | Timer ButtonTimer; // for reading button states |
scohennm | 1:2688f68df85d | 22 | |
scohennm | 3:7e9670be412e | 23 | bool ledState = LEDON; |
scohennm | 3:7e9670be412e | 24 | |
scohennm | 1:2688f68df85d | 25 | DigitalIn buttons[NUMBUTS] = {RBUT, LBUT}; |
scohennm | 1:2688f68df85d | 26 | DigitalOut LEDs[NUMBUTS] = {LED_GREEN, LED_RED}; |
scohennm | 1:2688f68df85d | 27 | Serial pc(USBTX, USBRX);// set up USB as communicationis to Host PC via USB connectons |
scohennm | 0:e23fffd4b9a7 | 28 | |
scohennm | 3:7e9670be412e | 29 | void allLEDsOff(){ |
scohennm | 3:7e9670be412e | 30 | int i; |
scohennm | 3:7e9670be412e | 31 | for (i=0; i<NUMBUTS; i++){ |
scohennm | 3:7e9670be412e | 32 | LEDs[i] = LEDOFF; |
scohennm | 3:7e9670be412e | 33 | } |
scohennm | 3:7e9670be412e | 34 | } |
scohennm | 3:7e9670be412e | 35 | |
scohennm | 2:b49e5adf84df | 36 | void LCDMess(char *lMess){ |
scohennm | 2:b49e5adf84df | 37 | slcd.Home(); |
scohennm | 2:b49e5adf84df | 38 | slcd.clear(); |
scohennm | 2:b49e5adf84df | 39 | slcd.printf(lMess); |
scohennm | 2:b49e5adf84df | 40 | } |
scohennm | 2:b49e5adf84df | 41 | |
scohennm | 3:7e9670be412e | 42 | void initialize_global_vars(){ |
scohennm | 3:7e9670be412e | 43 | pc.printf(PROGNAME); |
scohennm | 3:7e9670be412e | 44 | // set up DAQ timers |
scohennm | 3:7e9670be412e | 45 | ButtonTimer.start(); |
scohennm | 3:7e9670be412e | 46 | ButtonTimer.reset(); |
scohennm | 3:7e9670be412e | 47 | LEDTimer.start(); |
scohennm | 3:7e9670be412e | 48 | LEDTimer.reset(); |
scohennm | 3:7e9670be412e | 49 | allLEDsOff(); |
scohennm | 3:7e9670be412e | 50 | } |
scohennm | 1:2688f68df85d | 51 | // -------------------------------- |
scohennm | 3:7e9670be412e | 52 | int main() { |
scohennm | 1:2688f68df85d | 53 | int i; |
scohennm | 1:2688f68df85d | 54 | int currentLED = 0; |
scohennm | 2:b49e5adf84df | 55 | |
scohennm | 3:7e9670be412e | 56 | initialize_global_vars(); //keep things organized |
scohennm | 3:7e9670be412e | 57 | LEDs[currentLED] = LEDON; |
scohennm | 3:7e9670be412e | 58 | // End of setup |
scohennm | 3:7e9670be412e | 59 | |
scohennm | 0:e23fffd4b9a7 | 60 | while(true) { |
scohennm | 3:7e9670be412e | 61 | if (ButtonTimer > BUTTONTIME){ |
scohennm | 3:7e9670be412e | 62 | for (i=0; i<NUMBUTS; i++){ // index will be 0 or 1 |
scohennm | 3:7e9670be412e | 63 | if(!buttons[i]) { |
scohennm | 3:7e9670be412e | 64 | allLEDsOff(); |
scohennm | 3:7e9670be412e | 65 | currentLED = i; |
scohennm | 3:7e9670be412e | 66 | } // if ! buttons |
scohennm | 3:7e9670be412e | 67 | }// for loop to look at buttons |
scohennm | 3:7e9670be412e | 68 | ButtonTimer.reset(); |
scohennm | 1:2688f68df85d | 69 | } |
scohennm | 3:7e9670be412e | 70 | if(LEDTimer.read() > BLINKTIME){ |
scohennm | 3:7e9670be412e | 71 | LEDTimer.reset(); |
scohennm | 3:7e9670be412e | 72 | ledState = !ledState; // Flip the general state |
scohennm | 3:7e9670be412e | 73 | LEDs[currentLED] = ledState; |
scohennm | 3:7e9670be412e | 74 | } |
scohennm | 3:7e9670be412e | 75 | // Do other things here between times of reading and flashing |
scohennm | 0:e23fffd4b9a7 | 76 | } |
scohennm | 0:e23fffd4b9a7 | 77 | } |