aboutsummaryrefslogtreecommitdiff
path: root/esp32/lego-piano.ino
blob: 42c7e1a966da070dad6b13d223060e693a03c59e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
  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 <driver/dac.h>
 
int currentLed = 0;
int lastSeen = 100;
 
const int rows[] = { 523191826 };
const int cols[] = { 3433352122 };
const int adc[] = { 24122714 };
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;
}
 
int count=0;
uint32_t notes[] = { 4408801200 };
int currentNote = 0;
 
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!");
    }
  }
  else if (lastSeen == currentLed) {
    lastSeen = 100;
  }
 
  currentLed = (currentLed+1)%25;
 
  if (count == 450{
    //mute(); 
  }
  else if (count == 0{
    play(notes[currentNote]);
    currentNote = (currentNote+1)%3;
  }
  count = (count+1)%500;
}