-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArduino_Code.ino
115 lines (93 loc) · 2.85 KB
/
Arduino_Code.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#define trigPin 6 //Define the HC-SE04 trigger on pin 6 on the arduino
#define echoPin 5 //Define the HC-SE04 echo on pin 5 on the arduino
#include<SoftwareSerial.h>
SoftwareSerial BT(10, 11); // Connect Tx to pin 10 and Rx to pin 11 of HC-05/HC-06 Bluetooth Module
String readData; // String for storing data send from the Bluetooth device
int relay = 7; //From Relay module to pin 7 of Arduino
bool listenToBluetooth = false;
int val2 = 0;
void setup()
{
BT.begin(9600);
Serial.begin(9600);
pinMode(relay, OUTPUT);
digitalWrite(relay, LOW);
//Start the serial monitor
pinMode(6, OUTPUT); //set the trigpin to output
pinMode(5, INPUT); //set the echopin to input
}
void loop()
{
int duration, distance; //Define two intregers duration and distance to save data
digitalWrite(6, HIGH); //write a digital high to the trigpin to send out the pulse
delay(50); //wait half a millisecond
digitalWrite(6, LOW); //turn off the trigpin
duration = pulseIn(5, HIGH); //measure the time using pulsein when the echo receives a signal set it to high
distance = (duration / 2) / 29.1; //distance is the duration divided by 2
//Read data coming from HC-04 Bluetooth Module
while (BT.available())
{
delay(50);
char c = BT.read();
readData += c;
int idx = readData.indexOf("L");
if (idx != -1) {
readData = readData.substring(idx, 3);
}
}
if (readData.length() > 0)
{
Serial.println(readData);
if (readData == "H" || readData == "HIGH" || readData == "turn on" || readData == "switch on")
{
val2 = digitalRead(relay);
if (val2 == 0)
{
digitalWrite(relay, HIGH);
val2 = 1;
Serial.println("I switched it on");
}
listenToBluetooth = true;
delay(200);
}
else if (readData == "L" || readData == "LOW" || readData.equalsIgnoreCase("turn off") || readData == "switch off")
{
val2 = digitalRead(relay);
if (val2 == 1)
{
digitalWrite(relay, LOW);
val2 = 0;
Serial.println("I switched it off");
}
// else
// {
// digitalWrite(relay, HIGH);
// val2 = 0;
// }
listenToBluetooth = true;
delay(200);
}
readData = "";
}
Serial.println(distance);
while (distance > 0 )
{
if (listenToBluetooth) {
break;
}
if (distance < 200 )
{
digitalWrite(7, HIGH);
}
else
{
digitalWrite(7, LOW);
}
Serial.println(distance); //Dispaly the distance on the serial monitor
// Serial.println(" CM"); //in centimeters
delay(100);
break;//execute the Light subroutine below
}
// listenToBluetooth = false;
//delay half a second
}