// Fixed Touch Pad - Pull-up version (Ardra PCB) // Touch pulls pin LOW → delta goes NEGATIVE when touched #define TOUCH_PIN PIN_PA4 #define NUM_SAMPLES 10 #define DELTA_THRESH 40 // Touch = delta drops BELOW negative of this #define DRIFT_SPEED 0.02 float baseline = 0; long measureOnce() { long count = 0; pinMode(TOUCH_PIN, OUTPUT); digitalWrite(TOUCH_PIN, LOW); delayMicroseconds(10); pinMode(TOUCH_PIN, INPUT); while (digitalRead(TOUCH_PIN) == LOW && count < 5000) count++; return count; } float measureAvg() { long total = 0; for (int i = 0; i < NUM_SAMPLES; i++) { total += measureOnce(); delayMicroseconds(200); } return (float)total / NUM_SAMPLES; } void setup() { Serial.begin(115200); delay(500); // Calibration long cal = 0; for (int i = 0; i < 50; i++) { cal += measureOnce(); delay(10); } baseline = (float)cal / 50.0; } void loop() { float reading = measureAvg(); float delta = reading - baseline; // Serial Plotter friendly output (3 channels) Serial.print(reading); Serial.print(" "); Serial.print(baseline); Serial.print(" "); Serial.println(delta); // Touch detection if (delta < -DELTA_THRESH) { // Optional: you can toggle an LED or do something here } else { // Drift correction only when not touched baseline = baseline + (reading - baseline) * DRIFT_SPEED; } delay(50); }