From ae94deaa166e05612a349853f5724953806361eb Mon Sep 17 00:00:00 2001 From: dakkar Date: Sun, 16 Aug 2020 11:41:47 +0100 Subject: matrix scanning --- Makefile | 12 ++++++++++ lego-piano.ino | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 Makefile create mode 100644 lego-piano.ino diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a7d095c --- /dev/null +++ b/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/lego-piano.ino b/lego-piano.ino new file mode 100644 index 0000000..4923159 --- /dev/null +++ b/lego-piano.ino @@ -0,0 +1,69 @@ +/* + matrix-scan a set of LEDs + + pins: + + "rows" + 12 & 13 go to a 220Ω resistor, then to 2 LEDs each (positive / long stem side) + + "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); + 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 enableRowCol(int row, int col) { + 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); + } +} + +void loop() { + Serial.print("current led "); + Serial.println(currentLed); + enableRowCol(currentLed/2,currentLed%2); + currentLed = (currentLed+1)%4; + delay(10); +} \ No newline at end of file -- cgit v1.2.3