-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArduinoPiano.ino
61 lines (50 loc) · 1.32 KB
/
ArduinoPiano.ino
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
int triggerPin = 11;
int echoPin = 10;
long duration, distance;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
//pin designations
pinMode(echoPin, INPUT);
pinMode(triggerPin, OUTPUT);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, LOW);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
//get travel time by playing and receiving sound
duration = pulseIn(echoPin, HIGH);
distance = MicrosecondsToCentimeters(duration);
Serial.print("Distance : ");
Serial.println(distance);
// at distances less than 20, 15 and 10cm, send 8.1 volts (91.5) to pins 3, 5 and 7 respectively to turn the LED lights on
if(distance < 20){
analogWrite(3, 91.5);
} else {
analogWrite(3,0);
}
if(distance < 15){
//3 pin and 8.1v
analogWrite(5, 91.5);
} else {
analogWrite(5,0);
}
if(distance < 10){
//3 pin and 8.1v
analogWrite(6, 91.5);
} else {
analogWrite(6,0);
}
}
long MicrosecondsToCentimeters(long duration){
//speed of sound is 0.0343 cm/ms)
// divide by 2 to compensate for time taken to travel to and from destination
long d = (duration / 2) * 0.0343;
return d;
}