altered scohennm's code to allow for 6 outputs
Fork of CapKey_46_v2_button by
Diff: cdg_main.cpp
- Revision:
- 2:4535af993186
- Parent:
- 1:232e3f430f69
- Child:
- 3:ee958939c828
diff -r 232e3f430f69 -r 4535af993186 cdg_main.cpp --- a/cdg_main.cpp Wed Oct 29 22:13:25 2014 +0000 +++ b/cdg_main.cpp Fri Oct 31 22:32:27 2014 +0000 @@ -6,7 +6,7 @@ // Four inputs and Four relays // Changed to simulate capkeys with the KL on board buttons #define PROGNAME "DG Cap Key v1.1\r\n" -#define KEYHIT "KEY1" +#define KEYHIT "SND" #define SERIAL_SPEED 9600 #define HIGH 1 #define LOW 0 @@ -16,31 +16,25 @@ #define LED_ON LOW #define LED_OFF HIGH #define DEBOUNCE 50 -#define NUMREL 2 // really just the number of outputs +#define NUMREL 4 // really just the number of outputs #define LCDDELAY 0.200 #define COUNTERLIMIT 300 -// constants won't change. They're used here to + +DigitalOut Relays[NUMREL] = {D10,D11,D12,D13}; // corresponds to touches 1,2,3,4 +//DigitalIn capKeys[NUMREL] = {D2,D4,D6,D8}; +DigitalOut capRST[NUMREL] = {D3,D5,D7,D9}; +DigitalIn sendKey(PTC3); // on board buttons just use 1 for test -/* -DigitalOut Relays[NUMREL] = {D10,D11,D12,D13}; // corresponds to touches 1,2,3,4 -DigitalIn capKeys[NUMREL] = {PTC3,D4,D6,D8}; -DigitalOut capRST[NUMREL] = {D3,D5,D7,D9}; -*/ -// make two channel both buttons for testing -DigitalOut Relays[NUMREL] = {D10,D11}; -DigitalIn capKeys[NUMREL] = {PTC3, PTC12}; // on board buttons -DigitalOut capRST[NUMREL] = {D3,D5}; -DigitalOut LEDs[NUMREL] = {LED_GREEN, LED_RED}; -/* -DigitalOut RLed(LED_RED); -DigitalOut GLed(LED_GREEN); -*/ + Timer millis; Serial pc(USBTX, USBRX); SLCD slcd; //define LCD display +InterruptIn chanButton(PTC12); +int channelSelect=0; +int channelDisplay = 1; // variables will change: int buttonState = true; @@ -48,6 +42,11 @@ long capKeyTimeout; int LCDCounter = 0; +void getChannel() { // button interrupt + channelSelect = (channelSelect + 1) % NUMREL; + channelDisplay = channelSelect +1; +} + void LCDMess(char *lMess, float dWait){ slcd.Home(); slcd.clear(); @@ -59,11 +58,13 @@ int main() { int i; int buttonVal = 0; - int buttonVal2 = 0; - int ledState = LED_ON; + char lcdData[10]; //buffer needs places dor decimal pt and colon int OKtoRead = true; + + chanButton.fall(&getChannel); + // initialize the LED pin as an output: millis.start(); millis.reset(); @@ -72,39 +73,27 @@ for (i=0; i<NUMREL; i++){ Relays[i].write(RELAY_OFF); capRST[i].write(HIGH); - LEDs[i].write(LED_OFF); } while (true) { - // For dramatic effect - ledState = !ledState; - LCDCounter++; - LCDCounter = LCDCounter % COUNTERLIMIT; - sprintf (lcdData,"%3d",LCDCounter); + // Show active channel + + sprintf (lcdData,"CH-%1d",channelDisplay); LCDMess(lcdData, LCDDELAY); - //RLed.write(ledState); - //GLed.write(ledState); - //End dramatic effect - // read the state of the pushbutton value: if(OKtoRead) { - for (i=0; i<NUMREL;i++){ - buttonVal = ! capKeys[i].read(); - wait_ms(DEBOUNCE); - buttonVal2 = ! capKeys[i].read(); - buttonVal = buttonVal && buttonVal2; - if (buttonVal){ - LCDMess(KEYHIT, LCDDELAY); - Relays[i].write(RELAY_ON); - capRST[i].write(LOW); //reset the cap key - LEDs[i].write(LED_ON); - pc.printf("Relay = %d \n\r", i); - millis.reset(); - relayTimeout = RESET_DELAY; - OKtoRead = false; - break; - } + buttonVal = !sendKey.read() ; // on board buttons just use 1 + if (buttonVal){ + sprintf (lcdData,"SND%1d",channelDisplay); + LCDMess(lcdData, LCDDELAY); + Relays[channelSelect].write(RELAY_ON); + capRST[channelSelect].write(LOW); //reset the cap key + pc.printf("Relay = %d \n\r", channelSelect); + millis.reset(); + relayTimeout = RESET_DELAY; + OKtoRead = false; + } }//end OKtoRead @@ -112,8 +101,7 @@ OKtoRead = true; for (i=0; i<NUMREL; i++){ Relays[i].write(RELAY_OFF); - capRST[i].write(HIGH); - LEDs[i].write(LED_OFF); + capRST[i].write(HIGH); } }