-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.c
52 lines (43 loc) · 1.32 KB
/
code.c
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
int sensors[4];
int low[4] = {1000, 1000, 1000, 1000};
int high[4]= {0, 0, 0, 0};
int ports[4] = {A0, A1, A2, A3}; // data and ports stored in arrays for easy traversal
const int ledPin = 13;
void setup()
{
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH); // use onboard LED as an indicator
for(int i = 0; i < 4; i++){ // calibrate each phototransistor based on current state of the environment
while(millis() < 5000*(i+1)) {
sensors[i] = analogRead(ports[i]);
if(sensors[i] > high[i])
high[i] = sensors[i];
else if(sensors[i] < low[i])
low[i] = sensors[i];
}
}
digitalWrite(ledPin,LOW);
}
void loop()
{
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
int combination = 0;
int bit = 0;
for(int i = 0; i < 4; i++){ // acquire the state of each phototransistor
sensors[i] = analogRead(ports[i]);
if(sensors[i] <= (low[i] + high[i])/2){ // convert analog input to digital
bit = 1;
digitalWrite(5 - i, HIGH); // light up the associated LED
} else
bit = 0;
combination += (bit << i); // use bit manipulation to get the combination
}
if(combination != 0){ // if any key is pressed
int pitch = combination*100;
tone(8, pitch, 10); // generate the corresponding tone
}
delay(10);
}