aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordakkar <dakkar@thenautilus.net>2020-08-20 12:04:47 +0100
committerdakkar <dakkar@thenautilus.net>2020-08-20 12:04:47 +0100
commit90fb5981770fb75a4d3e7eefed28eaf47a8730ab (patch)
tree6ce13f7c8a9a54f8a5095f45880273b05e0a7607
parentmatrix scanning (diff)
downloadlego-piano-90fb5981770fb75a4d3e7eefed28eaf47a8730ab.tar.gz
lego-piano-90fb5981770fb75a4d3e7eefed28eaf47a8730ab.tar.bz2
lego-piano-90fb5981770fb75a4d3e7eefed28eaf47a8730ab.zip
input works!
-rw-r--r--lego-piano.ino46
1 files changed, 35 insertions, 11 deletions
diff --git a/lego-piano.ino b/lego-piano.ino
index 4923159..81f75c9 100644
--- a/lego-piano.ino
+++ b/lego-piano.ino
@@ -3,8 +3,12 @@
pins:
- "rows"
- 12 & 13 go to a 220Ω resistor, then to 2 LEDs each (positive / long stem side)
+ "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)
@@ -20,6 +24,8 @@ void setup() {
pinMode(13, OUTPUT);
pinMode(14, OUTPUT);
pinMode(15, OUTPUT);
+ pinMode(A1, INPUT);
+ pinMode(A2, INPUT);
currentLed = 0;
}
@@ -39,11 +45,13 @@ void ground(int pin) {
digitalWrite(pin,LOW);
}
-void enableRowCol(int row, int col) {
- Serial.print("enabling ");
- Serial.print(row);
- Serial.print(" ");
- Serial.println(col);
+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);
@@ -60,10 +68,26 @@ void enableRowCol(int row, int col) {
}
}
+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);
- enableRowCol(currentLed/2,currentLed%2);
+ /* 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(10);
+ delay(20);
} \ No newline at end of file