-
Notifications
You must be signed in to change notification settings - Fork 1
/
ihcode
44 lines (42 loc) · 760 Bytes
/
ihcode
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
void setup()
{
pinMode (2,OUTPUT);
pinMode (5,OUTPUT);
pinMode (8,OUTPUT);
}
void loop()
{
digitalWrite(2, HIGH);
digitalWrite(8, HIGH);
long duration, inches, cm;
pinMode(3, OUTPUT);
digitalWrite(3, LOW);
delayMicroseconds(2);
digitalWrite(3, HIGH);
delayMicroseconds(5);
digitalWrite(3, LOW);
pinMode (4, INPUT);
duration = pulseIn(4, HIGH);
inches = microsecondsToInches(duration);
cm = inches*2.54 ;
if (cm < 10 )
{
analogWrite(6,255);
}
else if (cm < 50)
{
analogWrite(6,200);
}
else if (cm < 100)
{
analogWrite(6,150);
}
else
{
analogWrite(6,0);
}
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}