#include #include #include #include #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define SCREEN_ADDRESS 0x3C Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1, 1700000UL, 1700000UL); #define PIN_LED 15 #define N_TOUCH 5 // pad2 = start, others = tones #define THRESHOLD 500 uint8_t touch_pins[N_TOUCH] = {2, 4, 5, 6, 7}; Adafruit_FreeTouch* touch_devices[N_TOUCH]; bool pin_touched_now[N_TOUCH] = {false, false, false, false, false}; bool pin_touched_past[N_TOUCH] = {false, false, false, false, false}; const char* fortune_labels[N_TOUCH] = { "start", // pin 2 "optimistic", // pin 4 "wise", // pin 5 "mean", // pin 6 "mysterious" // pin 7 }; String lastFortune = ""; String todayDate = ""; void update_touch() { Ptc *ptc = ((Ptc *)PTC); for (int i = 0; i < N_TOUCH; i++) { touch_devices[i]->begin(); int val = touch_devices[i]->measure(); ptc->CTRLA.bit.ENABLE = 0; ptc->CTRLA.bit.SWRESET = 1; pin_touched_past[i] = pin_touched_now[i]; pin_touched_now[i] = val > THRESHOLD; } } void showMessage(String msg) { display.clearDisplay(); display.setCursor(0, 10); display.setTextSize(1); display.setTextColor(SSD1306_WHITE); display.println(msg); display.display(); } void setup() { Serial.begin(9600); while (!Serial) {} for (int i = 0; i < N_TOUCH; i++) { touch_devices[i] = new Adafruit_FreeTouch( touch_pins[i], OVERSAMPLE_1, RESISTOR_100K, FREQ_MODE_NONE ); } pinMode(PIN_LED, OUTPUT); digitalWrite(PIN_LED, LOW); if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1306 allocation failed")); for (;;); } display.clearDisplay(); display.setTextSize(1); display.setTextColor(SSD1306_WHITE); display.setCursor(10, 25); display.println("Ready"); display.display(); Serial.println("Fortune Teller Ready"); } void loop() { update_touch(); for (int i = 0; i < N_TOUCH; i++) { if (pin_touched_now[i] && !pin_touched_past[i]) { String tag = fortune_labels[i]; if (tag == "start") { Serial.println("request:initial"); } else if (lastFortune.length() > 0) { Serial.print("request:tone:"); Serial.print(tag); Serial.print("::"); Serial.println(lastFortune); } } } // Receive responses from Python if (Serial.available()) { String response = Serial.readStringUntil('\n'); response.trim(); if (response.startsWith("date::")) { todayDate = response.substring(6); // strip "date::" showMessage(todayDate + "\nToday's Fortune"); } else if (response.startsWith("fortune::")) { lastFortune = response.substring(9); // strip "fortune::" showMessage(todayDate + "\n" + lastFortune); } } delay(100); }