Skip to content

Instantly share code, notes, and snippets.

@Mayoogh
Created March 18, 2021 14:00
Show Gist options
  • Save Mayoogh/d652102d239d9a4fb9c63164b86fe88c to your computer and use it in GitHub Desktop.
Save Mayoogh/d652102d239d9a4fb9c63164b86fe88c to your computer and use it in GitHub Desktop.
//#define DEBUG_SERIAL // Uncomment for Serial Monitor
#ifdef DEBUG_SERIAL
#define DEBUG_SERIAL_BEGIN(x) Serial.begin(x)
#define DEBUG_SERIAL_PRINT(x) Serial.print(x)
#define DEBUG_SERIAL_PRINTLN(x) Serial.println(x)
#else
#define DEBUG_SERIAL_BEGIN(x)
#define DEBUG_SERIAL_PRINT(x)
#define DEBUG_SERIAL_PRINTLN(x)
#endif
#define BT_SERIAL // Uncomment for Bluetooth communication
#ifdef BT_SERIAL
#define BT_SERIAL_BEGIN(x) Serial.begin(x)
#define BT_SERIAL_PRINT(x) Serial.print(x)
#define BT_SERIAL_PRINTLN(x) Serial.println(x)
#else
#define BT_SERIAL_BEGIN(x)
#define BT_SERIAL_PRINT(x)
#define BT_SERIAL_PRINTLN(x)
#endif
#define USE_ARDUINO_INTERRUPTS true
#include <PulseSensorPlayground.h>
#include <math.h>
#include<LiquidCrystal.h>
const int PulseWire = A0;
const int LED13 = 13;
int Threshold, Signal, BPMsum, count, avgBPM, SpO2round, Contrast, x, i;
float avgstress, BPSsum, avgRawData, sugar, diabetes, stress, RawDataSum;
double pq;
PulseSensorPlayground pulseSensor;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup()
{
DEBUG_SERIAL_BEGIN(9600);
BT_SERIAL_BEGIN(9600);
Signal = BPMsum = count = avgBPM = SpO2round = x = i = 0;
avgstress = BPSsum = avgRawData = sugar = diabetes = stress = RawDataSum = 0;
Threshold = 550;
Contrast = 75;
pulseSensor.analogInput(PulseWire);
pulseSensor.blinkOnPulse(LED13);
pulseSensor.setThreshold(Threshold);
if (pulseSensor.begin())
{
DEBUG_SERIAL_PRINTLN("We created a pulseSensor Object !");
}
analogWrite(6, Contrast);
lcd.begin(16, 2);
lcd.setCursor(5, 0);
lcd.print("Welcome");
delay(1000);
lcd.clear();
lcd.print("Loading...");
delay(1000);
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("Place your");
lcd.setCursor(5, 2);
lcd.print("finger");
}
void loop()
{
Signal = analogRead(PulseWire);
if ((Signal > 500) && (Signal < 900))
{
pq = Signal;
}
double SPO2 = (log10(620 * pq) / log10(940 * pq)) * 100 ;
if (pulseSensor.sawStartOfBeat())
{
float myBPM = pulseSensor.getBeatsPerMinute();
float BPS = myBPM / 60;
float TofB = 1 / BPS;
float avgBPS;
float avgBeatDuration;
float BeatDuration;
float stress;
float i, j, k, z;
i=j=k=z=0; //Prevent junk values
if (myBPM == 0)
{
count = 0;
}
else
{
BPMsum = BPMsum + pulseSensor.getBeatsPerMinute(); //BPM sum
count = count + 1; //count
avgBPM = BPMsum / count; //average BPM
BPSsum = BPSsum + BPS; //BPS sum
avgBPS = BPSsum / count; //average BPS
avgBeatDuration = 1 / avgBPS; //average duration of beat
BeatDuration = 1 / BPS; //duration of beat
stress = (BeatDuration - avgBeatDuration) * 1000; //stress
z = stress * stress; // Stress^2 ?
i = i + z; // .....
j = i / count; // .....
stress = sqrt(j); // ..... Removed K
avgstress = stress / count;
SpO2round = round(SPO2); //Round to nearest int
RawDataSum = RawDataSum + Signal;
x = count % 10;
avgRawData = RawDataSum / count;
sugar = (1046.72 - avgRawData) / 5.885;
diabetes = sugar + 70.46;
}
DEBUG_SERIAL_PRINT("Sensor Data: ");
DEBUG_SERIAL_PRINTLN(Signal);
DEBUG_SERIAL_PRINTLN("♥ A HeartBeat Happened ! ");
DEBUG_SERIAL_PRINT("BPM: ");
DEBUG_SERIAL_PRINTLN(myBPM);
// DEBUG_SERIAL_PRINT("BPM Sum: ");
// DEBUG_SERIAL_PRINTLN(BPMsum);
DEBUG_SERIAL_PRINT("Count: ");
DEBUG_SERIAL_PRINTLN(count);
DEBUG_SERIAL_PRINT("Average BPM: ");
DEBUG_SERIAL_PRINTLN(avgBPM);
// DEBUG_SERIAL_PRINT("BPS: ");
// DEBUG_SERIAL_PRINTLN(BPS);
//
// DEBUG_SERIAL_PRINT("Average BPS: ");
// DEBUG_SERIAL_PRINTLN(avgBPS);
//
// DEBUG_SERIAL_PRINT("Average duration of a Beat: ");
// DEBUG_SERIAL_PRINTLN(avgBeatDuration);
//
// DEBUG_SERIAL_PRINT("Stress: ");
// DEBUG_SERIAL_PRINTLN(stress);
// DEBUG_SERIAL_PRINT("SPO2: ");
// DEBUG_SERIAL_PRINTLN(SPO2);
//
// DEBUG_SERIAL_PRINT("SENSOR DATA:");
// DEBUG_SERIAL_PRINTLN(Signal);
//
// DEBUG_SERIAL_PRINT("SENSOR DATA SUM: ");
// DEBUG_SERIAL_PRINTLN(RawDataSum);
//
// DEBUG_SERIAL_PRINT("COUNT: ");
// DEBUG_SERIAL_PRINTLN(count);
//
// DEBUG_SERIAL_PRINT("AVERAGE SENSOR DATA: ");
// DEBUG_SERIAL_PRINTLN(avgRawData);
DEBUG_SERIAL_PRINT("Blood Sugar: ");
DEBUG_SERIAL_PRINTLN(sugar);
DEBUG_SERIAL_PRINT("Blood Sugar(diabetes): ");
DEBUG_SERIAL_PRINTLN(diabetes);
/////////////////////////////////////////////////////// LCD - Bluetooth //////////////////////////////////////////////////
lcd.clear();
if ((x == 0) && (count != 0))
{
lcd.clear();
lcd.print("BPM: ");
lcd.setCursor(0, 1);
lcd.print(myBPM);
// BT_SERIAL_PRINTLN();
BT_SERIAL_PRINT("!"); // BPM identifier symbol !
BT_SERIAL_PRINTLN(myBPM);
delay(1500);
lcd.clear();
lcd.print("Blood sugar(mg/dl): ");
lcd.setCursor(0, 1);
lcd.print(sugar);
BT_SERIAL_PRINT("$"); // Blood sugar identifier symbol $
BT_SERIAL_PRINTLN(sugar);
delay(1500);
lcd.clear();
lcd.print("SPO2(%): ");
lcd.setCursor(0, 1);
lcd.print(SpO2round);
BT_SERIAL_PRINT("~"); // SpO2 identifier symbol ~
BT_SERIAL_PRINTLN(SpO2round);
delay(1500);
lcd.clear();
lcd.print("Stress level: ");
lcd.setCursor(0, 1);
if ((stress > 10) && (stress < 100))
{
lcd.print("Normal");
BT_SERIAL_PRINTLN("@Normal"); //@ Normal
}
else if ((stress > 0) && (stress < 10))
{
lcd.print("Average");
BT_SERIAL_PRINTLN("#Average"); //# Average
}
else if ((stress > 100) && (stress < 200))
{
lcd.print("Quitely tensed");
BT_SERIAL_PRINTLN("%Quitely tensed"); //% Quitely tensed
}
delay(1500);
lcd.clear();
lcd.print("Try again.. ");
}
else
{
lcd.print("Taking samples...");
BT_SERIAL_PRINT("*");
BT_SERIAL_PRINTLN();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment