#include <LiquidCrystal.h>
//#include <LiquidCrystal_I2C.h>
#include <TimerOne.h>
#include <TinyGPS.h>
#include <SoftwareSerial.h>
// include the library code:
SoftwareSerial SIM900(4, 5);
LiquidCrystal lcd(13, 12, 11, 10, 9, 8);
TinyGPS gps; //Creates a new instance of the TinyGPS object
int HBSensor = A0;// Sensor Pin
int HBCount = 0;
int HBCheck = 0;
int TimeinSec = 0;
int HBperMin = 0;
int HBStart = 2; //Pushbutton Pin
int HBStartCheck = 0;
void setup()
{
Serial.begin(9600);
SIM900.begin(9600);
pinMode(HBSensor, INPUT);
pinMode(HBStart, INPUT_PULLUP);
Timer1.initialize(800000);
Timer1.attachInterrupt( timerIsr );
lcd.begin(20, 4); // set up the LCD's number of columns and rows:
lcd.clear();
lcd.setCursor(0,0); // set the cursor position:
lcd.print(" THE BRIGHT LIGHT ");
lcd.setCursor(0,1);
lcd.print(" Heart-Beat Sensor");
lcd.setCursor(0,3);
lcd.print(" Press Button");
}
void loop()
{
if(digitalRead(HBStart) == LOW)
{
lcd.setCursor(0,2);
lcd.print(" HB Counting... ");
lcd.setCursor(0,3);
lcd.print(" Please Wait: ");
HBStartCheck = 1;
}
if(HBStartCheck == 1)
{
if((digitalRead(HBSensor) == HIGH) && (HBCheck == 0))
{
HBCount = HBCount + 1;
HBCheck = 1;
}
if((digitalRead(HBSensor) == LOW) && (HBCheck == 1))
{
HBCheck = 0;
}
if(TimeinSec == 10)
{
HBperMin = HBCount*6;
HBStartCheck = 0;
lcd.setCursor(0,2);
lcd.print(" HB Per Min: ");
lcd.print(HBperMin);
delay(1000);
lcd.print(" ");
lcd.setCursor(0,3);
lcd.print(" Press Button Again.");
HBCount = 0;
TimeinSec = 0;
if(HBperMin >= 120 && HBperMin < 180){
lcd.setCursor(0,0);
lcd.print("Effort intense");
delay(1000);
lcd.clear();
}
else if(HBperMin >= 180 || HBperMin < 50){
lcd.setCursor(0,0);
lcd.print("Urgence");
gps_gsm();
delay(1000);
lcd.clear();
}
lcd.setCursor(0,3);
lcd.print("Press Button Again.");
HBCount = 0;
TimeinSec = 0;
}
}
}
void timerIsr()
{
if(HBStartCheck == 1)
{
TimeinSec = TimeinSec + 1;
lcd.setCursor(14,3);
lcd.print(TimeinSec);
lcd.print(" ");
}
}
void gps_gsm()
{
bool newData = false;
unsigned long chars;
unsigned short sentences, failed;
// For one second we parse GPS data and report some key values
for (unsigned long start = millis(); millis() - start < 1000;)
{
while (Serial.available())
{
char c = Serial.read();
//Serial.print(c);
if (gps.encode(c))
newData = true;
}
}
if (newData) //If newData is true
{
float flat, flon;
unsigned long age;
gps.f_get_position(&flat, &flon, &age);
SIM900.print("AT+CMGF=1\r");
delay(400);
SIM900.println("AT + CMGS = \"+212619234787\"");// recipient's mobile number with country code
delay(300);
SIM900.print("Latitude = ");
SIM900.print(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6);
SIM900.print(" Longitude = ");
SIM900.print(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6);
delay(200);
SIM900.println((char)26); // End AT command with a ^Z, ASCII code 26
delay(200);
SIM900.println();
}
Serial.println(failed);
if (chars == 0)
Serial.println("** No characters received from GPS: check wiring **");
}