NMHU, KL46Z, buttons, leds HW 5.2 starting

Dependencies:   mbed

Fork of blink_kl46z_button_LCD_HW32 by Stanley Cohen

Committer:
scohennm
Date:
Thu Sep 24 15:08:25 2015 +0000
Revision:
5:dc506f422393
Parent:
4:db8f1d2ff809
Update eliminate LCD references

Who changed what in which revision?

UserRevisionLine numberNew contents of line
scohennm 0:e23fffd4b9a7 1 #include "mbed.h"
scohennm 2:b49e5adf84df 2
scohennm 0:e23fffd4b9a7 3 #define LEDON false
scohennm 0:e23fffd4b9a7 4 #define LEDOFF true
scohennm 1:2688f68df85d 5 #define NUMBUTS 2
scohennm 3:7e9670be412e 6 #define LBUT PTC12 // port addresses for buttons
scohennm 1:2688f68df85d 7 #define RBUT PTC3
scohennm 3:7e9670be412e 8 #define BLINKTIME 0.3 // in seconds
scohennm 3:7e9670be412e 9 #define BUTTONTIME 0.2
scohennm 2:b49e5adf84df 10 #define LCDCHARLEN 10
scohennm 2:b49e5adf84df 11 #define NUMMESS 2
scohennm 3:7e9670be412e 12
scohennm 4:db8f1d2ff809 13 #define PROGNAME "blink_kl46z_buttton no wait\r\n"
scohennm 0:e23fffd4b9a7 14
scohennm 3:7e9670be412e 15 // Timer to elliminate wait() function
scohennm 3:7e9670be412e 16 Timer LEDTimer; // for blinking LEDs
scohennm 3:7e9670be412e 17 Timer ButtonTimer; // for reading button states
scohennm 1:2688f68df85d 18
scohennm 3:7e9670be412e 19 bool ledState = LEDON;
scohennm 3:7e9670be412e 20
scohennm 1:2688f68df85d 21 DigitalIn buttons[NUMBUTS] = {RBUT, LBUT};
scohennm 1:2688f68df85d 22 DigitalOut LEDs[NUMBUTS] = {LED_GREEN, LED_RED};
scohennm 1:2688f68df85d 23 Serial pc(USBTX, USBRX);// set up USB as communicationis to Host PC via USB connectons
scohennm 0:e23fffd4b9a7 24
scohennm 3:7e9670be412e 25 void allLEDsOff(){
scohennm 3:7e9670be412e 26 int i;
scohennm 3:7e9670be412e 27 for (i=0; i<NUMBUTS; i++){
scohennm 3:7e9670be412e 28 LEDs[i] = LEDOFF;
scohennm 3:7e9670be412e 29 }
scohennm 3:7e9670be412e 30 }
scohennm 3:7e9670be412e 31
scohennm 2:b49e5adf84df 32
scohennm 3:7e9670be412e 33 void initialize_global_vars(){
scohennm 3:7e9670be412e 34 pc.printf(PROGNAME);
scohennm 3:7e9670be412e 35 // set up DAQ timers
scohennm 3:7e9670be412e 36 ButtonTimer.start();
scohennm 3:7e9670be412e 37 ButtonTimer.reset();
scohennm 3:7e9670be412e 38 LEDTimer.start();
scohennm 3:7e9670be412e 39 LEDTimer.reset();
scohennm 3:7e9670be412e 40 allLEDsOff();
scohennm 3:7e9670be412e 41 }
scohennm 1:2688f68df85d 42 // --------------------------------
scohennm 3:7e9670be412e 43 int main() {
scohennm 1:2688f68df85d 44 int i;
scohennm 1:2688f68df85d 45 int currentLED = 0;
scohennm 2:b49e5adf84df 46
scohennm 3:7e9670be412e 47 initialize_global_vars(); //keep things organized
scohennm 3:7e9670be412e 48 LEDs[currentLED] = LEDON;
scohennm 3:7e9670be412e 49 // End of setup
scohennm 3:7e9670be412e 50
scohennm 0:e23fffd4b9a7 51 while(true) {
scohennm 3:7e9670be412e 52 if (ButtonTimer > BUTTONTIME){
scohennm 3:7e9670be412e 53 for (i=0; i<NUMBUTS; i++){ // index will be 0 or 1
scohennm 3:7e9670be412e 54 if(!buttons[i]) {
scohennm 3:7e9670be412e 55 allLEDsOff();
scohennm 3:7e9670be412e 56 currentLED = i;
scohennm 3:7e9670be412e 57 } // if ! buttons
scohennm 3:7e9670be412e 58 }// for loop to look at buttons
scohennm 3:7e9670be412e 59 ButtonTimer.reset();
scohennm 1:2688f68df85d 60 }
scohennm 3:7e9670be412e 61 if(LEDTimer.read() > BLINKTIME){
scohennm 3:7e9670be412e 62 LEDTimer.reset();
scohennm 3:7e9670be412e 63 ledState = !ledState; // Flip the general state
scohennm 3:7e9670be412e 64 LEDs[currentLED] = ledState;
scohennm 3:7e9670be412e 65 }
scohennm 3:7e9670be412e 66 // Do other things here between times of reading and flashing
scohennm 0:e23fffd4b9a7 67 }
scohennm 0:e23fffd4b9a7 68 }