![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Full six channel cap sensor input and output CDG KL46Z control to Raspberry Pi NMHU Elements of Exhibit Design Fall 2014
Fork of CapKey_46_v2_button_osmon by
cdg_main.cpp@4:34ca86236686, 2014-11-13 (annotated)
- Committer:
- scohennm
- Date:
- Thu Nov 13 03:24:24 2014 +0000
- Revision:
- 4:34ca86236686
- Parent:
- 3:ee958939c828
Full six channel cap sensor input and output CDG KL46Z control to Raspberry Pi
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
scohennm | 0:8a169bac9011 | 1 | #include "mbed.h" |
scohennm | 0:8a169bac9011 | 2 | #include "SLCD.h" |
scohennm | 0:8a169bac9011 | 3 | |
scohennm | 0:8a169bac9011 | 4 | //Dating Game Capkey controller 07/16/2013 sc |
scohennm | 0:8a169bac9011 | 5 | // Modified for mbet KL46Z 10/07/2014 |
scohennm | 0:8a169bac9011 | 6 | // Four inputs and Four relays |
scohennm | 1:232e3f430f69 | 7 | // Changed to simulate capkeys with the KL on board buttons |
scohennm | 4:34ca86236686 | 8 | #define PROGNAME "DG Cap Key v2.1\r\n" |
scohennm | 2:4535af993186 | 9 | #define KEYHIT "SND" |
scohennm | 4:34ca86236686 | 10 | #define IDLEMESS "IDLE" |
scohennm | 0:8a169bac9011 | 11 | #define HIGH 1 |
scohennm | 0:8a169bac9011 | 12 | #define LOW 0 |
scohennm | 1:232e3f430f69 | 13 | #define RESET_DELAY 1200 |
scohennm | 0:8a169bac9011 | 14 | #define RELAY_ON HIGH |
scohennm | 0:8a169bac9011 | 15 | #define RELAY_OFF LOW |
scohennm | 0:8a169bac9011 | 16 | #define LED_ON LOW |
scohennm | 0:8a169bac9011 | 17 | #define LED_OFF HIGH |
scohennm | 0:8a169bac9011 | 18 | #define DEBOUNCE 50 |
josmon | 3:ee958939c828 | 19 | #define NUMREL 6 // really just the number of outputs |
scohennm | 0:8a169bac9011 | 20 | #define LCDDELAY 0.200 |
scohennm | 0:8a169bac9011 | 21 | #define COUNTERLIMIT 300 |
scohennm | 4:34ca86236686 | 22 | #define TESTCHANNEL 0 |
scohennm | 4:34ca86236686 | 23 | #define LEDDELAY 0.75 |
scohennm | 4:34ca86236686 | 24 | #define CHNLLCDPRINT "CH-%1d" |
scohennm | 4:34ca86236686 | 25 | #define RASPIOUTDBG "PIOUT = %d \n\r" |
scohennm | 2:4535af993186 | 26 | |
josmon | 3:ee958939c828 | 27 | DigitalOut Relays[NUMREL] = {D10,D11,D12,D13,PTC13,PTC16}; // corresponds to touches 1,2,3,4 |
scohennm | 4:34ca86236686 | 28 | DigitalIn capKeys[NUMREL] = {D2,D4,D6,D8,PTC6,PTC7}; |
josmon | 3:ee958939c828 | 29 | DigitalOut capRST[NUMREL] = {D3,D5,D7,D9,PTC10,PTC11}; |
scohennm | 4:34ca86236686 | 30 | //DigitalIn sendKey(PTC3); // on board buttons just use 1 for test |
scohennm | 4:34ca86236686 | 31 | DigitalOut REDLed(LED_RED); // red led |
scohennm | 4:34ca86236686 | 32 | DigitalOut GREENLed(PTD5); // green led |
scohennm | 1:232e3f430f69 | 33 | |
scohennm | 0:8a169bac9011 | 34 | |
scohennm | 0:8a169bac9011 | 35 | |
scohennm | 4:34ca86236686 | 36 | Ticker ledBlink; // timinginterrupt for RED led |
scohennm | 0:8a169bac9011 | 37 | Timer millis; |
scohennm | 0:8a169bac9011 | 38 | Serial pc(USBTX, USBRX); |
scohennm | 0:8a169bac9011 | 39 | SLCD slcd; //define LCD display |
scohennm | 4:34ca86236686 | 40 | //InterruptIn chanButton(PTC12); |
scohennm | 2:4535af993186 | 41 | int channelSelect=0; |
scohennm | 2:4535af993186 | 42 | int channelDisplay = 1; |
scohennm | 0:8a169bac9011 | 43 | |
scohennm | 0:8a169bac9011 | 44 | // variables will change: |
scohennm | 0:8a169bac9011 | 45 | int buttonState = true; |
scohennm | 0:8a169bac9011 | 46 | long relayTimeout; |
scohennm | 0:8a169bac9011 | 47 | long capKeyTimeout; |
scohennm | 0:8a169bac9011 | 48 | int LCDCounter = 0; |
scohennm | 4:34ca86236686 | 49 | int outState = false; |
scohennm | 4:34ca86236686 | 50 | |
scohennm | 4:34ca86236686 | 51 | void LEDBlinker(){ // RED LED interrupt |
scohennm | 4:34ca86236686 | 52 | outState = !outState; |
scohennm | 4:34ca86236686 | 53 | REDLed.write(outState); |
scohennm | 4:34ca86236686 | 54 | GREENLed.write(!outState); |
scohennm | 4:34ca86236686 | 55 | } |
scohennm | 0:8a169bac9011 | 56 | |
scohennm | 2:4535af993186 | 57 | void getChannel() { // button interrupt |
scohennm | 2:4535af993186 | 58 | channelSelect = (channelSelect + 1) % NUMREL; |
scohennm | 2:4535af993186 | 59 | channelDisplay = channelSelect +1; |
scohennm | 2:4535af993186 | 60 | } |
scohennm | 2:4535af993186 | 61 | |
scohennm | 0:8a169bac9011 | 62 | void LCDMess(char *lMess, float dWait){ |
scohennm | 0:8a169bac9011 | 63 | slcd.Home(); |
scohennm | 0:8a169bac9011 | 64 | slcd.clear(); |
scohennm | 0:8a169bac9011 | 65 | slcd.printf(lMess); |
scohennm | 0:8a169bac9011 | 66 | wait(dWait); |
scohennm | 0:8a169bac9011 | 67 | } |
scohennm | 0:8a169bac9011 | 68 | // variable for reading the pushbutton status |
scohennm | 0:8a169bac9011 | 69 | |
scohennm | 0:8a169bac9011 | 70 | int main() { |
scohennm | 0:8a169bac9011 | 71 | int i; |
scohennm | 0:8a169bac9011 | 72 | int buttonVal = 0; |
scohennm | 2:4535af993186 | 73 | |
scohennm | 4:34ca86236686 | 74 | char lcdData[10]; //buffer needs places for decimal pt and colon |
scohennm | 0:8a169bac9011 | 75 | int OKtoRead = true; |
scohennm | 2:4535af993186 | 76 | |
scohennm | 4:34ca86236686 | 77 | ledBlink.attach(&LEDBlinker, LEDDELAY);// Alive LED |
scohennm | 0:8a169bac9011 | 78 | millis.start(); |
scohennm | 0:8a169bac9011 | 79 | millis.reset(); |
scohennm | 0:8a169bac9011 | 80 | |
scohennm | 4:34ca86236686 | 81 | pc.printf(PROGNAME); |
scohennm | 4:34ca86236686 | 82 | sprintf (lcdData,IDLEMESS); //Show that things are on |
scohennm | 4:34ca86236686 | 83 | LCDMess(lcdData, LCDDELAY); |
scohennm | 0:8a169bac9011 | 84 | for (i=0; i<NUMREL; i++){ |
scohennm | 0:8a169bac9011 | 85 | Relays[i].write(RELAY_OFF); |
scohennm | 0:8a169bac9011 | 86 | capRST[i].write(HIGH); |
scohennm | 4:34ca86236686 | 87 | capKeys[i].mode(PullDown); // click out the pull-up resistor looks like it needs a pulldown |
scohennm | 0:8a169bac9011 | 88 | } |
scohennm | 0:8a169bac9011 | 89 | |
scohennm | 0:8a169bac9011 | 90 | while (true) { |
scohennm | 2:4535af993186 | 91 | // Show active channel |
scohennm | 2:4535af993186 | 92 | |
scohennm | 4:34ca86236686 | 93 | |
scohennm | 0:8a169bac9011 | 94 | |
scohennm | 0:8a169bac9011 | 95 | // read the state of the pushbutton value: |
scohennm | 0:8a169bac9011 | 96 | if(OKtoRead) { |
scohennm | 4:34ca86236686 | 97 | for (i=TESTCHANNEL; i<NUMREL; i++){ |
scohennm | 4:34ca86236686 | 98 | if (capKeys[i] == HIGH) { |
scohennm | 4:34ca86236686 | 99 | channelSelect = i; |
scohennm | 4:34ca86236686 | 100 | channelDisplay = channelSelect +1; |
scohennm | 4:34ca86236686 | 101 | buttonVal = true; // Got a touch |
scohennm | 4:34ca86236686 | 102 | break; |
scohennm | 4:34ca86236686 | 103 | } |
scohennm | 4:34ca86236686 | 104 | } |
scohennm | 4:34ca86236686 | 105 | //buttonVal = !sendKey.read() ; // on board buttons just use 1 |
scohennm | 2:4535af993186 | 106 | if (buttonVal){ |
scohennm | 2:4535af993186 | 107 | Relays[channelSelect].write(RELAY_ON); |
scohennm | 2:4535af993186 | 108 | capRST[channelSelect].write(LOW); //reset the cap key |
scohennm | 4:34ca86236686 | 109 | pc.printf(RASPIOUTDBG, channelSelect); |
scohennm | 2:4535af993186 | 110 | millis.reset(); |
scohennm | 2:4535af993186 | 111 | relayTimeout = RESET_DELAY; |
scohennm | 2:4535af993186 | 112 | OKtoRead = false; |
scohennm | 4:34ca86236686 | 113 | buttonVal = false; |
scohennm | 4:34ca86236686 | 114 | sprintf (lcdData,CHNLLCDPRINT,channelDisplay); |
scohennm | 4:34ca86236686 | 115 | LCDMess(lcdData, LCDDELAY); |
scohennm | 2:4535af993186 | 116 | |
scohennm | 0:8a169bac9011 | 117 | } |
scohennm | 0:8a169bac9011 | 118 | }//end OKtoRead |
scohennm | 1:232e3f430f69 | 119 | |
scohennm | 0:8a169bac9011 | 120 | if (millis.read_ms() > relayTimeout) { |
scohennm | 0:8a169bac9011 | 121 | OKtoRead = true; |
scohennm | 4:34ca86236686 | 122 | sprintf (lcdData,IDLEMESS); |
scohennm | 4:34ca86236686 | 123 | LCDMess(IDLEMESS, LCDDELAY); |
scohennm | 0:8a169bac9011 | 124 | for (i=0; i<NUMREL; i++){ |
scohennm | 0:8a169bac9011 | 125 | Relays[i].write(RELAY_OFF); |
scohennm | 2:4535af993186 | 126 | capRST[i].write(HIGH); |
scohennm | 0:8a169bac9011 | 127 | } |
scohennm | 0:8a169bac9011 | 128 | } |
scohennm | 1:232e3f430f69 | 129 | |
scohennm | 0:8a169bac9011 | 130 | }// end forever while |
scohennm | 0:8a169bac9011 | 131 | } // end main |