+ // put your main code here, to run repeatedly:
+ if(!partone) {
+ checkPasscode();
+ if(passcodeCheck ==true) {
+ Display.println("Locked 1/2");
+ delay(500);
+ }
+ }
+ if(!parttwo) {
+ if(passcodeCheck == true) {
+ checkTilt();
+ if(tiltcodeCheck == true) {
+ //unlocked = true;
+ Display.gfx_Cls();
+ Display.txt_FGcolour(GREEN);
+ Display.println("Unlocked!");
+ Serial.println("Fully Unlocked");
+ delay(1000);
+ }
+ }
+ }
+}
+//function definitions
+void checkPasscode() {
+ currtouched = cap.touched();
+
+ for (uint8_t i=0; i<12; i++) {
+ bool justPressed = (currtouched & _BV(i)) && !(lasttouched & _BV(i));
+ bool justReleased = !(currtouched & _BV(i)) && (lasttouched & _BV(i));
+
+ // process only on release so it fires once per touch
+ if (justReleased) {
+ if (i == 11) {
+ if (attempt.length() > 0) {
+ attempt.remove(attempt.length() - 1);
+ }
+ }
+ else if (i == 10) {
+ if (attempt == passcode) {
+ passcodeCheck = true;
+ Display.gfx_Cls();
+ Display.txt_FGcolour(YELLOW);
+ Display.println("Correct");
+ Serial.println("Correct Passcode");
+ partone = true;
+ return;
+ } else {
+ Serial.println("Wrong Passcode");
+ Display.println("Wrong Passcode");
+ }
+ }
+ else {
+ attempt += String(i);
+ }
+ Serial.println(attempt);
+ Display.gfx_Cls();
+ Display.println(attempt);
+ }
+ }
+ lasttouched = currtouched;
+}
+void checkTilt() {
+ String lastTilt = "";
+ if (point->dataReady()) {
+ point->getAGMT();
+ } else {
+ return; // no new data
+ }
+
+ String currentTilt = "";
+
+ // Detect tilt direction
+ if(point->accX() < -500) {
+ currentTilt = "Left";
+ }
+ else if(point->accX() > 500) {
+ currentTilt = "Right";
+ }
+ else if(point->accY() < -500) {
+ currentTilt = "Down";
+ }
+ else if(point->accY() > 500) {
+ currentTilt = "Up";
+ }
+ delay(500);
+
+ // Only do something if tilt changed and is valid
+ if (currentTilt.length() > 0 && currentTilt != lastTilt) {
+ tiltAttempt += currentTilt;
+ count++;
+ lastTilt = currentTilt;
+
+ Serial.println("Tilt sequence so far: " + tiltAttempt);
+ Display.gfx_Cls();
+ Display.println(tiltAttempt);
+ drawArrow(currentTilt);
+ delay(100);
+
+ }
+
+ // Reset lastTilt when neutral (so repeats are possible again)
+ if (currentTilt == "") {
+ lastTilt = "";
+ }
+
+ // Check if we have 3 tilts
+ if (count == 3) {
+ if (tiltAttempt == tiltcode) {
+ tiltcodeCheck = true;
+ Serial.println("Correct tiltcode");
+ Display.gfx_Cls();
+ Display.println("Correct tiltcode");
+ parttwo = true;
+ } else {
+ Serial.println("Incorrect tiltcode");
+ Display.gfx_Cls();
+ Display.println("Incorrect tiltcode");
+ // Reset for next try
+ tiltAttempt = "";
+ count = 0;
+ lastTilt = "";
+ }
+ }
+}
+void drawArrow(String dir) {
+ if(dir == "Up") {
+ Display.gfx_Triangle(64, 34, 54, 54, 74, 54, WHITE);
+ Display.gfx_Rectangle(59, 54, 69, 84, WHITE);
+ }
+ else if(dir == "Down") {
+ Display.gfx_Triangle(64, 94, 54, 74, 74, 74, WHITE);
+ Display.gfx_Rectangle(59, 44, 69, 74, WHITE);
+ }
+ else if(dir == "Left") {
+ Display.gfx_Triangle(34, 64, 54, 54, 54, 74, WHITE);
+ Display.gfx_Rectangle(54, 59, 84, 69, WHITE);
+ }
+ else if(dir == "Right") {
+ Display.gfx_Triangle(94, 64, 74, 54, 74, 74, WHITE);
+ Display.gfx_Rectangle(44, 59, 74, 69, WHITE);
+ }
+ else {}
+}
+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("========================================");