B. Arduino Mega Program
B. Arduino Mega Program
#include <LayadCircuits_SalengGSM.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <LiquidCrystal_I2C.h>
TinyGPSPlus gps;
LiquidCrystal_I2C lcd(0x3F,16,2);
//SoftwareSerial gsmSerial(2,3);
LayadCircuits_SalengGSM salengGSM =
LayadCircuits_SalengGSM(&gsmSerial);
TinyGPSPlus tinygps;
//Pulse Variables
float latitude;
float longitude;
char sms_buff[160]="";
char buf[17]="";
bool flag_abnormal_bpm;
void gps_routine()
if(Serial2.available())
tim = millis();
if(Serial2.available())
char c;
c = Serial2.read();
tinygps.encode(c);
if (!tinygps.location.isValid()) Serial.write(c); //
test only
t=millis();
if (tinygps.location.isValid())
digitalWrite(LED_BUILTIN,HIGH);
latitude = tinygps.location.lat();
longitude = tinygps.location.lng();
Serial.print(F("-----------------latitude="));
Serial.println(latitude);
Serial.print(F("-----------------longitude="));
Serial.println(longitude);
Appendices 45
else digitalWrite(LED_BUILTIN,LOW);
void interruptSetup()
TCCR2A = 0x02;
TCCR2B = 0x06;
OCR2A = 0X7C;
// SET THE TOP OF THE COUNT TO 124 FOR 500Hz SAMPLE RATE
TIMSK2 = 0x02;
sei();
ISR(TIMER2_COMPA_vect)
cli();
Signal = analogRead(PULSEPIN);
sampleCounter += 2;
// T is the trough
T = Signal;
P = Signal;
// P is the peak
if (N > 250){
Pulse = true;
lastBeatTime = sampleCounter;
if(secondBeat){
secondBeat = false;
rate[i] = IBI;
Appendices 48
if(firstBeat){
firstBeat = false;
secondBeat = true;
sei();
return;
word runningTotal = 0;
rate[i] = rate[i+1];
runningTotal += rate[i];
rate[9] = IBI;
runningTotal += rate[9];
runningTotal /= 10;
BPM = 60000/runningTotal;
QS = true;
Pulse = false;
amp = P - T;
thresh = amp/2 + T;
P = thresh;
Appendices 50
T = thresh;
if (N > 2500){
thresh = 512;
P = 512;
// set P default
T = 512;
// set T default
lastBeatTime = sampleCounter;
firstBeat = true;
secondBeat = false;
sei();
void pulse_sensor()
Appendices 51
actual_temp = millis();
void sensor_check()
timer = millis();
Appendices 52
if(flagp == false)
ctrp++;
flagp = true;
else
flagp = false;
ctrp = 0;
Serial.print(F("BPM="));
Serial.println(BPM);
void sms_manager()
Appendices 53
if(digitalRead(SMS_DISABLE) == LOW)
flag_abnormal_bpm = false;
return;
t=millis();
flag_abnormal_bpm = false;
assemble_sms(sms_buff);
Serial.print(F("Sending to:"));
Serial.println(PHONE);
Serial.println(sms_buff);
Appendices 54
// if(DEBUG)Serial.println(F("ABNORMAL PULSERATE"));
char temp[32];
byte p;
p=0;
memset(pmsg,' ',160);
pmsg[159] = 0;
sprintf(pmsg,"BPM=%03d",BPM); p = 7;
memcpy(&pmsg[p],"\nLOCATION\nLATITUDE:",19); p = p +
19
memset(temp,0,32);
// dtostrf(latitude,3,6,temp);
// strncpy(&pmsg[p], temp,9);
// p = p + 9;
dtostrf(latitude,3,6,temp);
strncpy(&pmsg[p], temp,8);
p = p + 8;
Appendices 55
pmsg[p] = '\n';
p++;
memcpy(&pmsg[p],"LONGITUDE:",10); p = p + 10;
memset(temp,0,32);
dtostrf(longitude,3,6,temp);
strncpy(&pmsg[p], temp,9);
p = p + 9;
pmsg[p] = '\n';
p++;
strncpy(&pmsg[p],"\r\n",2); p = p + 2;
if(latitude!=0)
// http://maps.google.com/?q=lll.llllll,ooo.oooooo
// p=0;
strncpy(&pmsg[p], "http://maps.google.com/?q=",26);
p = p+ 26;
memset(temp,0,32);
dtostrf(latitude,3,6,temp);
// strncpy(&pmsg[p], temp,9);
Appendices 56
// p = p + 9;
strncpy(&pmsg[p], temp,8);
p = p + 8;
pmsg[p] = ',';
p++;
memset(temp,0,32);
dtostrf(longitude,3,6,temp);
strncpy(&pmsg[p], temp,9);
p = p + 9;
else
sprintf(pmsg,"BPM=
%03d\nLOCATION\nLATITUDE:0\nLONGITUDE:0\n\r\nNo GPS
Signal",BPM);
memcpy(&pmsg[p], "\r\n",2);
p = p+2;
pmsg[p] = 0;
Appendices 57
void lcd_refresh()
t=millis();
char g,s;
if (tinygps.location.isValid()) g='G';
else s = 'S';
if(salengGSM.isFreeToSend() == false)
lcd.setCursor(0,1);
lcd.print(buf);
else
Appendices 58
lcd.setCursor(0,1);
lcd.print(buf);
void setup() {
pinMode(LED_BUILTIN,OUTPUT);
pinMode(SMS_DISABLE,INPUT_PULLUP);
salengGSM.begin(9600);
Serial.begin(9600);
Serial2.begin(9600);
Serial.println(F("AT"));
lcd.init();
lcd.backlight();
//Pulse Sensor
interruptSetup();
unsigned long t;
salengGSM.initSalengGSM();
Serial.println(F("Start"));
void loop() {
salengGSM.smsMachine();
pulse_sensor()a;
sms_manager();
sensor_check();
gps_routine();
lcd_refresh();
}
Appendices 60
C. Documentation