-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTWD_01 .ino
52 lines (32 loc) · 1.01 KB
/
TWD_01 .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
const int trigPin = 9;
const int echoPin = 10;
const int LED = 13;
const int LED2 = 12;
long duration;
int distance;
void setup() {
pinMode (trigPin,OUTPUT);
pinMode (echoPin,INPUT);
pinMode (LED,OUTPUT);
pinMode (LED2,OUTPUT);
Serial.begin (9600);
}
void loop() {
digitalWrite (trigPin, LOW);
delayMicroseconds(2);
digitalWrite (trigPin, HIGH);
delayMicroseconds(10);
digitalWrite (trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration*0.034/2;
Serial.print ("Distance: ");
Serial.println (distance);
if ( distance < 300 ) {
digitalWrite( LED, HIGH );
digitalWrite( LED2, LOW ); }
// 300cm is only for testing, a more proper distance should be determined
else {
digitalWrite ( LED, LOW );
digitalWrite ( LED2, HIGH ); }
// sketch for buzzer has not been added yet. When distance remains <threshold for 10 seconds, the buzzer would go on and off until the distance becomes <= threshold.
}