]> Devi Nivas Git - gt-ece4180-lab02.git/commitdiff
Update 06-Keypad.ino
authorPhonekeo, Lila <lphonekeo3@gatech.edu>
Thu, 2 Oct 2025 00:29:58 +0000 (20:29 -0400)
committerGitHub Enterprise <github-noreply@oit.gatech.edu>
Thu, 2 Oct 2025 00:29:58 +0000 (20:29 -0400)
06-Keypad/06-Keypad.ino

index 74f966a4e2fe85eb6097ef342c3d3f2633e66ad8..658e85808617ff5678d459f632de1fef9c11b6cf 100644 (file)
@@ -1,6 +1,94 @@
+#include <Wire.h>
+#include <Adafruit_MPR121.h>
+
+#ifndef _BV
+#define _BV(bit) (1 << (bit)) 
+#endif
+
+Adafruit_MPR121 cap = Adafruit_MPR121();
+
+#define SDA_PIN 21
+#define SCL_PIN 22
+
+// Keeps track of the last pins touched
+// so we know when buttons are 'released'
+uint16_t lasttouched = 0;
+uint16_t currtouched = 0;
+
+void dump_regs() { //example helper function
+  Serial.println("========================================");
+  Serial.println("CHAN 00 01 02 03 04 05 06 07 08 09 10 11");
+  Serial.println("     -- -- -- -- -- -- -- -- -- -- -- --"); 
+  // CDC
+  Serial.print("CDC: ");
+  for (int chan=0; chan<12; chan++) {
+    uint8_t reg = cap.readRegister8(0x5F+chan);
+    if (reg < 10) Serial.print(" ");
+    Serial.print(reg);
+    Serial.print(" ");
+  }
+  Serial.println();
+  // CDT
+  Serial.print("CDT: ");
+  for (int chan=0; chan<6; chan++) {
+    uint8_t reg = cap.readRegister8(0x6C+chan);
+    uint8_t cdtx = reg & 0b111;
+    uint8_t cdty = (reg >> 4) & 0b111;
+    if (cdtx < 10) Serial.print(" ");
+    Serial.print(cdtx);
+    Serial.print(" ");
+    if (cdty < 10) Serial.print(" ");
+    Serial.print(cdty);
+    Serial.print(" ");
+  }
+  Serial.println();
+  Serial.println("========================================");
+}
 
 void setup() {
+  delay(1000);
+  Serial.begin(115200);
+  while (!Serial);
+  Serial.println("MPR121 Autoconfiguration Test. (MPR121-AutoConfig.ino)");
+  
+  Serial.println("startup `Wire`");
+  Wire.begin(SDA_PIN, SCL_PIN);
+  Serial.println("startup `Wire` done.");
+  delay(100);
+  
+  Serial.println("cap.begin..");
+  if (!cap.begin(0x5A, &Wire)) {
+    Serial.println("MPR121 not found, check wiring?");
+    while (1);
+  }
+  Serial.println("MPR121 found!");
+  
+  delay(100);
+
+  Serial.println("Initial CDC/CDT values:");
+  dump_regs();
+
+  cap.setAutoconfig(true);
+
+  Serial.println("After autoconfig CDC/CDT values:");
+  dump_regs();
 }
 
 void loop() {
+  // Get the currently touched pads
+  currtouched = cap.touched();
+  
+  for (uint8_t i=0; i<12; i++) {
+    // it if *is* touched and *wasnt* touched before, alert!
+    if ((currtouched & _BV(i)) && !(lasttouched & _BV(i)) ) {
+      Serial.print(i); Serial.println(" touched");
+    }
+    // if it *was* touched and now *isnt*, alert!
+    //if (!(currtouched & _BV(i)) && (lasttouched & _BV(i)) ) {
+    //  Serial.print(i); Serial.println(" released");
+    //}
+  }
+
+  // reset our state
+  lasttouched = currtouched;
 }