-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduino_theremin_all_leds.ino
70 lines (64 loc) · 1.45 KB
/
arduino_theremin_all_leds.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
62
63
64
65
66
67
68
69
70
// Arduino Theremin!
// by Smitha Pisupati
// Last update: November 2015
// arduino_theremin_all_led.ino
int trigger = 4;
int echo = 5;
int led1 = 13;
int led2 = 12;
int led3 = 11;
int led4 = 10;
int led5 = 9;
int led6 = 8;
int led7 = 7;
unsigned long count = 0;
unsigned long distanceInCm = 0;
void setup()
{
pinMode (trigger, OUTPUT);
pinMode(echo, INPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(led6, OUTPUT);
pinMode(led7, OUTPUT);
digitalWrite(trigger, LOW);
Serial.begin(9600);
}
void loop()
{
// Hold trigger pin high for 10 microseconds
digitalWrite(trigger, HIGH);
delayMicroseconds(10);
digitalWrite(trigger, LOW);
// Get the width of the pulse with pulseIn
count = pulseIn(echo, HIGH);
// get distance in cm. Divide by 148 for inches
distanceInCm = count / 58;
Serial.println(distanceInCm);
if((distanceInCm > 4) && (distanceInCm < 25))
{
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
digitalWrite(led5, HIGH);
digitalWrite(led6, HIGH);
digitalWrite(led7, HIGH);
}
else
{
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
digitalWrite(led6, LOW);
digitalWrite(led7, LOW);
}
count = 0;
// add a delay for stable behaviour
delay(200);
}