#include // After adding the code for checking the last touched pad, // there were not enough memory for the display, so I disabled // the code for the screen temporarily //#define SCREEN #ifdef SCREEN #include #define W 128 #define H 64 #define ADDR 0x3C Adafruit_SSD1306 display(W, H); #endif CapacitiveSensor pads[] = { { 2, 3 }, { 2, 4 }, { 2, 5 }, { 2, 6 }, { 2, 7 }, { 2, 8 }, { 2, 9 }, { 2, 10 }, { 2, 11 }, { 2, 12 }, }; #define array_len(a) (sizeof(a) / sizeof *(a)) void setup() { Serial.begin(9600); Serial.println(); pinMode(13, OUTPUT); #ifdef SCREEN if (!display.begin(SSD1306_SWITCHCAPVCC, ADDR)) { Serial.println(F("SSD1306 allocation failed")); for (;;); // Don't proceed, loop forever } Serial.println(F("Init display")); display.setTextColor(WHITE); display.setTextSize(2); #endif } unsigned long last_touch_time[array_len(pads)] = {}; void loop() { unsigned long start = millis(); int last_touched = -1; for (int i = 0; i < array_len(pads); ++i) { long val = pads[i].capacitiveSensor(30); Serial.print(i); Serial.print(": "); Serial.print(val); Serial.print("\t"); bool current = val > 200; if (current) { if (last_touch_time[i] == 0) { last_touch_time[i] = millis(); } if (last_touched < 0 || last_touch_time[last_touched] < last_touch_time[i]) { last_touched = i; } } else { last_touch_time[i] = 0; } } Serial.println(); if (last_touched < 0) { noTone(13); } else { tone(13, 300 + last_touched * 100); } unsigned long processing_time = millis() - start; Serial.print(F("Last touched: ")); Serial.println(last_touched); Serial.println(processing_time); #ifdef SCREEN display.clearDisplay(); display.setCursor(0, 0); if (last_touched < 0) { display.println(F("No touch")); } else { display.print(F("Touched ")); display.println(last_touched); } display.print(F("T:")); display.println(processing_time); display.display(); #endif }