#include "SPI.h"
#include "Adafruit_GFX.h"
#include <Adafruit_ILI9341.h> // Hardware-specific library for ILI9341
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <DallasTemperature.h>
#define TFT_CS 10
#define TFT_RST 9
#define TFT_DC 8
#define FirstPotentio A0
#define SecondPotentio A1
#define ThirdPotentio A2
#define SelfDefinePulse 2
#define DallasTempComp 3
const float stepcountmin = 1.5f;
// Initialize the display
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
//Improved imports
Adafruit_MPU6050 mpu;
volatile int heartpulse;
OneWire onewire(DallasTempComp);
DallasTemperature temperaturesensor(&onewire);
void AddHeartRate(){
heartpulse++;
}
void setup() {
Serial.begin(9600);
// Initialize the display
tft.begin();
// Set up the text color and size
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
// Clear the screen
tft.fillScreen(ILI9341_BLACK);
//initialize potentiometers
pinMode(FirstPotentio, INPUT);
pinMode(SecondPotentio, INPUT);
pinMode(ThirdPotentio, INPUT);
//initialize accelerometer
while (!mpu.begin()) {
Serial.println("Model not connected");
delay(1000);
}
Serial.println("Ready to use"); //A4 and A5 used
pinMode(SelfDefinePulse, INPUT);
attachInterrupt(digitalPinToInterrupt(SelfDefinePulse), AddHeartRate, RISING);
Serial.println("INPUT OK for Pulse sensor");
temperaturesensor.begin();
Serial.println("Temp. sensor ready");
}
int CriticalPulse(int pulse){
if(pulse < 40 || pulse > 200){
return 1;
} else {
return 0;
}
}
int count = 0;
int steps = 0;
sensors_event_t traceevent;
void loop() {
tft.setTextColor(ILI9341_WHITE);
// Print the third sentence on the third line
tft.setCursor(0, 20);
tft.fillRect(0, 10, 240, 30, ILI9341_BLACK); // Clear the area where the third sentence was
Serial.println("Duration: "+String(count)+"s");
tft.println("Duration: "+String(count)+"s");
count++;
float indexA0 = analogRead(FirstPotentio);
float sp02rate = map(indexA0, 0, 1023, 0, 100);
tft.fillRect(0, 40, 240, 30, ILI9341_BLACK);
tft.setCursor(0, 50);
Serial.println("SPO2: "+String(sp02rate)+"%");
tft.println("SPO2: "+String(sp02rate)+"%");
float indexA1 = analogRead(SecondPotentio);
float sysPressure = map(indexA1, 0, 1023, 0, 256);
tft.fillRect(0, 70, 240, 30, ILI9341_BLACK);
tft.setCursor(0, 80);
Serial.println("SYS: "+String(sysPressure)+"mmHg");
tft.println("SYS: "+String(sysPressure)+"mmHg");
float indexA2 = analogRead(ThirdPotentio);
float diaPressure = map(indexA2, 0, 1023, 0, 256);
tft.fillRect(0, 100, 240, 30, ILI9341_BLACK);
tft.setCursor(0, 110);
Serial.println("DIA: "+String(diaPressure)+"mmHg");
tft.println("DIA: "+String(diaPressure)+"mmHg");
mpu.getAccelerometerSensor()->getEvent(&traceevent);
float cartacc = sqrt(sq(traceevent.acceleration.x)+sq(traceevent.acceleration.y)+sq(traceevent.acceleration.z));
if(cartacc > stepcountmin){
steps++;
}
tft.fillRect(0, 160, 240, 30, ILI9341_BLACK);
tft.setCursor(0, 170);
Serial.println("Steps: "+String(steps)+" steps");
tft.println("Steps: "+String(steps)+" steps");
int heartpulseparse = heartpulse;
heartpulse = 0;
heartpulseparse = map(heartpulseparse, 0, 220, 0 , 220);
tft.fillRect(0, 200, 240, 30, ILI9341_BLACK);
tft.setCursor(0, 210);
Serial.println("Heart rate: "+String(heartpulseparse)+" BPM");
tft.println("Heart rate: "+String(heartpulseparse)+" BPM");
temperaturesensor.requestTemperatures();
float temperature = temperaturesensor.getTempCByIndex(0);
tft.fillRect(0, 230, 240, 30, ILI9341_BLACK);
tft.setCursor(0, 240);
Serial.println("Body Temperature: "+String(temperature)+" C");
tft.println("Body temp.: "+String(temperature)+" C");
tft.fillRect(0, 130, 240, 30, ILI9341_BLACK);
if(sp02rate < 95.0f || (sysPressure > 140.0f && diaPressure > 90.0f) ||
(sysPressure < 90.0f && diaPressure < 60.0f) || temperature > 37.5f
|| temperature < 35.0f){
tft.setCursor(0, 140); //temp
tft.setTextColor(ILI9341_RED);
Serial.println("WARNING");
tft.println("WARNING");
}
if(CriticalPulse(heartpulseparse) == 1){
Serial.println("AWARE PULSE");
tft.setCursor(0,270);
tft.println("AWARE PULSE");
}
delay(1000); // Delay for 1 second before printing again
}SPO2
SysPressure
DiaPressure