/********* Pleasedontcode.com **********
Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- Terms and Conditions:
You have a non-exclusive, revocable, worldwide, royalty-free license
for personal and commercial use. Attribution is optional; modifications
are allowed, but you're responsible for code maintenance. We're not
liable for any loss or damage. For full terms,
please visit pleasedontcode.com/termsandconditions.
- Project: **Fall Detection**
- Source Code NOT compiled for: Arduino Uno
- Source Code created on: 2025-06-09 05:34:17
********* Pleasedontcode.com **********/
/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
/* it wont reset after i pressed cancellation button */
/****** END SYSTEM REQUIREMENTS *****/
/* START CODE */
/****** DEFINITION OF LIBRARIES *****/
#include <EasyButton.h> //https://github.com/evert-arias/EasyButton
#include "TinyGPS.h" // Include TinyGPS library
#include "SoftwareSerial.h" // Include SoftwareSerial library
/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t cancellationbutton_PushButton_PIN_D2 = 2;
#define buttonPin 8 // Confirmation button
#define cancelButtonPin 7 // Cancellation button
/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t myLED_LED_PIN_D3 = 3;
#define buzzerPin 6 // Buzzer pin
#define ledPin 5 // LED pin
/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
/***** used to store raw data *****/
bool myLED_LED_PIN_D3_rawData = 0;
/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
/***** used to store data after characteristic curve transformation *****/
float myLED_LED_PIN_D3_phyData = 0.0;
/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
SoftwareSerial GPRS(2,3); // RX=pin 2, TX=pin 3
SoftwareSerial serial_connection(10, 11); // RX=pin 10, TX=pin 11
TinyGPSPlus gps; // This is the GPS object that will work with the NMEA data
// Pins and variables declarations
float latitude=0;
float longitude=0;
String Speed="";
int Contrast=20;
// Accelerometer pins
#define x A1
#define y A2
#define z A3
// Pulse Sensor pin
#define pulseSensorPin A0
// Variables corresponding to accelerometer
int xsample=0;
int ysample=0;
int zsample=0;
#define samples 10 // number of sample readings in still case
#define fallThreshold 650 // Fall Detection Threshold
// Threshold maximum and minimum values of change in acceleration when fall is detected
#define minVal -50
#define MaxVal 50
// Variables tracking button state
bool isFallDetected = false;
bool isFallConfirmed = false;
bool isFallCancelled = false;
// Assign timing
unsigned long fallTime; // when fall is detected
unsigned long confirmationTimeout = 5000; // Time wait for confirmation or cancellation button is 5 seconds
unsigned long confirmationWaitTime; // Time to wait for confirmation
// Pulse sensor variables
int pulseValue = 0;
unsigned long lastBeatTime = 0; // Last time heartbeat detected
unsigned long beatInterval = 0; // Time interval between beats
int heartRate = 0; // heart rate in BPM
int pulseThreshold = 512; // default threshold for pulse
// Function declarations
void triggerFallConfirmation();
void sendAlert();
void resetPotentiometer();
void setup(void)
{
// put your setup code here, to run once:
Serial.begin(9600); // Start serial communication
serial_connection.begin(9600); // This opens up communications to the GPS
Serial.println("successfully Initialized....");
Serial.println("GPS Start"); // Just show to the monitor that the sketch has started
// Taking samples in still case
for(int i=0; i<samples; i++)
{
xsample += analogRead(x);
ysample += analogRead(y);
zsample += analogRead(z);
}
// Taking average
xsample /= samples;
ysample /= samples;
zsample /= samples;
Serial.println(xsample);
Serial.println(ysample);
Serial.println(zsample);
delay(1000);
// Set pin mode for button, buzzer and led
pinMode(cancellationbutton_PushButton_PIN_D2, INPUT_PULLUP);
pinMode(buttonPin, INPUT_PULLUP); // Confirmation button
pinMode(cancelButtonPin, INPUT_PULLUP); // Cancellation button
pinMode(buzzerPin, OUTPUT); // Buzzer pin
pinMode(ledPin, OUTPUT); // LED pin
pinMode(pulseSensorPin, INPUT); // Pulse sensor pin
}
void loop(void)
{
// put your main code here, to run repeatedly:
updateOutputs(); // Refresh output data
while(serial_connection.available()) // While there are characters to come from the GPS
{
gps.encode(serial_connection.read()); // This feeds the serial NMEA data into the library one char at a time
}
if(gps.location.isUpdated())
{
// Get the latest info from the gps object which it derived from the data sent by the GPS unit
Serial.println("Satellite Count : ");
Serial.println(gps.satellites.value());
Serial.println("Latitude : ");
Serial.println(gps.location.lat(), 6);
Serial.println("Longitude : ");
Serial.println(gps.location.lng(), 6);
Serial.println("Speed MPH : ");
Serial.println(gps.speed.mph());
Serial.println("Altitude Feet : ");
Serial.println(gps.altitude.feet());
Serial.println("");
}
int value1 = analogRead(x);
int value2 = analogRead(y);
int value3 = analogRead(z);
int xValue = xsample - value1;
int yValue = ysample - value2;
int zValue = zsample - value3;
Serial.print("x=");
Serial.println(xValue);
Serial.print("y=");
Serial.println(yValue);
Serial.print("z=");
Serial.println(zValue);
// Pulse sensor reading
pulseValue = analogRead(pulseSensorPin); // Read value from x axis potentiometer
pulseThreshold = map(analogRead(x), 0, 1023, 400, 600); // Adjust threshold range (400-600)
Serial.print("Pulse Value : ");
Serial.println(pulseValue);
Serial.print("Pulse Threshold : ");
Serial.println(pulseThreshold);
if(pulseValue > pulseThreshold)
{
if(millis() - lastBeatTime > 500) // Only count a beat every 500ms (roughly 60BPM)
{
heartRate = 60000 / (millis() - lastBeatTime); // Calculate heart rate in beats per minute
lastBeatTime = millis();
Serial.print("Heart Rate : ");
Serial.println(heartRate);
// Check if heart rate is abnormal
if(heartRate < MIN_HEART_RATE || heartRate > MAX_HEART_RATE)
{
Serial.println("Abnormal heart rate detected. Please confirm if fall has occured.");
triggerFallConfirmation(); // Confirm whether there's a fall or not
}
}
}
// Condition for Fall Detection
if(xValue < minVal || xValue > MaxVal || yValue < minVal || yValue > MaxVal || zValue < minVal || zValue > MaxVal)
{ // Fall Detected, send SMS to emergency contact
Serial.println("Fall Detected, Please press the button to confirm...");
isFallDetected = true; // When fall is detected
// Buzzer and LED on when fall is detected
digitalWrite(buzzerPin, HIGH); // Buzzer On
digitalWrite(ledPin, HIGH); // LED on
fallTime = millis(); // Record time when fall is detected
confirmationWaitTime = fallTime + confirmationTimeout; // Set timeout time for confirmation
// Confirmation button
while (!isFallConfirmed && !isFallCancelled && millis() < confirmationWaitTime)
{
// Wait for confirmation or cancellation button press
if (digitalRead(buttonPin) == LOW) // If confirmation button is pressed
{
isFallConfirmed = true;
Serial.println("Fall is confirmed!");
sendAlert(); // Send SMS alert
break; // Exit loop once fall is confirmed
}
if (digitalRead(cancelButtonPin) == LOW)
{
isFallCancelled = true; // If cancellation button is pressed
Serial.println("Fall Detection cancelled");
// Do not reset the state after cancellation
digitalWrite(buzzerPin, LOW); // Buzzer off
digitalWrite(ledPin, LOW); // LED off
break;
}
}
// If no confirmation or cancellation button is pressed, automatically confirm the fall
if(!isFallConfirmed && millis() >= confirmationWaitTime)
{
isFallConfirmed = true;
Serial.println("No response, fall Detection is confirmed");
sendAlert(); // send SMS automatically
}
// After confirmation or cancellation, reset fall detection
if (isFallCancelled)
{
resetPotentiometer(); // Reset Potentiometer values after confirmation
isFallDetected = false;
isFallConfirmed = false;
// Do not reset isFallCancelled to allow for further cancellations
Serial.println("Fall Detection reset.");
// Reset after cancellation and continue monitoring
digitalWrite(buzzerPin, LOW); // Buzzer off
digitalWrite(ledPin, LOW); // LED off
}
}
delay(1000);
}
void updateOutputs()
{
digitalWrite(myLED_LED_PIN_D3, myLED_LED_PIN_D3_rawData);
}
// Send SMS alert
void sendAlert()
{
// Initialize GPRS module
GPRS.begin(9600);
Serial.println("Connecting to network...");
delay(2000); // wait for network connection
Serial.println("Should be connected to network by now");
// Send SMS alerts with location
GPRS.print("AT+CMGF=1\r"); // set SMS format to text mode
delay(1200);
GPRS.print("AT+CMGS=\"+01xxxxxxxxx\"\r"); // Set phone number to send alert with (replace with actual number)
delay(1200);
// Message
GPRS.print("Fall Detected!");
GPRS.print("Location:\n");
GPRS.print("Latitude : ");
GPRS.print(latitude);
GPRS.print("\nLongitude : ");
GPRS.print("\nGoogle Maps Link: http://maps.google.com/maps?&z=15&mrt=yp&t=k&q=");
GPRS.println(latitude);
GPRS.println("+");
GPRS.println(longitude);
GPRS.write(26); // Send SMS
GPRS.println("SMS Sent!");
}
// Function resetting potentiometer
void resetPotentiometer()
{
xsample = 0;
ysample = 0;
zsample = 0;
for(int i = 0; i < samples; i++)
{
xsample += analogRead(x);
ysample += analogRead(y);
zsample += analogRead(z);
}
// Average after reset
xsample /= samples;
ysample /= samples;
zsample /= samples;
Serial.println("Potentiometer reset values: ");
Serial.println(xsample);
Serial.println(ysample);
Serial.println(zsample);
}
// Trigger fall confirmation based on abnormal heart rate
void triggerFallConfirmation()
{
Serial.println("Please press the confirmation button if fall has occured.");
}
/* END CODE */