diff options
author | dakkar <dakkar@thenautilus.net> | 2020-08-22 14:47:20 +0100 |
---|---|---|
committer | dakkar <dakkar@thenautilus.net> | 2020-08-22 14:47:20 +0100 |
commit | d43141350ca5255eb5af00e57a6afb75d41521b0 (patch) | |
tree | 6d133c1198dece75981798a2c42efcacc9dfdb26 | |
parent | different wiring (diff) | |
download | lego-piano-d43141350ca5255eb5af00e57a6afb75d41521b0.tar.gz lego-piano-d43141350ca5255eb5af00e57a6afb75d41521b0.tar.bz2 lego-piano-d43141350ca5255eb5af00e57a6afb75d41521b0.zip |
generalise the code a bit
-rw-r--r-- | esp32/lego-piano.ino | 62 |
1 files changed, 39 insertions, 23 deletions
diff --git a/esp32/lego-piano.ino b/esp32/lego-piano.ino index 4a88556..29368e6 100644 --- a/esp32/lego-piano.ino +++ b/esp32/lego-piano.ino @@ -19,13 +19,19 @@ #include <driver/dac.h> int currentLed = 0; -int lastSeen = 100; +int lastSeen = -1; -const int rows[] = { 5, 23, 19, 18, 26 }; -const int cols[] = { 7, 33, 8, 21, 22 }; -const int adc[] = { 2, 4, 12, 27, 14 }; +#define DEBUG 0 + +const size_t row_count = 5; +const size_t col_count = 5; + +const int rows[row_count] = { 5, 23, 19, 18, 26 }; +const int cols[col_count] = { 7, 33, 8, 21, 22 }; +const int adc[row_count] = { 2, 4, 12, 27, 14 }; const int ampEnable = 32; +const int octave_shift = 4; void setup() { Serial.begin(115200); @@ -36,15 +42,16 @@ void setup() { dac_i2s_disable(); dac_output_enable(DAC_CHANNEL_1); - for (int i=0;i<5;++i) { + for (int i=0;i<row_count;++i) { pinMode(rows[i], OUTPUT|PULLUP); - pinMode(cols[i], OUTPUT|PULLDOWN); pinMode(adc[i], INPUT); } + for (int i=0;i<col_count;++i) { + pinMode(cols[i], OUTPUT|PULLDOWN); + } currentLed = 0; - lastSeen = 100; - + lastSeen = -1; } void play(uint32_t freq) { @@ -52,7 +59,7 @@ void play(uint32_t freq) { en_ch: DAC_CHANNEL_1, scale: DAC_CW_SCALE_2, phase: DAC_CW_PHASE_0, - freq: freq, + freq: octave_shift*freq, offset: 0, }; @@ -84,28 +91,35 @@ void ground(int pin) { } 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) { + int row = (led/col_count) % row_count ; + int col = led%col_count; + +#if DEBUG & 0x02 + Serial.print("enabling "); + Serial.print(row); + Serial.print(" "); + Serial.println(col); +#endif + + for (int i=0;i<row_count;++i) { if (i==row) { power(rows[i]); } else { ground(rows[i]); } - + } + for (int i=0;i<col_count;++i) { if (i==col) { ground(cols[i]); } else { tristate(cols[i]); } } } int sense(int led) { - int row = led/5; + int row = (led/col_count)%row_count; int value = analogRead(adc[row]); +#if DEBUG & 0x04 Serial.println(value); +#endif + return value < 500; } @@ -140,8 +154,10 @@ uint32_t notes[] = { }; void loop() { - /* Serial.print("current led "); */ - /* Serial.println(currentLed); */ +#if DEBUG & 0x01 + Serial.print("current led "); + Serial.println(currentLed); +#endif enableLed(currentLed); delay(5); @@ -154,9 +170,9 @@ void loop() { } } else if (lastSeen == currentLed) { - lastSeen = 100; + lastSeen = -1; mute(); } - currentLed = (currentLed+1)%10; + currentLed = (currentLed+1)%(row_count*col_count); }
\ No newline at end of file |