Working Dating game for Natural History Museum. Using Arduino, Relays KL and Raspberry pi. however needs cleanup. Change names of Relays CapKeys get rid of capRST - not being needed anymore

Dependencies:   SLCD mbed

Fork of CapKey_46_v2_sensors by Stanley Cohen

Committer:
bomalley
Date:
Wed Feb 25 21:33:35 2015 +0000
Revision:
5:5d684bfc69d6
Parent:
4:34ca86236686
Working however needs cleanup. ; Change names of ; Relays; CapKeys; get rid of capRST - not being needed anymore

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 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
bomalley 5:5d684bfc69d6 27 //Relay are the outputs to the KL and input to the rasb pi -->
bomalley 5:5d684bfc69d6 28 DigitalOut Relays[NUMREL] = {D10,D11,D12,PTB18,PTC13,PTC16}; // corresponds to touches 1,2,3,4 //
bomalley 5:5d684bfc69d6 29 DigitalIn capKeys[NUMREL] = {D2,D4,D6,D8,PTC6,PTC7}; //input
bomalley 5:5d684bfc69d6 30 DigitalOut capRST[NUMREL] = {D3,D5,D7,D9,PTC10,PTC11};
scohennm 4:34ca86236686 31 //DigitalIn sendKey(PTC3); // on board buttons just use 1 for test
scohennm 4:34ca86236686 32 DigitalOut REDLed(LED_RED); // red led
scohennm 4:34ca86236686 33 DigitalOut GREENLed(PTD5); // green led
scohennm 1:232e3f430f69 34
scohennm 0:8a169bac9011 35
scohennm 0:8a169bac9011 36
scohennm 4:34ca86236686 37 Ticker ledBlink; // timinginterrupt for RED led
scohennm 0:8a169bac9011 38 Timer millis;
scohennm 0:8a169bac9011 39 Serial pc(USBTX, USBRX);
scohennm 0:8a169bac9011 40 SLCD slcd; //define LCD display
scohennm 4:34ca86236686 41 //InterruptIn chanButton(PTC12);
scohennm 2:4535af993186 42 int channelSelect=0;
scohennm 2:4535af993186 43 int channelDisplay = 1;
scohennm 0:8a169bac9011 44
scohennm 0:8a169bac9011 45 // variables will change:
scohennm 0:8a169bac9011 46 int buttonState = true;
scohennm 0:8a169bac9011 47 long relayTimeout;
scohennm 0:8a169bac9011 48 long capKeyTimeout;
scohennm 0:8a169bac9011 49 int LCDCounter = 0;
scohennm 4:34ca86236686 50 int outState = false;
scohennm 4:34ca86236686 51
scohennm 4:34ca86236686 52 void LEDBlinker(){ // RED LED interrupt
scohennm 4:34ca86236686 53 outState = !outState;
scohennm 4:34ca86236686 54 REDLed.write(outState);
scohennm 4:34ca86236686 55 GREENLed.write(!outState);
scohennm 4:34ca86236686 56 }
scohennm 0:8a169bac9011 57
scohennm 2:4535af993186 58 void getChannel() { // button interrupt
scohennm 2:4535af993186 59 channelSelect = (channelSelect + 1) % NUMREL;
scohennm 2:4535af993186 60 channelDisplay = channelSelect +1;
scohennm 2:4535af993186 61 }
scohennm 2:4535af993186 62
scohennm 0:8a169bac9011 63 void LCDMess(char *lMess, float dWait){
scohennm 0:8a169bac9011 64 slcd.Home();
scohennm 0:8a169bac9011 65 slcd.clear();
scohennm 0:8a169bac9011 66 slcd.printf(lMess);
scohennm 0:8a169bac9011 67 wait(dWait);
scohennm 0:8a169bac9011 68 }
scohennm 0:8a169bac9011 69 // variable for reading the pushbutton status
scohennm 0:8a169bac9011 70
scohennm 0:8a169bac9011 71 int main() {
scohennm 0:8a169bac9011 72 int i;
scohennm 0:8a169bac9011 73 int buttonVal = 0;
scohennm 2:4535af993186 74
scohennm 4:34ca86236686 75 char lcdData[10]; //buffer needs places for decimal pt and colon
scohennm 0:8a169bac9011 76 int OKtoRead = true;
scohennm 2:4535af993186 77
scohennm 4:34ca86236686 78 ledBlink.attach(&LEDBlinker, LEDDELAY);// Alive LED
scohennm 0:8a169bac9011 79 millis.start();
scohennm 0:8a169bac9011 80 millis.reset();
scohennm 0:8a169bac9011 81
scohennm 4:34ca86236686 82 pc.printf(PROGNAME);
scohennm 4:34ca86236686 83 sprintf (lcdData,IDLEMESS); //Show that things are on
scohennm 4:34ca86236686 84 LCDMess(lcdData, LCDDELAY);
scohennm 0:8a169bac9011 85 for (i=0; i<NUMREL; i++){
scohennm 0:8a169bac9011 86 Relays[i].write(RELAY_OFF);
scohennm 0:8a169bac9011 87 capRST[i].write(HIGH);
bomalley 5:5d684bfc69d6 88 capKeys[i].mode(PullUp); // click out the pull-up resistor looks like it needs a pulldown
scohennm 0:8a169bac9011 89 }
scohennm 0:8a169bac9011 90
scohennm 0:8a169bac9011 91 while (true) {
scohennm 2:4535af993186 92 // Show active channel
scohennm 2:4535af993186 93
scohennm 4:34ca86236686 94
scohennm 0:8a169bac9011 95
scohennm 0:8a169bac9011 96 // read the state of the pushbutton value:
scohennm 0:8a169bac9011 97 if(OKtoRead) {
scohennm 4:34ca86236686 98 for (i=TESTCHANNEL; i<NUMREL; i++){
bomalley 5:5d684bfc69d6 99 if (capKeys[i] == LOW) {
scohennm 4:34ca86236686 100 channelSelect = i;
scohennm 4:34ca86236686 101 channelDisplay = channelSelect +1;
scohennm 4:34ca86236686 102 buttonVal = true; // Got a touch
scohennm 4:34ca86236686 103 break;
scohennm 4:34ca86236686 104 }
scohennm 4:34ca86236686 105 }
scohennm 4:34ca86236686 106 //buttonVal = !sendKey.read() ; // on board buttons just use 1
scohennm 2:4535af993186 107 if (buttonVal){
scohennm 2:4535af993186 108 Relays[channelSelect].write(RELAY_ON);
scohennm 2:4535af993186 109 capRST[channelSelect].write(LOW); //reset the cap key
scohennm 4:34ca86236686 110 pc.printf(RASPIOUTDBG, channelSelect);
scohennm 2:4535af993186 111 millis.reset();
scohennm 2:4535af993186 112 relayTimeout = RESET_DELAY;
scohennm 2:4535af993186 113 OKtoRead = false;
scohennm 4:34ca86236686 114 buttonVal = false;
scohennm 4:34ca86236686 115 sprintf (lcdData,CHNLLCDPRINT,channelDisplay);
scohennm 4:34ca86236686 116 LCDMess(lcdData, LCDDELAY);
scohennm 2:4535af993186 117
scohennm 0:8a169bac9011 118 }
scohennm 0:8a169bac9011 119 }//end OKtoRead
scohennm 1:232e3f430f69 120
scohennm 0:8a169bac9011 121 if (millis.read_ms() > relayTimeout) {
scohennm 0:8a169bac9011 122 OKtoRead = true;
scohennm 4:34ca86236686 123 sprintf (lcdData,IDLEMESS);
scohennm 4:34ca86236686 124 LCDMess(IDLEMESS, LCDDELAY);
scohennm 0:8a169bac9011 125 for (i=0; i<NUMREL; i++){
scohennm 0:8a169bac9011 126 Relays[i].write(RELAY_OFF);
scohennm 2:4535af993186 127 capRST[i].write(HIGH);
scohennm 0:8a169bac9011 128 }
scohennm 0:8a169bac9011 129 }
scohennm 1:232e3f430f69 130
scohennm 0:8a169bac9011 131 }// end forever while
scohennm 0:8a169bac9011 132 } // end main