-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommunicationbluetoothbluetoothtest.ino
178 lines (135 loc) · 3.79 KB
/
communicationbluetoothbluetoothtest.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <ArduinoJson.h>
#include <SoftwareSerial.h>
/* Example code for HC-SR04 ultrasonic distance sensor with Arduino. No library required. More info: https://www.makerguides.com */
// Define Trig and Echo pin:
#define trigPin1 A2
#define echoPin1 A3
#define trigPin2 4
#define echoPin2 5
#define trigPin3 6
#define echoPin3 7
// Define variables:
long duration1;
int distance1;
bool etat1;
long duration2;
int distance2;
bool etat2;
long duration3;
int distance3;
bool etat3;
int redPin1 = 8;
int greenPin1 = 9;
int redPin2 = 10;
int greenPin2 = 11;
int redPin3 = 12;
int greenPin3 = 13;
StaticJsonDocument<200> doc;
void setup() {
// Define inputs and outputs:
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(trigPin3, OUTPUT);
pinMode(echoPin3, INPUT);
//Begin Serial communication at a baudrate of 9600:
Serial.begin(9600);
pinMode(redPin1, OUTPUT);
pinMode(greenPin1, OUTPUT);
pinMode(redPin2, OUTPUT);
pinMode(greenPin2, OUTPUT);
pinMode(redPin3, OUTPUT);
pinMode(greenPin3, OUTPUT);
Serial.begin(38400);
}
void loop() {
// Clear the trigPin by setting it LOW:
digitalWrite(trigPin2, LOW);
delayMicroseconds(5);
// Trigger the sensor by setting the trigPin high for 10 microseconds:
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
// Read the echoPin, pulseIn() returns the duration (length of the pulse) in microseconds:
duration2 = pulseIn(echoPin2, HIGH);
// Calculate the distance:
distance2= duration2*0.034/2.0;
// Print the distance on the Serial Monitor (Ctrl+Shift+M):
//Serial.print("Distance = ");
//Serial.print(distance2);
//Serial.println(" cm");
delay(100);
digitalWrite(trigPin1, LOW);
delayMicroseconds(5);
// Trigger the sensor by setting the trigPin high for 10 microseconds:
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
// Read the echoPin, pulseIn() returns the duration (length of the pulse) in microseconds:
duration1 = pulseIn(echoPin1, HIGH);
// Calculate the distance:
distance1= duration1*0.034/2.0;
//Serial.print("Distance1 = ");
//Serial.print(distance1);
//Serial.println(" cm");
delay(100);
// Clear the trigPin by setting it LOW:
digitalWrite(trigPin3, LOW);
delayMicroseconds(5);
// Trigger the sensor by setting the trigPin high for 10 microseconds:
digitalWrite(trigPin3, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin3, LOW);
// Read the echoPin, pulseIn() returns the duration (length of the pulse) in microseconds:
duration3 = pulseIn(echoPin3, HIGH);
// Calculate the distance:
distance3= duration3*0.034/2.0;
// Print the distance on the Serial Monitor (Ctrl+Shift+M):
//Serial.print("Distance3 = ");
//Serial.print(distance3);
//Serial.println(" cm");
delay(50);
if (distance1>20){
digitalWrite(redPin1,HIGH);
digitalWrite(greenPin1,LOW);
etat1 = 0;
}
else {
digitalWrite(redPin1,LOW);
digitalWrite(greenPin1,HIGH);
etat1 = 1;
}
if (distance2<20){
digitalWrite(redPin2,LOW);
digitalWrite(greenPin2,HIGH);
etat3 = 1;
}
else {
digitalWrite(redPin2,HIGH);
digitalWrite(greenPin2,LOW);
etat3 = 0;
}
if (distance3>20){
digitalWrite(redPin3,HIGH);
digitalWrite(greenPin3,LOW);
etat2 = 0;
}
else {
digitalWrite(redPin3,LOW);
digitalWrite(greenPin3,HIGH);
etat2 = 1;
}
Serial.write(2);
Serial.write(etat1);
delay(10);
Serial.write(4);
Serial.write(etat2);
delay(10);
Serial.write(6);
Serial.write(etat3);
delay(100);
//Serial.println(etat1);
//Serial.println(etat2);
//Serial.println(etat3);
}