forked from illale/alarm-robot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
digifab.ino
51 lines (44 loc) · 1.07 KB
/
digifab.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
const int echoPin = 13;
const int trigPin = 12;
const int tiltPin = 11;
const int ledPin = 10;
const int audPin = 3;
int tiltState = 0;
int previousTiltState = 0;
void setup() {
Serial.begin(9600);
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
pinMode(tiltPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(audPin, OUTPUT);
}
void loop() {
long distance;
distance = getDistance();
Serial.print(distance);
Serial.print(" cm \n");
tiltState = digitalRead(tiltPin);
if (tiltState != previousTiltState) {
digitalWrite(ledPin, LOW);
} else {
digitalWrite(ledPin, HIGH);
tone(audPin, 1000);
}
}
long getDistance() {
long duration, distance;
// Cleares the trigger signal
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Tells the sensor to trigger
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Reads the value coming form echo signal
duration = pulseIn(echoPin, HIGH);
// Turns the distance into cm's and returns it
distance = duration*0.034/2;
return distance;
}