-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatient_2.ino
More file actions
174 lines (146 loc) · 3.9 KB
/
Patient_2.ino
File metadata and controls
174 lines (146 loc) · 3.9 KB
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
//SENDER
#include <esp_now.h>
#include <WiFi.h>
const char* ssid = "Toey";
const char* password = "99999997";
//FLEX-SENSOR
const int flex1 = A3;
const int flex2 = A0;
//LED
const int red_led = 19;
const int yellow_led = 22;
const int green_led = 21;
const int violet_led = 23;
//ULTRASONIC
const int trigPin1 = 2;
const int echoPin1 = 15;
const int trigPin2 = 0;
const int echoPin2 = 4;
int duration1;
int duration2;
//BUZZER
const int buzzer = 14;
uint8_t broadcastAddress[] = {0x4C, 0x11, 0xAE, 0xD3, 0xED, 0x34};
String success;
typedef struct struct_message {
int id;
int blue_led;
int green_led;
int yellow_led;
int violet_led;
int red_led;
int buzz;
} struct_message_send;
struct_message send_Data;
esp_now_peer_info_t peerInfo;
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.print("\r\nLast Packet Send Status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
if (status ==0){
success = "Delivery Success :)";
}
else{
success = "Delivery Fail :(";
}
Serial.println(">>>>>");
}
void setup() {
Serial.begin(115200);
//FLEX-SENSOR
pinMode(flex1, INPUT);
pinMode(flex2, INPUT);
//LED
pinMode(red_led, OUTPUT);
pinMode(yellow_led, OUTPUT);
pinMode(green_led, OUTPUT);
pinMode(violet_led, OUTPUT);
digitalWrite(red_led, LOW);
digitalWrite(yellow_led, LOW);
digitalWrite(green_led, LOW);
digitalWrite(violet_led, LOW);
//ULTRASONIC
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
//BUZZER
pinMode(buzzer, OUTPUT);
digitalWrite(buzzer, HIGH);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
esp_now_register_send_cb(OnDataSent);
esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
}
void loop() {
send_Data.id = 2;
//FLEX SENSOR
Serial.println("data1 =");
Serial.println(analogRead(flex1));
Serial.println("data2 =");
Serial.println(analogRead(flex2));
delay(1000);
if ((analogRead(flex1) < 600) && (analogRead(flex2) < 600)){
digitalWrite(violet_led, HIGH);
send_Data.violet_led = 1;
}else if ((analogRead(flex1) < 500) && (analogRead(flex2) > 800)){
digitalWrite(yellow_led, HIGH);
send_Data.yellow_led = 1;
}else if ((analogRead(flex1) > 800) && (analogRead(flex2) < 600)){
digitalWrite(green_led, HIGH);
send_Data.green_led = 1;
}else{
digitalWrite(violet_led, LOW);
digitalWrite(yellow_led, LOW);
digitalWrite(green_led, LOW);
send_Data.violet_led = 0;
send_Data.yellow_led = 0;
send_Data.green_led = 0;
}
//ULTRASONIC & BUZZER
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
duration1 = pulseIn(echoPin1, HIGH);
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
duration2 = pulseIn(echoPin2, HIGH);
if (((duration1 < 500) and (duration1 > 200)) or ((duration2 < 500) and (duration2 > 200))){
digitalWrite(red_led, HIGH);
send_Data.red_led = 1;
digitalWrite(buzzer, LOW);
send_Data.buzz = 0;
delay(300);
}else{
digitalWrite(red_led, LOW);
send_Data.red_led = 0;
digitalWrite(buzzer, HIGH);
send_Data.buzz = 1;
delay(300);
}
Serial.println();
Serial.print(">>>>> ");
Serial.println("Send data");
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &send_Data, sizeof(send_Data));
if (result == ESP_OK) {
Serial.println("Sent with success");
}
else {
Serial.println("Error sending the data");
}
}