#include <Wire.h>
#include <MPU6050.h>
int heartRatePin = 32;
int gsrPin = 35;
int pumpPin = 2;
// Defining the airbag pin
#define airbagPin 13
// Setting thresholds for detecting seizure-like movements
#define ACCEL_THRESHOLD 20000 // Expected threshold for detecting sudden acceleration
#define GYRO_THRESHOLD 5000 // Expected threshold for detecting rapid rotation
// Creating an MPU6050 object
MPU6050 mpu;
const int heartRateHigh = 220;
const int heartRateLow = 40;
const int GSR = 600;
//int volume;
//int rate;
unsigned long relayActivationTime = 0;
unsigned long relayOnDuration ;//= volume / rate; // هتتحدد بناءا علس input هيحددها المريض فس البرنامج
void activateRelay()
{
digitalWrite(pumpPin, HIGH);
relayActivationTime = millis();
}
void deactivateRelay()
{
digitalWrite(pumpPin, LOW);
relayActivationTime = 0;
Serial.println("Medicine pump deactivated.");
}
// Setup function runs once when the program starts
void setup() {
// Starting the serial communication
Serial.begin(115200);
Wire.begin();
// Initializing the MPU6050 sensor
mpu.initialize();
pinMode(pumpPin, OUTPUT);
digitalWrite(pumpPin, LOW);
Serial.println("Seizure predication system intialization");
// Reading the value from Accelerometer configuration register to check the accelerometer range
uint8_t accelConfig = mpu.getFullScaleAccelRange();
// Checking the set range of the accelerometer
if (accelConfig == 0) {
Serial.println("Accelerometer is set to ±2g");
} else if (accelConfig == 1) {
Serial.println("Accelerometer is set to ±4g");
} else if (accelConfig == 2) {
Serial.println("Accelerometer is set to ±8g");
} else if (accelConfig == 3) {
Serial.println("Accelerometer is set to ±16g");
}
// Read the value from gyroscope configuration register to check the gyroscope range
uint8_t gyroConfig = mpu.getFullScaleGyroRange();
// Checking the set range of the gyroscope
if (gyroConfig == 0) {
Serial.println("Gyroscope is set to ±250°/s");
} else if (gyroConfig == 1) {
Serial.println("Gyroscope is set to ±500°/s");
} else if (gyroConfig == 2) {
Serial.println("Gyroscope is set to ±1000°/s");
} else if (gyroConfig == 3) {
Serial.println("Gyroscope is set to ±2000°/s");
}
// Initializing the airbag pin (this was previously the relay pin)
pinMode(airbagPin, OUTPUT);
digitalWrite(airbagPin, LOW); // Airbag is initially off
// Checking the connection of the sensor
if (mpu.testConnection()) {
Serial.println("MPU6050 connection successful");
} else {
Serial.println("MPU6050 connection failed");
while (1); // Stopping the program if the sensor is not connected
}
}
// Loop function runs continuously after the setup
void loop() {
int heartValue = analogRead(heartRatePin);
int gsrValue = analogRead(gsrPin);
Serial.print("Heart Rate: ");
Serial.println(heartValue);
Serial.print("Skin Electricity: ");
Serial.println(gsrValue);
if ((heartRatePin > heartRateHigh || heartRatePin < heartRateLow) && gsrValue > GSR)
{
Serial.println("Abnormal conditions detected, Activating medicine pump..");
activateRelay(); // المفروض هنا تحسب كميه معينه من الدوا من البرنامج تضغه وتقفل
}
if(millis() - relayActivationTime >= relayOnDuration && relayActivationTime)
{
deactivateRelay();
}
// Reading the raw accelerometer and gyroscope data
int16_t ax, ay, az, gx, gy, gz;
mpu.getAcceleration(&ax, &ay, &az);
mpu.getRotation(&gx, &gy, &gz);
// Printing the accelerometer and gyroscope data to the serial monitor
Serial.print("Accel X: "); Serial.print(ax);
Serial.print(" Y: "); Serial.print(ay);
Serial.print(" Z: "); Serial.println(az);
Serial.print("Gyro X: "); Serial.print(gx);
Serial.print(" Y: "); Serial.print(gy);
Serial.print(" Z: "); Serial.println(gz);
// Checking if the acceleration exceeds the threshold (indicating a possible fall or seizure)
if (abs(ax) > ACCEL_THRESHOLD || abs(ay) > ACCEL_THRESHOLD || abs(az) > ACCEL_THRESHOLD) {
Serial.println("Sudden acceleration detected! The airbag is inflated.");
digitalWrite(airbagPin, HIGH); // Turning ON the airbag
delay(2000); // Keeping the airbag ON for 2 seconds
digitalWrite(airbagPin, LOW); // Turning OFF the airbag
}
// Checking if the gyroscope data exceeds the threshold (indicating rapid rotation)
if (abs(gx) > GYRO_THRESHOLD || abs(gy) > GYRO_THRESHOLD || abs(gz) > GYRO_THRESHOLD) {
Serial.println("Sudden rotation detected! The airbag is inflated.");
digitalWrite(airbagPin, HIGH); // Turning ON the airbag
delay(2000); // Keeping the airbag ON for 2 seconds
digitalWrite(airbagPin, LOW); // Turning OFF the airbag
}
delay(2000);
}