diff options
author | dakkar <dakkar@thenautilus.net> | 2020-08-20 12:15:10 +0100 |
---|---|---|
committer | dakkar <dakkar@thenautilus.net> | 2020-08-20 12:15:10 +0100 |
commit | 1f3306845df9e2ed01fd81af438c694606431ef7 (patch) | |
tree | 355df6df63239c8bab60d70880102f300b20efde | |
parent | schematics (diff) | |
parent | input works! (diff) | |
download | lego-piano-1f3306845df9e2ed01fd81af438c694606431ef7.tar.gz lego-piano-1f3306845df9e2ed01fd81af438c694606431ef7.tar.bz2 lego-piano-1f3306845df9e2ed01fd81af438c694606431ef7.zip |
Add 'esp32/' from commit '90fb5981770fb75a4d3e7eefed28eaf47a8730ab'
git-subtree-dir: esp32
git-subtree-mainline: 4704c406483377353cfbbcb86dec742c23bbf6c9
git-subtree-split: 90fb5981770fb75a4d3e7eefed28eaf47a8730ab
-rw-r--r-- | esp32/Makefile | 12 | ||||
-rw-r--r-- | esp32/lego-piano.ino | 93 |
2 files changed, 105 insertions, 0 deletions
diff --git a/esp32/Makefile b/esp32/Makefile new file mode 100644 index 0000000..a7d095c --- /dev/null +++ b/esp32/Makefile @@ -0,0 +1,12 @@ +SKETCH = lego-piano.ino + +UPLOAD_PORT = /dev/ttyUSB0 +# this is the board with the OLED +#BOARD = lolin32 +# this is the more compact board with TTGO written on it +BOARD = featheresp32 + +ESP_ROOT = $(HOME)/HW/arduino-esp32 +CHIP = esp32 + +include $(HOME)/src/makeEspArduino/makeEspArduino.mk diff --git a/esp32/lego-piano.ino b/esp32/lego-piano.ino new file mode 100644 index 0000000..81f75c9 --- /dev/null +++ b/esp32/lego-piano.ino @@ -0,0 +1,93 @@ +/* + 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 + */ + +int currentLed = 0; + +void setup() { + Serial.begin(19200); + pinMode(12, OUTPUT); + pinMode(13, OUTPUT); + pinMode(14, OUTPUT); + pinMode(15, OUTPUT); + pinMode(A1, INPUT); + pinMode(A2, INPUT); + currentLed = 0; +} + +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/2; + int col = led%2; + /* Serial.print("enabling "); */ + /* Serial.print(row); */ + /* Serial.print(" "); */ + /* Serial.println(col); */ + + if (row==0) { + power(12); ground(13); + } + else { + ground(12); power(13); + } + + if (col==0) { + ground(14); tristate(15); + } + else { + tristate(14); ground(15); + } +} + +int sense(int led) { + int row = led/2; + + int value = analogRead(row == 0 ? A1 : A2); + + //Serial.println(value); + return value < 500; +} + +void loop() { + /* Serial.print("current led "); */ + /* Serial.println(currentLed); */ + + enableLed(currentLed); + delay(20); + if (sense(currentLed)) { + Serial.print(currentLed); + Serial.println(" proximity!"); + } + + currentLed = (currentLed+1)%4; + delay(20); +}
\ No newline at end of file |