#include <Wire.h>
// Declaration of Libraries used
#include <TimerOne.h>
#include <Wire.h>
#include "genieArduino.h"
// Variable Declaration
int passcode = 2402, display = 0; // For limited access
float t1, t2, t3, t4, t5, t6, t7, t8, t9, highest_temp, avg_t1, avg_t2, avg_t3, avg_t4, avg_t5, avg_t6, avg_t7, avg_t8, avg_t9, pre_temp, avg_temp, avg_temp_left, avg_temp_middle, avg_temp_right, room_temp, avg_room_temp, threshold_temp,threshold_avg_temp,difference; // For temperature reading;
int s1, s2, s3, s4, s5, s6, s7, s8, s9, td1, td2, td3, td4, td5, td6, td7, td8, td9;
int t1_dis, t2_dis, t3_dis, t4_dis, t5_dis, t6_dis, t7_dis, t8_dis, t9_dis, FRS1_dis, FRS2_dis, FRS3_dis, FRS4_dis, FRS5_dis, FRS6_dis, FRS7_dis, room_temp_dis, avg_temp_dis, avg_temp_left_dis, avg_temp_middle_dis, avg_temp_right_dis;
float FRS1, FRS2, FRS3, FRS4, FRS5, FRS6, FRS7, FRS_avg1, FRS_avg2, FRS_avg3, FRS_avg4, FRS_avg5, FRS_avg6, FRS_avg7; // For position reading
int pre_state = 0, current_state = 0, alert = 0, sv_temp = 33, valve_status = 0;
int tt_hours=0, tt_minutes=0, tt_seconds=0, temp_seconds=0, room_temp_sec=0, tmin; // For timer
int ahr=2,amin=0,alert_time=120, time_up = 0;
int tt,i, j,channel=1,ds=1,flipover_control,alert_control2;
String STRING_DATA_HEADER = "A", STRING_DATA_STOPPER = "B", output; // For zigbee communication
// Pin connection Definition
int temp1 = 0; //connect temperature output to analog pin3
int temp2 = 1; //connect temperature output to analog pin4
int temp3 = 2; //connect temperature output to analog pin5
int temp4 = 3; //connect temperature output to analog pin6
int temp5 = 4; //connect temperature output to analog pin7
int temp6 = 5; //connect temperature output to analog pin8
int temp7 = 6; //connect temperature output to analog pin9
int temp8 = 7; //connect temperature output to analog pin10
int temp9 = 8; //connect temperature output to analog pin11
int FRS_1 = 9;
int FRS_2 = 10;
int FRS_3 = 11;
int FRS_4 = 12;
int FRS_5 = 13;
int FRS_6 = 14;
int FRS_7 = 15;
int solenoid_valve = 40;
int buzzer = 41;
int vib = 42;
int tmp102Address = 0x48; // Digital temperature sensor for room temperature reading
//======Digital Temperature Sensor Function ======
float getTemperature()
{
Wire.requestFrom(tmp102Address,2); // We use our wire library’s get two readings from our TMP102. We pass it the address and the quantity.
// You may have noticed that we didn’t do anything with the request. That’s because we have to read back we requested. We do that sequentially.
byte MSB = Wire.read(); // The first byte we get, the Most Significant, get’s stored in MSB.
byte LSB = Wire.read(); // The second byte we get, the Least Significant, get’s stored in LSB.
int TemperatureSum = ((MSB < 8) | LSB) > 4;
float celsius = TemperatureSum*0.0625; // Convert our temp to celsius.
return celsius; // Return our result to be printed.
}
void readRoomTemp()
{
avg_room_temp = 0;
for(i=0;i<50;i++)
{
room_temp = getTemperature(); // Get the temperature in C and save it as a float called celsius.
avg_room_temp += room_temp;
}
room_temp = avg_room_temp / 50.0;
}
void readTemp()
{
avg_t1 = 0;
avg_t2 = 0;
avg_t3 = 0;
avg_t4 = 0;
avg_t5 = 0;
avg_t6 = 0;
avg_t7 = 0;
avg_t8 = 0;
avg_t9 = 0;
for(i=0; i<20 ; i++) //takes 20 reading per temperature sensor
{
t1 = analogRead(temp1); //takes analog reading
t1 = ((t1/1023.0*5.0) - 0.5) /0.01; //convert analog reading into degree celsius
avg_t1 += t1;
t2 = analogRead(temp2); //takes analog reading
t2 = ((t2/1023.0*5.0) - 0.5) /0.01; //convert analog reading into degree celsius
avg_t2 += t2;
t3 = analogRead(temp3); //takes analog reading
t3 = ((t3/1023.0*5.0) - 0.5) /0.01; //convert analog reading into degree celsius
avg_t3 += t3;
t4 = analogRead(temp4); //takes analog reading
t4 = ((t4/1023.0*5.0) - 0.5) /0.01; //convert analog reading into degree celsius
avg_t4 += t4;
t5 = analogRead(temp5); //takes analog reading
t5 = ((t5/1023.0*5.0) - 0.5) /0.01; //convert analog reading into degree celsius
avg_t5 += t5;
t6 = analogRead(temp6); //takes analog reading
t6 = ((t6/1023.0*5.0) - 0.5) /0.01; //convert analog reading into degree celsius
avg_t6 += t6;
t7 = analogRead(temp7); //takes analog reading
t7 = ((t7/1023.0*5.0) - 0.5) /0.01; //convert analog reading into degree celsius
avg_t7 += t7;
t8 = analogRead(temp8); //takes analog reading
t8 = ((t8/1023.0*5.0) - 0.5) /0.01; //convert analog reading into degree celsius
avg_t8 += t8;
t9 = analogRead(temp9); //takes analog reading
t9 = ((t8/1023.0*5.0) - 0.5) /0.01; //convert analog reading into degree celsius
avg_t9 += t9;
}
t1 = avg_t1/20; //divide the 20 reading to get average temperature
t2 = avg_t2/20;
t3 = avg_t3/20;
t4 = avg_t4/20;
t5 = avg_t5/20;
t6 = avg_t6/20;
t7 = avg_t7/20;
t8 = avg_t8/20;
t9 = avg_t9/20;
}
void mappingTemp()
{
if (t1 > room_temp)
{
td1 = t1 - room_temp;
t1 = t1 - td1;
}
else
{
td1 = room_temp - t1;
t1 = t1 + td1;
}
if (t2 > room_temp)
{
td2 = t2 - room_temp;
t2 = t2 - td2;
}
else
{
td2 = room_temp - t2;
t2 = t2 + td2;
}
if (t3 > room_temp)
{
td3 = t3 - room_temp;
t3 = t3 - td3;
}
else
{
td3 = room_temp - t3;
t3 = t3 + td3;
}
if (t2 > room_temp)
{
td4 = t4 - room_temp;
t4 = t4 - td4;
}
else
{
td4 = room_temp - t4;
t4 = t4 + td4;
}
if (t5 > room_temp)
{
td5 = t5 - room_temp;
t5 = t5 - td5;
}
else
{
td5 = room_temp - t5;
t5 = t5 + td5;
}
if (t6 > room_temp)
{
td6 = t6 - room_temp;
t6 = t6 - td6;
}
else
{
td6 = room_temp - t6;
t6 = t6 + td6;
}
if (t7 > room_temp)
{
td7 = t7 - room_temp;
t7 = t7 - td7;
}
else
{
td7 = room_temp - t7;
t7 = t7 + td7;
}
if (t8 > room_temp)
{
td8 = t8 - room_temp;
t8 = t8 - td8;
}
else
{
td8 = room_temp - t8;
t8 = t8 + td8;
}
if (t9 > room_temp)
{
td9 = t9 - room_temp;
t9 = t9 - td9;
}
else
{
td9 = room_temp - t9;
t9 = t9 + td9;
}
avg_temp = ((t1+t2+t3+t4+t5+t6+t7+t8+t9)/9); //get average temperature of 9 temperature sensor
avg_temp_left= ((t1+t4+t7)/3); //get average temperature of 3 temperature sensor on left
avg_temp_middle= ((t2+t5+t8)/3); //get average temperature of 3 temperature sensor on middle
avg_temp_right= ((t3+t6+t9)/3); //get average temperature of 3 temperature sensor on rightture of 3 temperature sensor on right
// Finding Highest Temperature
if((t1 > t2) & (t1 > t3) & (t1 > t4) & (t1 > t5) & (t1 > t6) & (t1 > t7) & (t1 > t8) & (t1 > t9))
highest_temp = t1;
if((t2 > t1) & (t2 > t3) & (t2 > t4) & (t2 > t5) & (t2 > t6) & (t2 > t7) & (t2 > t8) & (t2 > t9))
highest_temp = t2;
if((t3 > t1) & (t3 > t2) & (t3 > t4) & (t3 > t5) & (t3 > t6) & (t3 > t7) & (t3 > t8) & (t3 > t9))
highest_temp = t3;
if((t2 > t1) & (t2 > t2) & (t4 > t3) & (t4 > t5) & (t4 > t6) & (t4 > t7) & (t4 > t8) & (t4 > t9))
highest_temp = t4;
if((t5 > t1) & (t5 > t2) & (t5 > t3) & (t5 > t4) & (t5 > t6) & (t5 > t7) & (t5 > t8) & (t5 > t9))
highest_temp = t5;
if((t6 > t1) & (t6 > t2) & (t6 > t3) & (t6 > t4) & (t6 > t5) & (t6 > t7) & (t6 > t8) & (t6 > t9))
highest_temp = t6;
if((t7 > t1) & (t7 > t2) & (t7 > t3) & (t7 > t4) & (t7 > t5) & (t7 > t6) & (t7 > t8) & (t7 > t9))
highest_temp = t7;
if((t8 > t1) & (t8 > t2) & (t8 > t3) & (t8 > t4) & (t8 > t5) & (t8 > t6) & (t8 > t7) & (t8 > t9))
highest_temp = t8;
if((t9 > t1) & (t9 > t2) & (t9 > t3) & (t9 > t4) & (t9 > t5) & (t9 > t6) & (t9 > t7) & (t9 > t8))
highest_temp = t9;
}
void setup()
{
Wire.begin(); // Initiate the Wire library and join the I2C bus
Serial1.begin(9600); // the baudrate used for connecting the arduino to labview
Timer1.initialize(1000000); // set a timer of length 300000 microseconds (or 0.1 sec - or 10Hz => the led will blink 5 times, 5 cycles of on-and-off, per second)
Timer1.attachInterrupt(timerIsr); // attach the service routine here
// Declaration of input/output devices
pinMode(solenoid_valve, OUTPUT);
pinMode(vib, OUTPUT);
pinMode(buzzer, OUTPUT);
// Setting initial state of output devices
digitalWrite(solenoid_valve, LOW);
digitalWrite(vib, LOW);
digitalWrite(buzzer, LOW);
readRoomTemp();
readTemp();
mappingTemp();
genieBegin(GENIE_SERIAL,9600);
// Using Serail 0 with baudrate 9600 to communicate with LCD
genieAttachEventHandler(myGenieEventHandler);
genieWriteObject (GENIE_OBJ_FORM, 0, 0); // Select form 0
genieWriteStr(0," "); // Reset password
genieWriteStr(1, "No Patient Lying on bed"); // Write to String 1
// Set 2 hr alarm
genieWriteObject (GENIE_OBJ_LED_DIGITS, 0, ahr);
genieWriteObject (GENIE_OBJ_LED_DIGITS, 1, amin);
// Set initial valve temperature
genieWriteObject (GENIE_OBJ_LED_DIGITS, 20, sv_temp);
}
void loop()
{
genieDoEvents();
// Reading the position of the patient from pressure sensors
FRS_avg1 = 0;
FRS_avg2 = 0;
FRS_avg3 = 0;
FRS_avg4 = 0;
FRS_avg5 = 0;
FRS_avg6 = 0;
FRS_avg7 = 0;
for (i=0; i <= 10; i++)
{
FRS1 = analogRead(FRS_1);
FRS_avg1 += FRS1;
FRS2 = analogRead(FRS_2);
FRS_avg2 += FRS2;
FRS3 = analogRead(FRS_3);
FRS_avg3 += FRS3;
FRS4 = analogRead(FRS_4);
FRS_avg4 += FRS4;
FRS5 = analogRead(FRS_5);
FRS_avg5 += FRS5;
FRS6 = analogRead(FRS_6);
FRS_avg6 += FRS6;
FRS7 = analogRead(FRS_7);
FRS_avg7 += FRS7;
}
FRS1 = FRS_avg1/10;
FRS2 = FRS_avg2/10;
FRS3 = FRS_avg3/10;
FRS4 = FRS_avg4/10;
FRS5 = FRS_avg5/10;
FRS6 = FRS_avg6/10;
FRS7 = FRS_avg7/10;
room_temp_sec;
temp_seconds;
t1_dis = t1;
t2_dis = t2;
t3_dis = t3;
t4_dis = t4;
t5_dis = t5;
t6_dis = t6;
t7_dis = t7;
t8_dis = t8;
t9_dis = t9;
room_temp_dis = room_temp;
avg_temp_dis = avg_temp;
avg_temp_left_dis = avg_temp_left;
avg_temp_middle_dis = avg_temp_middle;
avg_temp_right_dis = avg_temp_right;
FRS1_dis = FRS1;
FRS2_dis = FRS2;
FRS3_dis = FRS3;
FRS4_dis = FRS4;
FRS5_dis = FRS5;
FRS6_dis = FRS6;
FRS7_dis = FRS7;
tmin = (tt_hours*60) + tt_minutes; //change total alarm timing to minutes
// Alert ON for same sleeping position or high temperature or leaving the bed
if ((tmin >= alert_time) || ((highest_temp - pre_temp > 2) & (highest_temp - pre_temp <10)))
{
digitalWrite(buzzer, HIGH);
alert = 1;
}
else
{
digitalWrite(buzzer, LOW);
alert = 0;
}
//Position Monitoring
if (FRS1< 300 & FRS2< 300 & FRS3< 300 & FRS4< 300 & FRS5< 300)
{
genieWriteObject(GENIE_OBJ_USER_LED, 1, 0);
genieWriteObject(GENIE_OBJ_USER_LED, 2, 0);
genieWriteObject(GENIE_OBJ_USER_LED, 3, 0);
genieWriteObject(GENIE_OBJ_USER_LED, 4, 0);
genieWriteObject(GENIE_OBJ_USER_LED, 5, 0);
if (FRS6 >= 300 || FRS7 >= 300)
{
genieWriteObject(GENIE_OBJ_USER_LED, 0, 1);
digitalWrite(buzzer, HIGH);
alert = 1;
}
else
{
genieWriteObject(GENIE_OBJ_USER_LED, 0, 0);
digitalWrite(buzzer, LOW);
alert = 0;
}
current_state = 0;
}
else
{
if (FRS1 < 300 & FRS2 < 300 & FRS3 < 300 & FRS4 >= 300 & FRS5 >= 300)
{
genieWriteObject(GENIE_OBJ_USER_LED, 4, 1);
genieWriteObject(GENIE_OBJ_USER_LED, 5, 1);
current_state = 1;
}
if (FRS1 < 300 & FRS2 < 300 & FRS3 < 300 & FRS4 >= 300 & FRS5 < 300)
{
genieWriteObject(GENIE_OBJ_USER_LED, 4, 1);
current_state = 2;
}
if (FRS1 < 300 & FRS2 < 300 & FRS3 < 300 & FRS4 < 300 & FRS5 >= 300)
{
genieWriteObject(GENIE_OBJ_USER_LED, 5, 1);
current_state = 3;
}
if (FRS1 >= 300 & FRS2 >= 300 & FRS3 < 300 & FRS4 < 300 & FRS5 < 300)
{
genieWriteObject(GENIE_OBJ_USER_LED, 1, 1);
genieWriteObject(GENIE_OBJ_USER_LED, 2, 1);
current_state = 4;
}
if (FRS1 < 300 & FRS2 >= 300 & FRS3 < 300 & FRS4 < 300 & FRS5 < 300)
{
genieWriteObject(GENIE_OBJ_USER_LED, 2, 1);
current_state = 5;
}
if (FRS1 >= 300 & FRS2 < 300 & FRS3 < 300 & FRS4 < 300 & FRS5 < 300)
{
genieWriteObject(GENIE_OBJ_USER_LED, 1, 1);
current_state = 6;
}
if (FRS1 < 300 & FRS2 >= 300 & FRS3 >= 300 & FRS4 >= 300 & FRS5 < 300)
{
genieWriteObject(GENIE_OBJ_USER_LED, 2, 1);
genieWriteObject(GENIE_OBJ_USER_LED, 3, 1);
genieWriteObject(GENIE_OBJ_USER_LED, 4, 1);
current_state = 7;
}
if (FRS1 < 300 & FRS2 < 300 & FRS3 >= 300 & FRS4 >= 300 & FRS5 < 300)
{
genieWriteObject(GENIE_OBJ_USER_LED, 3, 1);
genieWriteObject(GENIE_OBJ_USER_LED, 4, 1);
current_state = 8;
}
if (FRS1 < 300 & FRS2 >= 300 & FRS3 >= 300 & FRS4 < 300 & FRS5 < 300)
{
genieWriteObject(GENIE_OBJ_USER_LED, 2, 1);
genieWriteObject(GENIE_OBJ_USER_LED, 3, 1);
current_state = 9;
}
if (FRS1 < 300 & FRS2 < 300 & FRS3 >= 300 & FRS4 < 300 & FRS5 < 300)
{
genieWriteObject(GENIE_OBJ_USER_LED, 3, 1);
current_state = 10;
}
if (FRS1 >= 300 & FRS2 >= 300 & FRS3 >= 300 & FRS4 < 300 & FRS5 < 300)
current_state = 11;
if (FRS1 < 300 & FRS2 < 300 & FRS3 >= 300 & FRS4 >= 300 & FRS5 >= 300)
current_state = 12;
if (FRS1 >= 300 & FRS2 >= 300 & FRS3 >= 300 & FRS4 >= 300 & FRS5 < 300)
current_state = 13;
if (FRS1 < 300 & FRS2 >= 300 & FRS3 >= 300 & FRS4 >= 300 & FRS5 >= 300)
current_state = 14;
if (FRS1 >= 300 & FRS2 >= 300 & FRS3 >= 300 & FRS4 >= 300 & FRS5 >= 300)
current_state = 15;
}
if((pre_state == 0) & (pre_state != current_state))
pre_temp = avg_temp;
if(current_state==0)
pre_temp = 0;
if (((pre_state == 0) || (current_state == 0)) || ((pre_state != 0)& (pre_state != current_state)))
{
tt_hours=0;
tt_minutes=0;
tt_seconds=0;
genieWriteObject (GENIE_OBJ_LED_DIGITS, 4, tt_hours);
genieWriteObject (GENIE_OBJ_LED_DIGITS, 5, tt_minutes);