Simple button and slider command to PC program, adapted from the KL46Z button with interrupt and slider made by Stan
Fork of blink_kl46z_but_interrupt_slider by
Diff: main.cpp
- Revision:
- 3:7e9670be412e
- Parent:
- 2:b49e5adf84df
- Child:
- 4:db8f1d2ff809
--- a/main.cpp Mon Sep 14 14:14:04 2015 +0000 +++ b/main.cpp Thu Sep 24 15:00:13 2015 +0000 @@ -3,60 +3,75 @@ #define LEDON false #define LEDOFF true -#define BUTDOWN false -#define BUTUP true #define NUMBUTS 2 -#define LBUT PTC12 +#define LBUT PTC12 // port addresses for buttons #define RBUT PTC3 -#define BLINKTIME 0.3 -#define REDMESS "RED LED is ON\r\n" // adding DR and line feed for terminal line advance +#define BLINKTIME 0.3 // in seconds +#define BUTTONTIME 0.2 #define LCDCHARLEN 10 #define NUMMESS 2 -#define LRED "RED" -#define LGREEN "GREN" -#define PRED "RED\r\n" -#define PGREEN "GREEN\r\n" -#define PROGNAME "blink_kl46z_buttton LCD v1\r\n" + +#define PROGNAME "blink_kl46z_buttton no wait v2\r\n" // slightly more interesting blinky 140814 sc SLCD slcd; //define LCD display +// Timer to elliminate wait() function +Timer LEDTimer; // for blinking LEDs +Timer ButtonTimer; // for reading button states -int ledState = LEDON; -int buttonStates[NUMBUTS] = {BUTDOWN,BUTUP}; +bool ledState = LEDON; + DigitalIn buttons[NUMBUTS] = {RBUT, LBUT}; DigitalOut LEDs[NUMBUTS] = {LED_GREEN, LED_RED}; Serial pc(USBTX, USBRX);// set up USB as communicationis to Host PC via USB connectons +void allLEDsOff(){ + int i; + for (i=0; i<NUMBUTS; i++){ + LEDs[i] = LEDOFF; + } +} + void LCDMess(char *lMess){ slcd.Home(); slcd.clear(); slcd.printf(lMess); } +void initialize_global_vars(){ + pc.printf(PROGNAME); + // set up DAQ timers + ButtonTimer.start(); + ButtonTimer.reset(); + LEDTimer.start(); + LEDTimer.reset(); + allLEDsOff(); +} // -------------------------------- -int main() { + int main() { int i; int currentLED = 0; - char rMess[NUMMESS][LCDCHARLEN]={LGREEN, LRED}; - char pMess[NUMMESS][LCDCHARLEN]={PRED, PGREEN}; - pc.printf(PROGNAME); - LCDMess(rMess[currentLED]); - pc.printf(pMess[currentLED]); + initialize_global_vars(); //keep things organized + LEDs[currentLED] = LEDON; +// End of setup + while(true) { - for (i=0; i<NUMBUTS; i++){ // index will be 0 or 1 - LEDs[i] = LEDOFF; - // if(buttons[i] == BUTDOWN) { - if(!buttons[i]) { - LCDMess(rMess[i]); - pc.printf(pMess[i]); // this means that RED will be blinking - currentLED = i; - } + if (ButtonTimer > BUTTONTIME){ + for (i=0; i<NUMBUTS; i++){ // index will be 0 or 1 + if(!buttons[i]) { + allLEDsOff(); + currentLED = i; + } // if ! buttons + }// for loop to look at buttons + ButtonTimer.reset(); } - - ledState = !ledState; // Flip the general state - LEDs[currentLED] = ledState; - wait(BLINKTIME); + if(LEDTimer.read() > BLINKTIME){ + LEDTimer.reset(); + ledState = !ledState; // Flip the general state + LEDs[currentLED] = ledState; + } + // Do other things here between times of reading and flashing } } \ No newline at end of file