Skip to content

Instantly share code, notes, and snippets.

@Aabill
Last active November 25, 2021 10:47
Show Gist options
  • Save Aabill/69755a5284db987fff985c29ac9bcfcb to your computer and use it in GitHub Desktop.
Save Aabill/69755a5284db987fff985c29ac9bcfcb to your computer and use it in GitHub Desktop.
Arduino TFT LCD, BP, SPO2, HC-RS04, HX711
#include <Adafruit_GFX.h>
#include <Adafruit_GrayOLED.h>
#include <Adafruit_SPITFT.h>
#include <Adafruit_SPITFT_Macros.h>
#include <gfxfont.h>
//#if 1
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <TouchScreen.h>
#define MINPRESSURE 300
#define MAXPRESSURE 1000
// PULSERATE & OXYGENLEVEL
#include <Wire.h>
#include <heartRate.h>
#include <MAX30105.h>
#include <spo2_algorithm.h>
#include <Adafruit_MLX90614.h>
//#include "MAX30105.h"
MAX30105 particleSensor;
Adafruit_MLX90614 mlx = Adafruit_MLX90614();
const byte RATE_SIZE = 4; //Increase this for more averaging. 4 is good.
byte rates[RATE_SIZE]; //Array of heart rates
byte rateSpot = 0;
long lastBeat = 0; //Time at which the last beat occurred
float beatsPerMinute;
int beatAvg;
//OL
#define MAX_BRIGHTNESS 255
#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
//Arduino Uno doesn't have enough SRAM to store 100 samples of IR led data and red led data in 32-bit format
//To solve this problem, 16-bit MSB of the sampled data will be truncated. Samples become 16-bit data.
uint16_t irBuffer[100]; //infrared LED sensor data
uint16_t redBuffer[100]; //red LED sensor data
#else
uint32_t irBuffer[100]; //infrared LED sensor data
uint32_t redBuffer[100]; //red LED sensor data
#endif
int32_t bufferLength; //data length
int32_t spo2; //SPO2 value
int8_t validSPO2; //indicator to show if the SPO2 calculation is valid
int32_t heartRate; //heart rate value
int8_t validHeartRate; //indicator to show if the heart rate calculation is valid
byte pulseLED = 11; //Must be on PWM pin
byte readLED = 13; //Blinks with each data read
//end OL
// END PULSERATE & OXYGEN LEVEL
// ALL Touch panels and wiring is DIFFERENT
// copy-paste results from TouchScreen_Calibr_native.ino
const int XP = 6, XM = A2, YP = A1, YM = 7; //ID=0x9341
const int TS_LEFT = 907, TS_RT = 136, TS_TOP = 942, TS_BOT = 139;
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
// Initialize TFT Buttons
Adafruit_GFX_Button A_btn, B_btn, C_btn, D_btn, E_btn, F_btn, G_btn, H_btn, I_btn, J_btn, K_btn, L_btn, M_btn, N_btn, O_btn, P_btn, Q_btn, R_btn, S_btn, T_btn, U_btn, V_btn, W_btn, X_btn, Y_btn, Z_btn, DEL_btn, SPACE_btn, ENTER_btn, CLEAR_btn, one_btn,
two_btn, three_btn, four_btn, five_btn, six_btn, seven_btn, eight_btn, nine_btn, zero_btn, male_btn, female_btn, reset_btn;//on_btn, off_btn,;
Adafruit_GFX_Button *keyButtons[] = { &Q_btn, &W_btn, &E_btn, &R_btn, &T_btn, &Y_btn, &U_btn, &I_btn, &O_btn, &P_btn, &A_btn, &S_btn, &D_btn, &F_btn, &G_btn, &H_btn, &J_btn, &K_btn, &L_btn, &Z_btn, &X_btn, &C_btn, &V_btn, &B_btn, &N_btn, &M_btn };
Adafruit_GFX_Button *numButtons[] = { &one_btn, &two_btn, &three_btn, &four_btn, &five_btn, &six_btn, &seven_btn, &eight_btn, &nine_btn, &zero_btn};
const char* line1[] = {"Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"};
const char* line2[] = {"A", "S", "D", "F", "G", "H", "J", "K", "L"};
const char* line3[] = {"Z", "X", "C", "V", "B", "N", "M"};
const char* ageLine[] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"};
int pixel_x, pixel_y; //Touch_getXY() updates global vars
// BP Variables
#include <SoftwareSerial.h>
SoftwareSerial mySerial(50, 51); // RX, TX
// Mass Weight & HEIGHT Variables
#include <HX711_ADC.h>
#if defined(ESP8266)|| defined(ESP32) || defined(AVR)
#include <EEPROM.h>
#endif
//pins:
const int HX711_dout = 37; //mcu > HX711 dout pin
const int HX711_sck = 36; //mcu > HX711 sck pin
//HX711 constructor:
HX711_ADC LoadCell(HX711_dout, HX711_sck);
const int calVal_calVal_eepromAdress = 0;
unsigned long Masst = 0;
// Height
#define echoPin 31 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 30 //attach pin D3 Arduino to pin Trig of HC-SR04
// defines variables
long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement
// Printer Settings
#include "Adafruit_Thermal.h"
//#include "adalogo.h"
//#include "adaqrcode.h"
// Here's the new syntax when using SoftwareSerial (e.g. Arduino Uno) ----
// If using hardware serial instead, comment out or remove these lines:
#include "SoftwareSerial.h"
#define TX_PIN 53 // Arduino transmit YELLOW WIRE labeled RX on printer
#define RX_PIN 52 // Arduino receive GREEN WIRE labeled TX on printer
SoftwareSerial printerSerial(RX_PIN, TX_PIN); // Declare SoftwareSerial obj first
Adafruit_Thermal printer(&printerSerial); // Pass addr to printer constructor
void(* resetFunc) (void) = 0; // declare reset fuction at address 0
bool Touch_getXY(void)
{
TSPoint p = ts.getPoint();
pinMode(YP, OUTPUT); //restore shared pins
pinMode(XM, OUTPUT);
digitalWrite(YP, HIGH); //because TFT control pins
digitalWrite(XM, HIGH);
bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
if (pressed) {
pixel_x = map(p.x, TS_LEFT, TS_RT, 0, tft.width()); //.kbv makes sense to me
pixel_y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());
}
return pressed;
}
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
// Variables Global
struct PRINT {
String Name;
String Age;
String Gender;
String BP;
String PulseRate;
String PulseRateAVG;
String PulseRateBPM;
String OxygenLevel;
String Height;
String Weight;
String BodyTemperature;
String BodyStatus;
float metricHeight;
float massWeight;
String BMI;
String Print;
};
PRINT person = {};
String patientStatus = "";
String textInput = "";
String ageInput = "";
// String Name = "";
//
// String Age = "";
// String Gender = "";
// String PulseRateAVG = "";
// String PulseRateBPM = "";
// String SPO2 = "";
void inputValue(String val) {
tft.fillRect(10, 50, 300, 60, BLACK);
tft.setTextSize(2);
tft.setCursor(12, 55);
tft.print(val);
}
void inputQuestion(String text, bool showInput = true, String showValue = "", int yPointer = 15) {
tft.fillRect(10, yPointer - 5, 300, 30, BLUE);
tft.setCursor(10, yPointer);
tft.setTextSize(2);
tft.setTextColor(WHITE);
tft.print(" " + text);
if (showInput) {
tft.fillRect(10, 50, 300, 60, BLACK);
tft.setCursor(15, 55);
tft.print(showValue);
}
}
void nameSetup(void) {
tft.fillScreen(WHITE);
inputQuestion("Enter your name.");
int buttonPlacefromLeft = 0;
for ( int i = 0; i < 10; i++) {
int buttonSpace = 30;
if (i == 0) {
buttonSpace = 25;
}
buttonPlacefromLeft = buttonPlacefromLeft + buttonSpace;
keyButtons[i]->initButton(&tft, buttonPlacefromLeft, 140, 30, 40, WHITE, CYAN, BLACK, line1[i], 2);
keyButtons[i]->drawButton(false);
}
buttonPlacefromLeft = 10;
for ( int i = 10; i > 9 && i < 19; i++) {
int buttonSpace = 30;
buttonPlacefromLeft = buttonPlacefromLeft + buttonSpace;
keyButtons[i]->initButton(&tft, buttonPlacefromLeft, 180, 30, 40, WHITE, CYAN, BLACK, line2[i - 10], 2);
keyButtons[i]->drawButton(false);
}
buttonPlacefromLeft = 40;
for ( int i = 19; i > 18 && i < 26; i++) {
int buttonSpace = 30;
buttonPlacefromLeft = buttonPlacefromLeft + buttonSpace;
keyButtons[i]->initButton(&tft, buttonPlacefromLeft, 220, 30, 40, WHITE, CYAN, BLACK, line3[i - 19], 2);
keyButtons[i]->drawButton(false);
}
// Initialize Del Button
DEL_btn.initButton(&tft, 50, 270, 60, 40, WHITE, RED, WHITE, "DEL", 2);
DEL_btn.drawButton(false);
// Initialize Clear Button
CLEAR_btn.initButton(&tft, 50, 320, 70, 40, WHITE, RED, WHITE, "CLEAR", 2);
CLEAR_btn.drawButton(false);
// Intialize Space Button
SPACE_btn.initButton(&tft, 160, 260, 90, 40, WHITE, CYAN, BLACK, "SPACE", 2);
SPACE_btn.drawButton(false);
// Initialize Enter Button
ENTER_btn.initButton(&tft, 260, 320, 80, 40, WHITE, BLUE, WHITE, "ENTER", 2);
ENTER_btn.drawButton(false);
}
void ageSetup(void) {
tft.fillScreen(WHITE);
const String firstName = getSplitValue(person.Name, ' ', 0);
inputQuestion(firstName + "'S AGE IS:");
int buttonPlacefromLeft = 0;
int buttonSpace = 50;
int topPad = 140;
for ( int i = 0; i < 10; i++) {
if ( i == 3 || i == 6 ) {
topPad = topPad + 50;
buttonPlacefromLeft = 0;
}
buttonPlacefromLeft = buttonPlacefromLeft + buttonSpace;
if ( i == 9 ) {
topPad = 290;
buttonPlacefromLeft = 100;
}
numButtons[i]->initButton(&tft, buttonPlacefromLeft, topPad, 50, 50, WHITE, CYAN, BLACK, ageLine[i], 2);
numButtons[i]->drawButton(false);
}
// Initialize Del Button
DEL_btn.initButton(&tft, 220, 140, 60, 40, WHITE, RED, WHITE, "DEL", 2);
DEL_btn.drawButton(false);
// Initialize Enter Button
ENTER_btn.initButton(&tft, 220, 290, 80, 40, WHITE, BLUE, WHITE, "ENTER", 2);
ENTER_btn.drawButton(false);
}
void choiceSetup(String question, int yPointer = 0) {
if (yPointer > 0) {
inputQuestion(question, false, "", yPointer);
} else {
inputQuestion(question, false);
}
if (patientStatus == "getGender") {
one_btn.initButton(&tft, 110, 100, 100, 100, WHITE, CYAN, BLACK, "MALE", 2);
one_btn.drawButton(false);
zero_btn.initButton(&tft, 210, 100, 100, 100, WHITE, CYAN, BLACK, "FEMALE", 2);
zero_btn.drawButton(false);
}
if (patientStatus == "getPrint") {
one_btn.initButton(&tft, 110, 370, 100, 100, WHITE, CYAN, BLACK, "NO", 2);
one_btn.drawButton(false);
zero_btn.initButton(&tft, 210, 370, 100, 100, WHITE, CYAN, BLACK, "YES", 2);
zero_btn.drawButton(false);
}
}
void resetSetup() {
reset_btn.initButton(&tft, 155, 200, 100, 100, WHITE, CYAN, BLACK, "RESET", 2);
reset_btn.drawButton(false);
}
void BPSETUP(void) {
tft.fillScreen(WHITE);
const String firstName = getSplitValue(person.Name, ' ', 0);
inputQuestion("GETTING " + firstName + "'S BP", true, "Please Start BP & wait.");
mySerial.begin(4800);
}
void WEIGHTSETUP(void) {
// Start Weight Setup
delay(10);
Serial.println("Starting...");
float calibrationValue; // calibration value
calibrationValue = 25972.72;//696.0 - 21.33; // uncomment this if you want to set this value in the sketch
#if defined(ESP8266) || defined(ESP32)
//EEPROM.begin(512); // uncomment this if you use ESP8266 and want to fetch this value from eeprom
#endif
//EEPROM.get(calVal_eepromAdress, calibrationValue); // uncomment this if you want to fetch this value from eeprom
LoadCell.begin();
//LoadCell.setReverseOutput();
unsigned long stabilizingtime = 2000; // tare preciscion can be improved by adding a few seconds of stabilizing time
boolean _tare = true; //set this to false if you don't want tare to be performed in the next step
LoadCell.start(stabilizingtime, _tare);
if (LoadCell.getTareTimeoutFlag()) {
Serial.println("Timeout, check MCU>HX711 wiring and pin designations");
}
else {
LoadCell.setCalFactor(calibrationValue); // set calibration factor (float)
Serial.println("Startup is complete");
}
// while (!LoadCell.update());
// Serial.print("Calibration value: ");
// Serial.println(LoadCell.getCalFactor());
// Serial.print("HX711 measured conversion time ms: ");
// Serial.println(LoadCell.getConversionTime());
// Serial.print("HX711 measured sampling rate HZ: ");
// Serial.println(LoadCell.getSPS());
// Serial.print("HX711 measured settlingtime ms: ");
// Serial.println(LoadCell.getSettlingTime());
// Serial.println("Note that the settling time may increase significantly if you use delay() in your sketch!");
// if (LoadCell.getSPS() < 7) {
// Serial.println("!!Sampling rate is lower than specification, check MCU>HX711 wiring and pin designations");
// }
// else if (LoadCell.getSPS() > 100) {
// Serial.println("!!Sampling rate is higher than specification, check MCU>HX711 wiring and pin designations");
// }
}
void HEIGHTSETUP() {
// Start Height Setup
pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
Serial.begin(9600); // // Serial Communication is starting with 9600 of baudrate speed
Serial.println("Ultrasonic Sensor HC-SR04 Test"); // print some text in Serial Monitor
Serial.println("with Arduino UNO R3");
}
bool detectWeight = false;
bool gettingName = false;
bool gettingAge, hasName, hasAge, hasGender, hasBP, hasPulseRate, hasOxygenLevel, hasHeight, hasWeight, hasTemp, hasPrint, gettingGender, gettingBP, gettingPulseRate, gettingOL, gettingHeight, gettingWeight, gettingTemp, gettingPrint, printNow = false;
void setup(void)
{
Serial.begin(9600);
Serial1.begin(9600);
uint16_t ID = tft.readID();
Serial.print("TFT ID = 0x");
Serial.println(ID, HEX);
Serial.println("Calibrate for your Touch Panel");
if (ID == 0xD3D3) ID = 0x9486; // write-only shield
tft.begin(ID);
tft.setRotation(0); //PORTRAIT
// pulseRateOxygenSETUP();
WEIGHTSETUP();
inputQuestion("Step on the platform.", false);
}
/* two buttons are quite simple
*/
void pulseRateOxygenSETUP()
{
Serial.println("Initializing pulseRateSensor...");
mlx.begin();
// Initialize sensor
if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
{
Serial.println("MAX30105 was not found. Please check wiring/power. ");
while (1);
}
Serial.println("Place your index finger on the sensor with steady pressure.");
particleSensor.setup(); //Configure sensor with default settings
particleSensor.setPulseAmplitudeRed(0x0A); //Turn Red LED to low to indicate sensor is running
particleSensor.setPulseAmplitudeGreen(0); //Turn off Green LED
}
void OLSETUP() {
pinMode(pulseLED, OUTPUT);
pinMode(readLED, OUTPUT);
mlx.begin();
// Initialize sensor
if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
{
Serial.println(F("MAX30105 was not found. Please check wiring/power."));
while (1);
}
// Serial.println(F("Attach sensor to finger with rubber band. Press any key to start conversion"));
// while (Serial.available() == 0) ; //wait until user presses a key
// Serial.read();
byte ledBrightness = 60; //Options: 0=Off to 255=50mA
byte sampleAverage = 4; //Options: 1, 2, 4, 8, 16, 32
byte ledMode = 2; //Options: 1 = Red only, 2 = Red + IR, 3 = Red + IR + Green
byte sampleRate = 100; //Options: 50, 100, 200, 400, 800, 1000, 1600, 3200
int pulseWidth = 411; //Options: 69, 118, 215, 411
int adcRange = 4096; //Options: 2048, 4096, 8192, 16384
particleSensor.setup(ledBrightness, sampleAverage, ledMode, sampleRate, pulseWidth, adcRange); //Configure sensor with these settings
}
void loop(void)
{
// Serial.println("Test Mode");
// if (PulseRateAVG == "") {
// pulseRateOxygen();
// }
if (patientStatus == "getName") {
inputName();
Serial.println("Getting Name");
if (hasName) {
gettingAge = true;
}
}
if (patientStatus == "getAge") {
inputAge();
Serial.println("Getting Age");
if (hasAge) {
gettingGender = true;
}
}
if (patientStatus == "getGender") {
choosing("MALE", "FEMALE");
Serial.println("Choosing Gender");
if (hasGender) {
gettingBP = true;
}
}
if (patientStatus == "getBP") {
Serial.println("Getting BP");
loopBP();
if (hasBP) {
gettingOL = true;
}
}
// if (patientStatus == "getPulseRate") {
// pulseRateOxygen();
// if (hasPulseRate) {
//// inputQuestion("Pulse Rate is:", true, PulseRateAVG);
// gettingOL = true;
// }
// }
if (patientStatus == "getOL") {
loopOL();
if (hasOxygenLevel) {
gettingHeight = true;
}
}
if (patientStatus == "getHeight") {
loopHeight();
if (hasHeight) {
gettingWeight = true;
}
}
if (patientStatus == "getWeight") {
// loopWeight();
float weight;
for (int i = 0; i <= 49; i++) {
weight = loopWeight();
if (weight > 0 && i == 49) {
String massKG = "";
massKG.concat(weight);
massKG.concat(" kg");
person.Weight = massKG;
if (person.Weight.length() > 0) {
person.massWeight = weight;
hasWeight = true;
}
}
}
if (hasWeight) {
gettingTemp = true;
}
// Compute BMI since we have Height and Weight from &person (object);
if (person.metricHeight > 0 && person.massWeight > 0) {
float BMI = person.massWeight / (person.metricHeight * person.metricHeight);
if (BMI <= 18.5) {
person.BMI = "Underweight";
}
else if (BMI >= 18.51 && BMI <= 24.9) {
person.BMI = "Normal";
}
else if (BMI >= 25 && BMI <= 29.9) {
person.BMI = "Overweight";
}
else {
person.BMI = "Obese";
}
}
}
if (patientStatus == "getTemp") {
loopTemp();
if (hasTemp) {
gettingPrint = true;
}
}
if (patientStatus == "getPrint") {
choosing("NO", "YES");
if (hasPrint) {
Serial.print("Print Selected: ");
Serial.println(person.Print);
printNow = true;
}
}
if (patientStatus == "printingResult") {
resetting();
}
// start here. Start weight
if (!detectWeight) {
float detectedWeight = loopWeight();
if (detectedWeight > 0.99) {
gettingName = true;
detectWeight = true;
}
Serial.print("Detected Weight: ");
Serial.println(detectedWeight);
}
if (detectWeight) {
if (!hasGender) {
float detectedWeight = loopWeight();
}
}
// gettingName - Set True at inialization to start;
if (gettingName) {
Serial.println("inputName");
patientStatus = "getName";
nameSetup();
gettingName = false;
}
if (gettingAge) {
Serial.println("inputAge");
patientStatus = "getAge";
ageSetup();
gettingAge = false;
}
if (gettingGender) {
Serial.println("Choose Gender");
patientStatus = "getGender";
const String firstName = getSplitValue(person.Name, ' ', 0);
tft.fillScreen(WHITE);
choiceSetup( firstName + "'S GENDER IS:" );
gettingGender = false;
}
if (gettingBP) {
// Serial.println("Skip BP Start Pulse Rate.");
// gettingPulseRate = true;
// gettingBP = false;
Serial.println("Start BP");
patientStatus = "getBP";
gettingBP = false;
BPSETUP();
// Serial.println(Name);
// Serial.println(Age);
// Serial.println(Gender);
}
// if (gettingPulseRate) {
// Serial.println("Start Pulse Rate.");
// patientStatus = "getPulseRate";
// pulseRateOxygenSETUP();
// gettingPulseRate = false;
// tft.fillScreen(WHITE);
// inputQuestion("Getting Pulse Rate...", false);
//// Serial.println(person.Name);
//// Serial.println(person.Age);
//// Serial.println(person.Gender);
//// Serial.println(person.BP);
//// gettingPulseRate = false;
// }
if (gettingOL) {
Serial.println("Start Oxygen Level");
patientStatus = "getOL";
OLSETUP();
gettingOL = false;
tft.fillScreen(WHITE);
inputQuestion("Getting Oxygen Level...", true, "Don't move your finger.");
}
if (gettingHeight) {
Serial.println("Start Height");
patientStatus = "getHeight";
HEIGHTSETUP();
gettingHeight = false;
tft.fillScreen(WHITE);
inputQuestion("Getting Height...", false);
// Serial.println(person.Name);
// Serial.println(person.Age);
// Serial.println(person.Gender);
// Serial.println(person.PulseRate);
//// Serial.println(person.PulseRateBPM);
// Serial.println(person.OxygenLevel);
}
if (gettingWeight) {
Serial.println("Start Weight");
patientStatus = "getWeight";
// WEIGHTSETUP();
gettingWeight = false;
tft.fillScreen(WHITE);
inputQuestion("Getting Weight...", false);
}
if (gettingTemp) {
Serial.println("Start Height and Weight");
mlx.begin();
patientStatus = "getTemp";
gettingTemp = false;
tft.fillScreen(WHITE);
inputQuestion("Getting Temperature", false);
}
if (gettingPrint) {
Serial.println(person.Name);
Serial.println(person.Age);
Serial.println(person.Gender);
Serial.println(person.BP);
Serial.println(person.PulseRate);
Serial.println(person.OxygenLevel);
Serial.println(person.Height);
Serial.println(person.Weight);
Serial.println(person.BodyTemperature);
Serial.println(person.BMI);
gettingPrint = false;
patientStatus = "getPrint";
Serial.println("Display Result & Choose Print");
// TFT LCD Print BMI
inputQuestion("BMI", true, person.BMI);
delay(3000);
tft.fillScreen(WHITE);
tft.setCursor(10, 15);
tft.setTextSize(1);
tft.setTextColor(BLACK);
tft.print("Name: " + person.Name);
tft.setCursor(10, 35);
tft.print("Age: " + person.Age);
tft.setCursor(10, 55);
tft.print("Gender: " + person.Gender);
tft.setCursor(10, 75);
tft.print("BP: " + person.BP);
tft.setCursor(10, 95);
tft.print("Pulse rate: " + person.PulseRate);
tft.setCursor(10, 115);
tft.print("Oxygen level: " + person.OxygenLevel);
tft.setCursor(10, 135);
tft.print("Height: " + person.Height);
tft.setCursor(10, 155);
tft.print("Weight: " + person.Weight);
tft.setCursor(10, 175);
tft.print("Body temperature: " + person.BodyTemperature + " C");
tft.setCursor(10, 195);
tft.print("BMI: " + person.BMI);
choiceSetup( "Print Result?", 275);
}
if (printNow) {
printNow = false;
patientStatus = "printingResult";
if (person.Print == "YES") {
Serial.println("Print NOW!!!");
tft.fillScreen(WHITE);
inputQuestion("Printing result...", true, "Please wait.");
printerSerial.begin(9600); // Initialize SoftwareSerial (Printer)
printer.begin(); // Init printer (same regardless of serial type)
printer.wake();
printer.setFont('B');
printer.println("NAME: " + person.Name);
printer.println("AGE: " + person.Age);
printer.println("GENDER: " + person.Gender);
printer.println("BP: " + person.BP);
printer.println("PULSE RATE: " + person.PulseRate);
printer.println("OXYGEN LEVEL: " + person.OxygenLevel);
printer.println("HEIGHT: " + person.Height);
printer.println("WEIGHT: " + person.Weight);
printer.println("BODY TEMPERATURE: " + person.BodyTemperature + " C");
printer.println("BMI: " + person.BMI);
printer.feed(3);
printer.sleep();
printer.setDefault();
Serial.println("Exit.");
tft.fillScreen(WHITE);
inputQuestion("Done. Thankyou!", false);
delay(3000);
tft.fillScreen(WHITE);
resetSetup();
}
if (person.Print == "NO") {
Serial.println("Exit.");
tft.fillScreen(WHITE);
inputQuestion("Done. Thankyou!", true, "You can go now.");
delay(3000);
tft.fillScreen(WHITE);
resetSetup();
}
}
}
void inputName(void) {
bool down = Touch_getXY();
// Q_btn.press(down && Q_btn.contains(pixel_x, pixel_y));
// if (Q_btn.justReleased()) Q_btn.drawButton();
// if (Q_btn.justPressed()) {
// Q_btn.drawButton(true);
// textInput += "Q";
// inputValue(textInput);
// }
for (int i = 0; i < 10; i++) {
keyButtons[i]->press(down && keyButtons[i]->contains(pixel_x, pixel_y));
if (keyButtons[i]->justReleased()) keyButtons[i]->drawButton();
if (keyButtons[i]->justPressed()) {
keyButtons[i]->drawButton(true);
textInput += line1[i];
inputValue(textInput);
}
}
for (int i = 10; i < 19; i++) {
keyButtons[i]->press(down && keyButtons[i]->contains(pixel_x, pixel_y));
if (keyButtons[i]->justReleased()) keyButtons[i]->drawButton();
if (keyButtons[i]->justPressed()) {
keyButtons[i]->drawButton(true);
textInput += line2[i - 10];
inputValue(textInput);
}
}
for ( int i = 19; i > 18 && i < 26; i++) {
keyButtons[i]->press(down && keyButtons[i]->contains(pixel_x, pixel_y));
if (keyButtons[i]->justReleased()) keyButtons[i]->drawButton();
if (keyButtons[i]->justPressed()) {
keyButtons[i]->drawButton(true);
textInput += line3[i - 19];
inputValue(textInput);
}
}
SPACE_btn.press(down && SPACE_btn.contains(pixel_x, pixel_y));
if (SPACE_btn.justReleased()) SPACE_btn.drawButton();
if (SPACE_btn.justPressed()) {
SPACE_btn.drawButton(true);
textInput += " ";
inputValue(textInput);
}
DEL_btn.press(down && DEL_btn.contains(pixel_x, pixel_y));
if (DEL_btn.justReleased()) DEL_btn.drawButton();
if (DEL_btn.justPressed()) {
DEL_btn.drawButton(true);
textInput.remove(textInput.length() - 1, 1);
tft.setTextColor(CYAN);
inputValue(textInput);
}
CLEAR_btn.press(down && CLEAR_btn.contains(pixel_x, pixel_y));
if (CLEAR_btn.justReleased()) CLEAR_btn.drawButton();
if (CLEAR_btn.justPressed()) {
CLEAR_btn.drawButton(true);
textInput = "";
inputValue(textInput);
}
ENTER_btn.press(down && ENTER_btn.contains(pixel_x, pixel_y));
if (ENTER_btn.justReleased()) ENTER_btn.drawButton();
if (ENTER_btn.justPressed()) {
ENTER_btn.drawButton(true);
person.Name = textInput;
textInput = "";
hasName = true;
}
}
void inputAge(void) {
bool down = Touch_getXY();
for (int i = 0; i < 10; i++) {
numButtons[i]->press(down && numButtons[i]->contains(pixel_x, pixel_y));
if (numButtons[i]->justReleased()) numButtons[i]->drawButton();
if (numButtons[i]->justPressed()) {
numButtons[i]->drawButton(true);
ageInput += ageLine[i];
inputValue(ageInput);
}
}
DEL_btn.press(down && DEL_btn.contains(pixel_x, pixel_y));
if (DEL_btn.justReleased()) DEL_btn.drawButton();
if (DEL_btn.justPressed()) {
DEL_btn.drawButton(true);
ageInput.remove(ageInput.length() - 1, 1);
tft.setTextColor(CYAN);
inputValue(ageInput);
}
ENTER_btn.press(down && ENTER_btn.contains(pixel_x, pixel_y));
if (ENTER_btn.justReleased()) ENTER_btn.drawButton();
if (ENTER_btn.justPressed()) {
ENTER_btn.drawButton(true);
person.Age = ageInput;
ageInput = "";
hasAge = true;
}
}
void choosing(String firstChoice, String secondChoice) {
bool down = Touch_getXY();
one_btn.press(down && one_btn.contains(pixel_x, pixel_y));
if (one_btn.justReleased()) one_btn.drawButton();
if (one_btn.justPressed()) {
one_btn.drawButton(true);
if (patientStatus == "getGender")
{
person.Gender = firstChoice;
hasGender = true;
}
if (patientStatus == "getPrint")
{
person.Print = firstChoice;
hasPrint = true;
}
}
zero_btn.press(down && zero_btn.contains(pixel_x, pixel_y));
if (zero_btn.justReleased()) zero_btn.drawButton();
if (zero_btn.justPressed()) {
zero_btn.drawButton(true);
if (patientStatus == "getGender")
{
person.Gender = secondChoice;
hasGender = true;
}
if (patientStatus == "getPrint")
{
person.Print = secondChoice;
hasPrint = true;
}
}
}
void resetting() {
bool down = Touch_getXY();
reset_btn.press(down && reset_btn.contains(pixel_x, pixel_y));
if (reset_btn.justReleased()) reset_btn.drawButton();
if (reset_btn.justPressed()) {
reset_btn.drawButton(true);
resetFunc(); //call reset
}
}
String getSplitValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = {0, -1};
int maxIndex = data.length() - 1;
for (int i = 0; i <= maxIndex && found <= index; i++) {
if (data.charAt(i) == separator || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i + 1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}
void pulseRateOxygen()
{
long irValue = particleSensor.getIR();
if (checkForBeat(irValue) == true)
{
//We sensed a beat!
Serial.println("Sensed a beat!");
long delta = millis() - lastBeat;
lastBeat = millis();
beatsPerMinute = 60 / (delta / 1000.0);
if (beatsPerMinute < 255 && beatsPerMinute > 20)
{
rates[rateSpot++] = (byte)beatsPerMinute; //Store this reading in the array
rateSpot %= RATE_SIZE; //Wrap variable
//Take average of readings
beatAvg = 0;
for (byte x = 0 ; x < RATE_SIZE ; x++)
beatAvg += rates[x];
beatAvg /= RATE_SIZE;
}
delay(50);
}
Serial.print("IR=");
Serial.print(irValue);
Serial.print(", BPM=");
Serial.print(beatsPerMinute);
Serial.print(", Avg BPM=");
Serial.print(beatAvg);
if (irValue < 50000) {
Serial.print(" No finger?");
beatsPerMinute = 0;
beatAvg = 0;
}
if (beatAvg > 39) {
Serial.println("yords!! STOP!");
person.PulseRateAVG = beatAvg;
person.PulseRateBPM = beatsPerMinute;
hasPulseRate = true;
}
Serial.println();
}
void loopOL() {
bufferLength = 20; //buffer length of 100 stores 4 seconds of samples running at 25sps
//read the first 100 samples, and determine the signal range
for (byte i = 0 ; i < bufferLength ; i++)
{
while (particleSensor.available() == false) //do we have new data?
particleSensor.check(); //Check the sensor for new data
redBuffer[i] = particleSensor.getRed();
irBuffer[i] = particleSensor.getIR();
particleSensor.nextSample(); //We're finished with this sample so move to next sample
Serial.print(F("red="));
Serial.print(redBuffer[i], DEC);
Serial.print(F(", SPO2="));
Serial.print(spo2, DEC);
Serial.print(F(", ir="));
Serial.println(irBuffer[i], DEC);
if (spo2 > 0 ) {
Serial.print(spo2);
Serial.println("\t -- oyoyo");
person.OxygenLevel = spo2;
hasOxygenLevel = true;
}
}
//calculate heart rate and SpO2 after first 100 samples (first 4 seconds of samples)
maxim_heart_rate_and_oxygen_saturation(irBuffer, bufferLength, redBuffer, &spo2, &validSPO2, &heartRate, &validHeartRate);
}
char BPmygetchar(void)
{ //receive serial character from sensor (blocking while nothing received)
while (!Serial1.available());
return Serial1.read();
}
void loopBP() {
if (mySerial.available()) {
String rec = mySerial.readString();
const String data1 = getSplitValue(rec, ' ', 0);
const String data2 = getSplitValue(rec, ' ', 1);
const String data3 = getSplitValue(rec, ' ', 2);
person.BP = data1 + "/" + data2;
person.PulseRate = data3 + " / min";
Serial.println(rec);
Serial.println(data1);
Serial.println(data2);
Serial.println(data3);
hasBP = true;
}
}
float loopWeight() {
// Weight Start
static boolean newDataReady = 0;
const int serialPrintInterval = 0; //increase value to slow down serial print activity
// check for new data/start next conversion:
if (LoadCell.update()) newDataReady = true;
// get smoothed value from the dataset:
if (newDataReady) {
if (millis() > Masst + serialPrintInterval) {
float i = LoadCell.getData();
Serial.print("Load_cell output val: ");
Serial.println(i);
newDataReady = 0;
Masst = millis();
return i;
// person.Weight = i;
// hasWeight = true;
}
}
// receive command from serial terminal, send 't' to initiate tare operation:
if (Serial.available() > 0) {
char inByte = Serial.read();
if (inByte == 't') LoadCell.tareNoDelay();
}
// check if last tare operation is complete:
if (LoadCell.getTareStatus() == true) {
Serial.println("Tare complete");
} // Weight END
}
void loopHeight() {
// Height Start
// Clears the trigPin condition
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
// Displays the distance on the Serial Monitor
if (distance > 2 && distance < 400) {
double inchesTotal = (213.36 - distance) / 2.54;
int ft = inchesTotal / 12;
double inches = inchesTotal - (12 * ft);
String measuredHeight = "";
measuredHeight.concat(ft);
measuredHeight.concat("ft & ");
measuredHeight.concat(inches);
measuredHeight.concat("in");
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
Serial.print("Measured Height: ");
Serial.println(measuredHeight);
person.Height = measuredHeight;
if (person.Height.length() > 0) {
person.metricHeight = (213.36 - distance) * 0.01;
hasHeight = true;
}
}
// Height END
}
void loopTemp() {
Serial.print("Ambient = "); Serial.print(mlx.readAmbientTempC()); Serial.print("\xC2\xB0");
Serial.print("C\tObject = "); Serial.print(mlx.readObjectTempC()); Serial.print("\xC2\xB0");
Serial.println("C");
Serial.print("Ambient = "); Serial.print(mlx.readAmbientTempF()); Serial.print("c");
Serial.print("F\tObject = "); Serial.print(mlx.readObjectTempF()); Serial.print("\xC2\xB0");
Serial.println("F");
Serial.println();
delay(1000);
person.BodyTemperature = mlx.readObjectTempC();
if (person.BodyTemperature.length() > 1) {
hasTemp = true;
}
}
//#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment