#include <TinyGPSPlus.h>
#include <SoftwareSerial.h>
//GPS stuff
const byte gps_RX = 7, gps_TX = 6;// TXPin must be a PWM pin
const uint32_t GPSBaud = 9600;
TinyGPSPlus gps;
SoftwareSerial NEO6M(gps_RX, gps_TX);
//Sensor Stuff
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
const byte motionPin = 13;
const byte buzzPin = 12;
//button stuff
const byte button_Vin = 9;
const byte button_Vout = 11;
//pressure resistor stuff
const byte pressurePin_Vin = 8;
void setup() {
Serial.begin(9600);// put your setup code here, to run once:
NEO6M.begin(GPSBaud);
//thermistor is connected to A0
pinMode(motionPin, INPUT_PULLUP); //motion sensor
pinMode(buzzPin, OUTPUT); //Buzzer
pinMode(button_Vin, OUTPUT);
pinMode(button_Vout, INPUT_PULLUP);
pinMode(pressurePin_Vin, OUTPUT);
//read pressure with pin A1;
}
void loop() {
int analogValue = analogRead(A0); //thermistor uses analog pins
float celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
int motion = digitalRead(motionPin);
int pressure = analogRead(A1);
int buzzTime = 250; //buzzer rhythm
static int trackingMode = 0; //remembers if someone was in the backseat
static int trackingTime = 0;
if (digitalRead(button_Vout) == 0) {
Serial.println("Button Test Successful");
tone(buzzPin,150,50);
delay(buzzTime);
tone(buzzPin,250,50);
}
while (NEO6M.available() > 0)
if (gps.encode(NEO6M.read()))
displayInfo();
if (millis() > 5000 && gps.charsProcessed() < 10)
{
Serial.println(F("No GPS detected: check wiring."));
while(true);
}
//---------------------
//pressure sensor
Serial.print(pressure);
Serial.print(" ");
//motion sensor
Serial.print(motion);
Serial.print(" ");
//thermistor
Serial.print(celsius);
Serial.print(" ");
if (motion == 1 || pressure < 15){
trackingMode = 1; //someone is in the backseat
trackingTime = 10; //memory of someone in backseat
}
if (trackingMode == 1) {
Serial.print("Passenger?");
Serial.print(" ");
}
//alert siren for heat stroke or hypothermia threat
if (trackingMode == 1 && (celsius >= 37.8 || celsius <= 0)) {
Serial.print("DANGER!");
Serial.print(" ");
tone(buzzPin,150,50);
delay(buzzTime);
tone(buzzPin,250,50);
} else delay(buzzTime); //delay compensates for buzzer usage
Serial.println();
if (trackingMode == 1) trackingTime--; //passenger memory fades
if (trackingTime == 0) trackingMode = 0;
}
//---------end of the main loop--------------
void displayInfo()
{
Serial.print(F("Location: "));
if (gps.location.isValid())
{
Serial.print(gps.location.lat(), 6);
Serial.print(F(","));
Serial.print(gps.location.lng(), 6);
}
else
{
Serial.print(F("INVALID"));
}
Serial.print(F(" Date "));
if (gps.date.isValid())
{
Serial.print(gps.date.month());
Serial.print(F("/"));
Serial.print(gps.date.day());
Serial.print(F("/"));
Serial.print(gps.date.year());
}
else
{
Serial.print(F("INVALID"));
}
Serial.println();
}