altered scohennm's code to allow for 6 outputs

Dependencies:   SLCD mbed

Fork of CapKey_46_v2_button by Stanley Cohen

Committer:
scohennm
Date:
Mon Oct 20 21:02:24 2014 +0000
Revision:
0:8a169bac9011
Child:
1:232e3f430f69
NMHU and NMMNHS capactive key program interface to Raspberry Pi

Who changed what in which revision?

UserRevisionLine numberNew 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 0:8a169bac9011 7 #define PROGNAME "DG Cap Key v1.1\r\n"
scohennm 0:8a169bac9011 8 #define KEYHIT "KEY1"
scohennm 0:8a169bac9011 9 #define SERIAL_SPEED 9600
scohennm 0:8a169bac9011 10 #define HIGH 1
scohennm 0:8a169bac9011 11 #define LOW 0
scohennm 0:8a169bac9011 12 #define RESET_DELAY 1500
scohennm 0:8a169bac9011 13 #define RELAY_ON HIGH
scohennm 0:8a169bac9011 14 #define RELAY_OFF LOW
scohennm 0:8a169bac9011 15 #define LED_ON LOW
scohennm 0:8a169bac9011 16 #define LED_OFF HIGH
scohennm 0:8a169bac9011 17 #define DEBOUNCE 50
scohennm 0:8a169bac9011 18 #define NUMREL 4
scohennm 0:8a169bac9011 19 #define LCDDELAY 0.200
scohennm 0:8a169bac9011 20 #define COUNTERLIMIT 300
scohennm 0:8a169bac9011 21
scohennm 0:8a169bac9011 22 // constants won't change. They're used here to
scohennm 0:8a169bac9011 23 // set pin numbers:
scohennm 0:8a169bac9011 24 //const int buttonPin = PUSH2;
scohennm 0:8a169bac9011 25
scohennm 0:8a169bac9011 26
scohennm 0:8a169bac9011 27 DigitalOut Relays[NUMREL] = {D10,D11,D12,D13}; // corresponds to touches 1,2,3,4
scohennm 0:8a169bac9011 28 DigitalIn capKeys[NUMREL] = {D2,D4,D6,D8};
scohennm 0:8a169bac9011 29 DigitalOut capRST[NUMREL] = {D3,D5,D7,D9};
scohennm 0:8a169bac9011 30 /*
scohennm 0:8a169bac9011 31 DigitalOut Relays[NUMREL] = {D10}; // corresponds to touches 1,2,3,4
scohennm 0:8a169bac9011 32 DigitalIn capKeys[NUMREL] = {D2};
scohennm 0:8a169bac9011 33 DigitalOut capRST[NUMREL] = {D3,};
scohennm 0:8a169bac9011 34 */
scohennm 0:8a169bac9011 35 DigitalOut RLed(LED_RED);
scohennm 0:8a169bac9011 36 DigitalOut GLed(LED_GREEN);
scohennm 0:8a169bac9011 37
scohennm 0:8a169bac9011 38 Timer millis;
scohennm 0:8a169bac9011 39 Serial pc(USBTX, USBRX);
scohennm 0:8a169bac9011 40 SLCD slcd; //define LCD display
scohennm 0:8a169bac9011 41
scohennm 0:8a169bac9011 42 // variables will change:
scohennm 0:8a169bac9011 43 int buttonState = true;
scohennm 0:8a169bac9011 44 long relayTimeout;
scohennm 0:8a169bac9011 45 long capKeyTimeout;
scohennm 0:8a169bac9011 46 int LCDCounter = 0;
scohennm 0:8a169bac9011 47
scohennm 0:8a169bac9011 48 void LCDMess(char *lMess, float dWait){
scohennm 0:8a169bac9011 49 slcd.Home();
scohennm 0:8a169bac9011 50 slcd.clear();
scohennm 0:8a169bac9011 51 slcd.printf(lMess);
scohennm 0:8a169bac9011 52 wait(dWait);
scohennm 0:8a169bac9011 53 }
scohennm 0:8a169bac9011 54 // variable for reading the pushbutton status
scohennm 0:8a169bac9011 55
scohennm 0:8a169bac9011 56 int main() {
scohennm 0:8a169bac9011 57 int i;
scohennm 0:8a169bac9011 58 int buttonVal = 0;
scohennm 0:8a169bac9011 59 int buttonVal2 = 0;
scohennm 0:8a169bac9011 60 int ledState = LED_ON;
scohennm 0:8a169bac9011 61 char lcdData[10]; //buffer needs places dor decimal pt and colon
scohennm 0:8a169bac9011 62
scohennm 0:8a169bac9011 63 int OKtoRead = true;
scohennm 0:8a169bac9011 64 // initialize the LED pin as an output:
scohennm 0:8a169bac9011 65 millis.start();
scohennm 0:8a169bac9011 66 millis.reset();
scohennm 0:8a169bac9011 67
scohennm 0:8a169bac9011 68 pc.printf(PROGNAME);
scohennm 0:8a169bac9011 69 for (i=0; i<NUMREL; i++){
scohennm 0:8a169bac9011 70 Relays[i].write(RELAY_OFF);
scohennm 0:8a169bac9011 71 capRST[i].write(HIGH);
scohennm 0:8a169bac9011 72 }
scohennm 0:8a169bac9011 73
scohennm 0:8a169bac9011 74 while (true) {
scohennm 0:8a169bac9011 75 // For dramatic effect
scohennm 0:8a169bac9011 76 ledState = !ledState;
scohennm 0:8a169bac9011 77 LCDCounter++;
scohennm 0:8a169bac9011 78 LCDCounter = LCDCounter % COUNTERLIMIT;
scohennm 0:8a169bac9011 79 sprintf (lcdData,"%3d",LCDCounter);
scohennm 0:8a169bac9011 80 LCDMess(lcdData, LCDDELAY);
scohennm 0:8a169bac9011 81 RLed.write(ledState);
scohennm 0:8a169bac9011 82 GLed.write(ledState);
scohennm 0:8a169bac9011 83 //End dramatic effect
scohennm 0:8a169bac9011 84
scohennm 0:8a169bac9011 85
scohennm 0:8a169bac9011 86 // read the state of the pushbutton value:
scohennm 0:8a169bac9011 87 if(OKtoRead) {
scohennm 0:8a169bac9011 88 for (i=0; i<NUMREL;i++){
scohennm 0:8a169bac9011 89 buttonVal = capKeys[i].read();
scohennm 0:8a169bac9011 90 wait_ms(DEBOUNCE);
scohennm 0:8a169bac9011 91 buttonVal2 = capKeys[i].read();
scohennm 0:8a169bac9011 92 buttonVal = buttonVal && buttonVal2;
scohennm 0:8a169bac9011 93 if (buttonVal){
scohennm 0:8a169bac9011 94 LCDMess(KEYHIT, LCDDELAY);
scohennm 0:8a169bac9011 95 Relays[i].write(RELAY_ON);
scohennm 0:8a169bac9011 96 capRST[i].write(LOW); //reset the cap key
scohennm 0:8a169bac9011 97 pc.printf("i=%d Relays[i] = %d\n\r", i, Relays[i]);
scohennm 0:8a169bac9011 98 millis.reset();
scohennm 0:8a169bac9011 99 relayTimeout = RESET_DELAY;
scohennm 0:8a169bac9011 100 OKtoRead = false;
scohennm 0:8a169bac9011 101 break;
scohennm 0:8a169bac9011 102 }
scohennm 0:8a169bac9011 103 }
scohennm 0:8a169bac9011 104 }//end OKtoRead
scohennm 0:8a169bac9011 105
scohennm 0:8a169bac9011 106 if (millis.read_ms() > relayTimeout) {
scohennm 0:8a169bac9011 107 OKtoRead = true;
scohennm 0:8a169bac9011 108 for (i=0; i<NUMREL; i++){
scohennm 0:8a169bac9011 109 Relays[i].write(RELAY_OFF);
scohennm 0:8a169bac9011 110 capRST[i].write(HIGH);
scohennm 0:8a169bac9011 111
scohennm 0:8a169bac9011 112 }
scohennm 0:8a169bac9011 113 }
scohennm 0:8a169bac9011 114 }// end forever while
scohennm 0:8a169bac9011 115 } // end main