/* * Touch Sensor Sargam Instrument - Barduino * Fab Academy 2026 - Week 4: Embedded Programming * Shivangi Pande | Fab Lab Barcelona * * Maps 6 capacitative touch pads on the Barduino * to Hindustani swaras (Sa Re Ga Ma Pa Ni). * Touch a pad → LED lights up + buzzer plays the note. * * Uses adaptive baseline calibration: * On boot, reads each sensor's "resting" value. * A touch is detected when the reading exceeds * baseline + threshold (50000). * When no touch is detected, baselines slowly drift * with an exponential moving average to handle * temperature/humidity changes. * * Pin mapping: * Pin 1 → Ni (491 Hz) * Pin 2 → Pa (392 Hz) * Pin 4 → Sa (262 Hz) * Pin 5 → Re (294 Hz) * Pin 6 → Ga (327 Hz) * Pin 7 → Ma (349 Hz) */ #define LED_PIN 48 #define BUZZER_PIN 46 // Touch pins #define T_NI 1 #define T_PA 2 #define T_SA 4 #define T_RE 5 #define T_GA 6 #define T_MA 7 // Frequencies int NI = 491; int PA = 392; int SA = 262; int RE = 294; int GA = 327; int MA = 349; // Baselines int bNI, bPA, bSA, bRE, bGA, bMA; int threshold = 50000; void setup() { Serial.begin(115200); pinMode(LED_PIN, OUTPUT); pinMode(BUZZER_PIN, OUTPUT); delay(500); // IMPORTANT: no touching now bNI = touchRead(T_NI); bPA = touchRead(T_PA); bSA = touchRead(T_SA); bRE = touchRead(T_RE); bGA = touchRead(T_GA); bMA = touchRead(T_MA); } void loop() { int vNI = touchRead(T_NI); int vPA = touchRead(T_PA); int vSA = touchRead(T_SA); int vRE = touchRead(T_RE); int vGA = touchRead(T_GA); int vMA = touchRead(T_MA); if (vNI > bNI + threshold) { digitalWrite(LED_PIN, HIGH); tone(BUZZER_PIN, NI); } else if (vPA > bPA + threshold) { digitalWrite(LED_PIN, HIGH); tone(BUZZER_PIN, PA); } else if (vSA > bSA + threshold) { digitalWrite(LED_PIN, HIGH); tone(BUZZER_PIN, SA); } else if (vRE > bRE + threshold) { digitalWrite(LED_PIN, HIGH); tone(BUZZER_PIN, RE); } else if (vGA > bGA + threshold) { digitalWrite(LED_PIN, HIGH); tone(BUZZER_PIN, GA); } else if (vMA > bMA + threshold) { digitalWrite(LED_PIN, HIGH); tone(BUZZER_PIN, MA); } else { digitalWrite(LED_PIN, LOW); noTone(BUZZER_PIN); // adaptive baseline (no touch only) bNI = (bNI * 9 + vNI) / 10; bPA = (bPA * 9 + vPA) / 10; bSA = (bSA * 9 + vSA) / 10; bRE = (bRE * 9 + vRE) / 10; bGA = (bGA * 9 + vGA) / 10; bMA = (bMA * 9 + vMA) / 10; } delay(20); }