#include "pitches.h" #include // Ultrasonic Sensor Code const int trigPin = 12; const int echoPin = 10; long duration; int distance; //Speaker code int melody[] = {NOTE_A4, NOTE_B4, NOTE_C3, NOTE_A4, NOTE_B4, NOTE_C3}; // notes in the melody int thisNote; int noteDuration; int pauseBetweenNotes; int noteDurations[] = { 4, 8, 8, 4, 4, 4, 4, 4}; // note durations: 4 = quarter note, 8 = eighth note, etc.:, //PIR Code int Pirsensor = 8; //the pin that the sensor is attached to int PirState = LOW; //default to show no motion detected int sensorvalue; //to store sensor status //Thermistor Code int ThermistorPin = A0; int Vo; float R1 = 100000; float logR2, R2, T; float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07; void setup() { Serial.begin(9600); //Ultrasonic sensor code pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(echoPin, INPUT); // Sets the echoPin as an Input //Speaker Code for (thisNote = 0; thisNote < 8; thisNote++) // iterate over the notes of the melody: { // to calculate the note duration, take one second divided by the note type. //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc. noteDuration = 1000 / noteDurations[thisNote]; // to distinguish the notes, set a minimum time between them, the note's duration + 30% seems to work well: pauseBetweenNotes = noteDuration * 1.30; } //PIR Code pinMode(Pirsensor, INPUT); } void loop() { //Ultrasonic Sensor Code digitalWrite(trigPin, LOW);// Clears the trigPin delayMicroseconds(2); digitalWrite(trigPin, HIGH);// Sets the trigPin on HIGH state for 10 micro seconds delay(500); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH);// Reads the echoPin, returns the sound wave travel time in microseconds distance = duration * 0.034 / 2; // Calculating the distance //PIR Code sensorvalue = digitalRead(Pirsensor);//read signal from sensor //Thermistor Code Vo = analogRead(ThermistorPin); R2 = R1 * (1023.0 / (float)Vo - 1.0); logR2 = log(R2); T = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2));//Temp in kelvins T = T - 273.15;//Temp in degree celcius //Use Ultrasonic sensor to activate system if (distance <= 20) //reverse is the case, but i am adjusting for the Ultrasonic sensor sensitivity { Serial.print("Distance = "); Serial.println(distance); Serial.println("Driver out of car, check for movement and temperature!!!!!"); if (sensorvalue == HIGH)// check for movement inside the car { //Turn Speaker ON tone(3, melody[thisNote], noteDuration); delay(pauseBetweenNotes); noTone(3); noTone(3); // stop the tone playing: Serial.println("Motion detected!"); Serial.print("Temperature: "); Serial.print(T); Serial.print('\n'); delay(500); } //Multiconditional code, to check that there's movement and the car is hot. if (sensorvalue == HIGH && T > 24)// check if there's movement and high temperature { //Turn Speaker ON tone(3, melody[thisNote], noteDuration); delay(pauseBetweenNotes); noTone(3); noTone(3); // stop the tone playing: Serial.println("Motion detected! Hot Car Emergency!!!"); Serial.print("Temperature: "); Serial.print(T); Serial.print('\n'); delay(500); // delay 500 milliseconds } } else { Serial.print("Distance: "); Serial.println(distance); Serial.println("Driver Inside car, all good"); delay(500); } delay(1000);