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

Revision:
4:34ca86236686
Parent:
3:ee958939c828
Child:
5:5d684bfc69d6
--- a/cdg_main.cpp	Tue Nov 04 05:27:58 2014 +0000
+++ b/cdg_main.cpp	Thu Nov 13 03:24:24 2014 +0000
@@ -5,9 +5,9 @@
 // Modified for mbet KL46Z 10/07/2014
 // 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 PROGNAME "DG Cap Key v2.1\r\n"
 #define KEYHIT "SND"
-#define SERIAL_SPEED 9600
+#define IDLEMESS "IDLE"
 #define HIGH 1
 #define LOW 0
 #define RESET_DELAY 1200
@@ -19,20 +19,25 @@
 #define NUMREL 6 // really just the number of outputs
 #define LCDDELAY 0.200
 #define COUNTERLIMIT 300
-
+#define TESTCHANNEL 0
+#define LEDDELAY 0.75
+#define CHNLLCDPRINT "CH-%1d"
+#define RASPIOUTDBG "PIOUT = %d \n\r"
 
 DigitalOut Relays[NUMREL] = {D10,D11,D12,D13,PTC13,PTC16}; // corresponds to touches 1,2,3,4
-//DigitalIn capKeys[NUMREL] = {D2,D4,D6,D8};
+DigitalIn capKeys[NUMREL] = {D2,D4,D6,D8,PTC6,PTC7};
 DigitalOut capRST[NUMREL] = {D3,D5,D7,D9,PTC10,PTC11};
-DigitalIn sendKey(PTC3); // on board buttons just use 1 for test
+//DigitalIn sendKey(PTC3); // on board buttons just use 1 for test
+DigitalOut REDLed(LED_RED); // red led
+DigitalOut GREENLed(PTD5); // green led
 
 
 
-
+Ticker ledBlink; // timinginterrupt for RED led
 Timer millis;
 Serial pc(USBTX, USBRX);
 SLCD slcd; //define LCD display
-InterruptIn chanButton(PTC12);
+//InterruptIn chanButton(PTC12);
 int channelSelect=0;
 int channelDisplay = 1;
 
@@ -41,6 +46,13 @@
 long relayTimeout;
 long capKeyTimeout;
 int LCDCounter = 0;
+int outState = false;
+
+void LEDBlinker(){  // RED LED interrupt
+    outState = !outState; 
+    REDLed.write(outState);
+    GREENLed.write(!outState);
+}
 
 void getChannel() { // button interrupt
     channelSelect = (channelSelect + 1) % NUMREL;
@@ -59,46 +71,56 @@
   int i;
   int buttonVal = 0;
 
-  char lcdData[10]; //buffer needs places dor decimal pt and colon
-
+  char lcdData[10]; //buffer needs places for decimal pt and colon
   int OKtoRead = true;
   
-  chanButton.fall(&getChannel);
-  
-  // initialize the LED pin as an output:  
+  ledBlink.attach(&LEDBlinker, LEDDELAY);// Alive LED
   millis.start();
   millis.reset();
   
-  pc.printf(PROGNAME);  
+  pc.printf(PROGNAME);
+  sprintf (lcdData,IDLEMESS); //Show that things are on
+  LCDMess(lcdData, LCDDELAY);  
   for (i=0; i<NUMREL; i++){ 
           Relays[i].write(RELAY_OFF);
           capRST[i].write(HIGH);
+          capKeys[i].mode(PullDown); // click out the pull-up resistor looks like it needs a pulldown
   }  
 
  while (true) {
    // Show active channel
    
-   sprintf (lcdData,"CH-%1d",channelDisplay);
-   LCDMess(lcdData, LCDDELAY);
+   
    
   // read the state of the pushbutton value:
   if(OKtoRead) {
-    buttonVal = !sendKey.read() ; // on board buttons just use 1
+    for (i=TESTCHANNEL; i<NUMREL; i++){
+        if (capKeys[i] == HIGH) {
+            channelSelect = i;
+            channelDisplay = channelSelect +1;
+            buttonVal = true; // Got a touch
+            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);
+        pc.printf(RASPIOUTDBG, channelSelect);
         millis.reset();
         relayTimeout = RESET_DELAY;
         OKtoRead = false;
+        buttonVal = false;
+        sprintf (lcdData,CHNLLCDPRINT,channelDisplay);
+        LCDMess(lcdData, LCDDELAY);
     
       }
     }//end OKtoRead
 
     if (millis.read_ms() > relayTimeout) {
       OKtoRead = true;
+      sprintf (lcdData,IDLEMESS);
+      LCDMess(IDLEMESS, LCDDELAY);
       for (i=0; i<NUMREL; i++){ 
          Relays[i].write(RELAY_OFF);
          capRST[i].write(HIGH);