//Fab Academy 2026 - Fab Lab León //Touch + LED //Fab-Xiao // #define LED 0 #define N_TOUCH 2 #define THRESHOLD 2 int touch_pins[N_TOUCH] = {27, 26}; int touch_values[N_TOUCH] = {0}; int touch_mapped[N_TOUCH] = {0}; bool pin_touched_now[N_TOUCH] = {false}; void update_touch() { int t; int t_max = 200; int p; for (int i = 0; i < N_TOUCH; i++) { p = touch_pins[i]; pinMode(p, OUTPUT); digitalWriteFast(p, LOW); delayMicroseconds(25); pinMode(p, INPUT_PULLUP); t = 0; while (!digitalReadFast(p) && t < t_max) { t++; } touch_values[i] = t; // Mapear a 0–255 touch_mapped[i] = map(touch_values[i], 0, t_max, 0, 255); pin_touched_now[i] = touch_values[i] > THRESHOLD; } } void setup() { Serial.begin(115200); pinMode(LED, OUTPUT); digitalWrite(LED, HIGH); // LED apagado } void loop() { update_touch(); // Control LED con pin 26 (índice 1) if (pin_touched_now[1]) { digitalWrite(LED, HIGH); // encendido } else { digitalWrite(LED, LOW); // apagado } // Mostrar valores en Serial Serial.print("Touch 27: "); Serial.print(touch_mapped[0]); Serial.print(" | Touch 26: "); Serial.println(touch_mapped[1]); delay(50); }