-
Notifications
You must be signed in to change notification settings - Fork 0
/
old_one
298 lines (258 loc) · 6.62 KB
/
old_one
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include <Arduino.h>
#include <SlowSoftI2CMaster.h>
#include <Wire.h>
#include "Selcouth.h"
#define disk1 0x50 //Address of 24LC256 eeprom chip
#define SAMPLE 1
#define SEND 0
char data = 0;
unsigned int address = 0;
extern int data_array[];
float temperature = 0;
char char_array[100];
int to_send = 0;
int counter= 0,checker = 0;
int Temp_Measuring_State = 0;
int BP_Measuring_State = 0;
int Systolic_Pressure = 120, Diastolic_Pressure = 80, Heart_Beat = 75;
void readEEPROM(int deviceaddress, unsigned int eeaddress );
void eraseEEPROM(int deviceaddress, unsigned int eeaddress, byte data );
void Read_software_I2C();
void Erase_software_I2C();
//**************Creating Objects***********
Temperature_Sensor temp(Gun_Power_Pin,Gun_Eeprom_Power_Pin,Gun_Trig_Pin);
BP_Meter bp(BP_Power_Pin,BP_Measure_Pin);
BIA bia;
ECG ecg;
SlowSoftI2CMaster si = SlowSoftI2CMaster(24, 26, true);
SlowSoftI2CMaster gun_I2C = SlowSoftI2CMaster(20, 21, true);
void setup(void)
{
HandShake_Config();
Serial.begin(9600); //For Arduino
Serial2.begin(38400); //For Bluetooth
Serial3.begin(9600); //From Raspberry Pi
//Turning off I2C at first
//Wire.begin(disk1); !!!!!!!!!!!!TWEANKING THIS THING
temp.Pins_Initializations();
bp.Pins_Initializations();
ecg.Pins_Initializations();
bp.Turn_Off();
temp.Turn_Off_Gun();
temp.Turn_Off_Eeprom();
Serial.print("Resetted");
Serial2.print("Resetted");
}
void loop(){
if(Serial3.available())
{
data = char(Serial3.read());
if(data == '0')
{
delay(2000);
Ready_To_Send();
Serial3.print("173h");
Ready_To_Send();
Serial.print("173h");
}
else if(data == '1')
{
temp.Turn_On_Eeprom();
Serial.print("Perform Relay Action;Join the Power Pin");
Serial2.print("Perform Relay Action;Join the Power Pin");
delay(2000);
temp.Turn_On_Gun();
Serial.print("Joined");
Serial2.print("Joined");
Temp_Measuring_State = 1;
}
else if (data == '2')
{
Serial.print("Activated");
delay(2000);
bia.Activate();
}
else if(data == '3')
{
Serial.write("Entered");
Serial2.write("Entered");
bp.Turn_On();
delay(8000);
Serial.print("Measuring ON");
Serial2.print("Measuring ON");
bp.Start_Measuring();
BP_Measuring_State =1;
}
/*
else if(data == '4')
{
delay(3000);
//delay(200);
Ready_To_Send();
delay(200);
Ready_To_Send();
// for(int i =0;i<100;i++)
// {
// Serial3.print(i);
// }
// Ready_To_Send();
// Ready_To_Send();
// Ready_To_Send();
while (1)
{
ecg.Send_Data(SEND);
}
// //Sample the Values at first
// for(int i =0;i<500;i++)
// {
// ecg.Send_Data(SAMPLE);
// }
// for(int i =0;i<1000;i++)
// {
// ecg.Send_Data(SEND);
// }
}*/
else if (data == 'r')
{
temp.Turn_On_Eeprom();
Wire.begin(disk1);
delay(200);
readEEPROM(disk1, address );
temperature = ((data_array[2]<<8)|(data_array[1]))/100.0;
//Serial.print("The temperature is ");
Serial.print(temperature);
Serial2.print(temperature);
to_send = int(temperature*100);
Ready_To_Send();
Serial3.print(to_send);
Serial3.print("t");
Ready_To_Send();
eraseEEPROM(disk1, address,0);
}
}
if(Temp_Measuring_State == 1)
{
pinMode(20,INPUT);
digitalWrite(20,INPUT_PULLUP);
pinMode(21,INPUT);
digitalWrite(21,INPUT_PULLUP);
Serial.print("Waiting for triggering");
pinMode(Gun_Trig_Pin,INPUT);
digitalWrite(Gun_Trig_Pin,INPUT_PULLUP);
delay(100);
while(digitalRead(Gun_Trig_Pin) !=0)
{
Serial.print("I am HIGH\n");
Serial2.print("I am HIGH\n");
delay(100);
}
Serial.print("I am pressed\n");
Serial2.print("I am pressed\n");
Serial.print("Now its time to turn off relay");
delay(4500);
temp.Turn_Off_Gun();
Ready_To_Send();
Serial3.print("y");
Ready_To_Send();
Serial.print("y");
Temp_Measuring_State = 0;
}
else if(BP_Measuring_State == 1)
{
pinMode(26,INPUT);
digitalWrite(26,INPUT_PULLUP);
delay(6000);
while(digitalRead(26) !=0)
{
Serial.print("High\n");
Serial2.print("High\n");
//delay(100);
}
Serial.print("Out of LOOP\n");
delay(1500);
if (!si.i2c_init()) // initialize I2C module
Serial.println("I2C init failed");
Read_software_I2C();
BP_Measuring_State = 0;
Erase_software_I2C();
}
}
void eraseEEPROM(int deviceaddress, unsigned int eeaddress, byte data )
{
for(int i=0; i<10; i++)
{
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.write(data);
Wire.endTransmission();
eeaddress = i ;
Serial.print(i);
Serial.write("Erasing");
}
Serial2.write(" ERASED!!");
}
void readEEPROM(int deviceaddress, unsigned int eeaddress )
{
byte rdata = 0xFF;
int x=0;
Serial.print("R\n");
for(int i=79; i<83; i++)
{
x= i-79;
Wire.beginTransmission(deviceaddress);
Wire.write((int)(i)); // LSB
Wire.endTransmission();
Wire.requestFrom(deviceaddress,1 );
rdata = Wire.read();
delay(10);
data_array[x] = rdata;
}
}
void Read_software_I2C()
{
Serial.println("Receiving from Eeprom \n");
if (!si.i2c_start((disk1<<1)|I2C_WRITE)) { // init transfer
Serial.println("I2C device busy");
return;
}
for(int i=0;i<15;i++)
{
si.i2c_write(i); // send memory to device
si.i2c_rep_start((disk1<<1)|I2C_READ); // restart for reading
data_array[i] = si.i2c_read(true); // read one byte and send NAK afterwards
si.i2c_stop(); // stop communication
Serial.print(i);
Serial.print(" ");
Serial.println(data_array[i]);
}
if(data_array[0]>20)
{
Systolic_Pressure = data_array[0];
Diastolic_Pressure = data_array[1];
Heart_Beat = data_array[2];
}
else
{
Systolic_Pressure = data_array[8];
Diastolic_Pressure = data_array[9];
Heart_Beat = data_array[10];
}
delay(1000);
sprintf(char_array,"%ds%dd%dh",Systolic_Pressure,Diastolic_Pressure,Heart_Beat);
Serial.write(char_array);
Ready_To_Send();
Serial3.write(char_array);
Ready_To_Send();
}
void Erase_software_I2C()
{
if (!si.i2c_start((disk1<<1)|I2C_WRITE)) { // init transfer
Serial.println("I2C device busy");
return;
}
si.i2c_write(0); // send memory to device
si.i2c_write(1);
si.i2c_stop(); // stop communication
Serial.write("Written 1");
Serial2.write("Written 1");
}