// New library-free version, prepared with Codex assistance; bench test pending.
#include <Arduino.h>
constexpr uint8_t TOUCH_PIN = D9;
constexpr uint8_t LED_PIN = D1;
constexpr bool TOUCH_ACTIVE_HIGH = true; // Check the actual TTP223 mode.
constexpr uint32_t STABLE_MS = 30;
bool candidate = false, touched = false;
uint32_t changedAt = 0;

void setup() {
  Serial.begin(115200);
  pinMode(TOUCH_PIN, INPUT);
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);
  Serial.println("Digital touch input; no capacitance measurement");
}

void loop() {
  const uint32_t now = millis();
  const bool raw = (digitalRead(TOUCH_PIN) == HIGH) == TOUCH_ACTIVE_HIGH;
  if (raw != candidate) { candidate = raw; changedAt = now; }
  if (candidate != touched && now-changedAt >= STABLE_MS) {
    touched = candidate;
    digitalWrite(LED_PIN, touched ? HIGH : LOW);
    Serial.println(touched ? "TOUCHED" : "RELEASED");
  }
}
