/* matrix-scan a set of LEDs pins: "rows" 12 & 13 go to a 220Ω resistor, then to 2 LEDs each (positive / long stem side); also to a 10kΩ resistor then to 2 phototransistors (collector / long stem side) also, from between the 10kΩ and the phototransistors, wire goes to A1 & A2 "columns" 14 & 15 go to 2 LEDs each (negative / short stem side) so that given one of 12|13 and one of 14|15, one LED is identified */ #include int currentLed = 0; int lastSeen = 100; const int rows[] = { 5, 23, 19, 18, 26 }; const int cols[] = { 34, 33, 35, 21, 22 }; const int adc[] = { 2, 4, 12, 27, 14 }; const int ampEnable = 32; void setup() { Serial.begin(115200); pinMode(ampEnable, OUTPUT); dac_i2s_disable(); dac_output_enable(DAC_CHANNEL_1); for (int i=0;i<5;++i) { pinMode(rows[i], OUTPUT); pinMode(cols[i], OUTPUT); pinMode(adc[i], INPUT); } currentLed = 0; lastSeen = 100; digitalWrite(ampEnable, LOW); } void play(uint32_t freq) { dac_cw_config_t wave_config = { en_ch: DAC_CHANNEL_1, scale: DAC_CW_SCALE_2, phase: DAC_CW_PHASE_0, freq: freq, offset: 0, }; dac_cw_generator_config(&wave_config); dac_cw_generator_enable(); digitalWrite(ampEnable, HIGH); } void mute() { digitalWrite(ampEnable, LOW); dac_cw_generator_disable(); } void tristate(int pin) { pinMode(pin,OUTPUT); digitalWrite(pin,LOW); pinMode(pin,INPUT); } void power(int pin) { pinMode(pin,OUTPUT); digitalWrite(pin,HIGH); } void ground(int pin) { pinMode(pin,OUTPUT); digitalWrite(pin,LOW); } void enableLed(int led) { int row = led/5; int col = led%5; /* Serial.print("enabling "); */ /* Serial.print(row); */ /* Serial.print(" "); */ /* Serial.println(col); */ for (int i=0;i<5;++i) { if (i==row) { power(rows[i]); } else { ground(rows[i]); } if (i==col) { ground(cols[i]); } else { tristate(cols[i]); } } } int sense(int led) { int row = led/5; int value = analogRead(adc[row]); //Serial.println(value); return value < 500; } uint32_t notes[] = { 261, // C4 277, 294, // D4 311, 330, // E4 349, // F4 370, 392, // G4 415, 440, // A4 466, 494, // B4 523, // C5 554, 587, // D5 622, 659, // E5 698, // F5 740, 783, // G5 831, 880, // A5 932, 988, // B5 1046, // C6 }; void loop() { /* Serial.print("current led "); */ /* Serial.println(currentLed); */ enableLed(currentLed); delay(1); if (sense(currentLed)) { if (lastSeen != currentLed) { lastSeen = currentLed; Serial.print(currentLed); Serial.println(" proximity!"); play(notes[currentLed]); } } else if (lastSeen == currentLed) { lastSeen = 100; mute(); } currentLed = (currentLed+1)%25; }