#include #include #include #include #include // --- ePaper pins --- #define EPD_CS 2 #define EPD_DC 3 #define EPD_RST 4 #define EPD_BUSY 43 // --- accelerometer pin --- #define LED_PIN 1 // --- display object --- GxEPD2_BW display(GxEPD2_290_T94(EPD_CS, EPD_DC, EPD_RST, EPD_BUSY)); // --- accelerometer object --- TwoWire I2CBus = TwoWire(0); Adafruit_ADXL343 accel = Adafruit_ADXL343(12345, &I2CBus); // --- previous accelerometer readings --- float lastX = 0, lastY = 0, lastZ = 0; const float threshold = 2.5; // --- state --- bool triggered = false; unsigned long triggerTime = 0; const unsigned long displayTimeout = 5000; // 5 seconds before reverting void setup() { pinMode(LED_PIN, OUTPUT); Serial.begin(115200); delay(200); // Initialize SPI and ePaper SPI.begin(7, -1, 9, EPD_CS); display.init(115200, true, 2, false); display.setRotation(1); display.setTextColor(GxEPD_BLACK); display.setTextSize(2); display.setFullWindow(); drawMessage("Hello ePaper!"); // Initialize accelerometer I2CBus.begin(5, 6); if (!accel.begin(0x1D)) { Serial.println("No ADXL343 detected!"); while (1); } sensors_event_t event; accel.getEvent(&event); lastX = event.acceleration.x; lastY = event.acceleration.y; lastZ = event.acceleration.z; Serial.println("Setup complete."); } void loop() { sensors_event_t event; accel.getEvent(&event); float dx = abs(event.acceleration.x - lastX); float dy = abs(event.acceleration.y - lastY); float dz = abs(event.acceleration.z - lastZ); float movement = dx + dy + dz; if (!triggered && movement > threshold) { Serial.println("Movement detected! Showing warning..."); digitalWrite(LED_PIN, HIGH); delay(50); digitalWrite(LED_PIN, LOW); drawMessage("Why did you touch me?"); triggered = true; triggerTime = millis(); } // Check if timeout passed to revert back to hello if (triggered && (millis() - triggerTime >= displayTimeout)) { drawMessage("Hello ePaper!"); triggered = false; } lastX = event.acceleration.x; lastY = event.acceleration.y; lastZ = event.acceleration.z; delay(50); } // --- helper function --- void drawMessage(const char* msg) { display.firstPage(); do { display.fillScreen(GxEPD_WHITE); int16_t tbx, tby; uint16_t tbw, tbh; display.getTextBounds(msg, 0, 0, &tbx, &tby, &tbw, &tbh); int16_t x = ((display.width() - tbw) / 2) - tbx; int16_t y = ((display.height() - tbh) / 2) - tby; display.setCursor(x, y); display.print(msg); } while (display.nextPage()); delay(500); }