-
Notifications
You must be signed in to change notification settings - Fork 0
/
SmartLighting.cpp
75 lines (71 loc) · 1.72 KB
/
SmartLighting.cpp
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
71
72
73
74
75
#include <Servo.h>
#define trigPin 12
#define echoPin 13
int IRSensor = 7;
int sound = 100;
int distance;
int buzzer = 11;
Servo servo_test;
Servo servo_test1;
int angle = 180;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzer, OUTPUT);
servo_test.attach(9);
servo_test1.attach(8);
pinMode(IRSensor, INPUT);
}
void loop() {
long duration, distance;
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);
irs();
if (distance < 10) {
tone(buzzer, sound);
//lcd
}
if (distance > 20) {
//tone(buzzer, sound);
Serial.print(distance);
Serial.println(" cm");
//lcd display
// if (distance >30)
// {
// serv();
//}
}
//else
//{
// noTone(buzzer);
//}
delay(500);
}
void serv() {
for (angle = 0; angle < 90; angle += 1) {
servo_test.write(angle);
servo_test1.write(angle);
delay(15);
}
delay(1000);
for (angle = 90; angle >= 1; angle -= 15) {
servo_test.write(angle);
servo_test1.write(angle);
delay(5);
}
}
void irs() {
int sensorStatus = digitalRead(IRSensor);
if (sensorStatus == 1) {
noTone(buzzer);
} else {
tone(buzzer, sound);
}
}