#include <Keypad.h>
#include <Wire.h> //I2C
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
Servo myservo1; // Create a servo object
// Heart read
#include "MAX30105.h"
#include "heartRate.h"
MAX30105 particleSensor;
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;
//add
double avgavg=0; long long avidx=0; bool fail=0;
//add
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C axress 0x27, 16 column and 2 rows
const byte ROWS = 4; //number of rows and columns in hexaKeys of keypad
const byte COLS = 4; // byte like int but range 0 : 255
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {10, 8, 7, A0}; //pins of arduino =>ROWS
byte colPins[COLS] = {A1, 4, 3, 2}; //pins of arduino =>columns
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
///////
void heart()
{
Serial.println("Initializing...");
// Initialize sensor
if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) {
Serial.println("MAX30102 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
//add
while(true)
{
avgavg=0; avidx=0; fail=0;
lcd.print("put your finger");
long irValue = particleSensor.getIR();
if (checkForBeat(irValue) == true) {lcd.clear(); break;}
}
//add
unsigned long long tim = millis(),tim2 = millis();
while(tim + 15000 > millis())
{
long irValue = particleSensor.getIR();
if (checkForBeat(irValue) == true) {
//We 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;
}
}
Serial.print("IR=");
Serial.print(irValue);
Serial.print(", BPM=");
Serial.print(beatsPerMinute);
Serial.print(", Avg BPM=");
Serial.print(beatAvg);
//add
avgavg+=beatAvg;
avidx++;
//add
if (irValue < 50000)
{
Serial.print(" No finger?");
//add
lcd.setCursor(0,0);
lcd.print("can't find");
lcd.setCursor(0,1);
lcd.print("finger");
delay(1000);
lcd.clear();
lcd.print("try again");
delay(1000);
lcd.clear();
fail=1;
break;
//add
}
Serial.println();
if (tim2 + 1000 > millis())
{
//add
lcd.setCursor(0, 0);
heartform();
//add
lcd.setCursor(2, 0);
lcd.print("bpm");
lcd.setCursor(6, 0);
lcd.print(beatsPerMinute);
lcd.setCursor(2, 1);
lcd.print("avg");
lcd.setCursor(6, 1);
lcd.print(beatAvg);
tim2 += 1000;
}
}
//add
if(fail==0)
{
avgavg=avgavg/avidx;
lcd.setCursor(0, 0);
lcd.print(avgavg);
delay(1000);
lcd.clear();
/*
if()
{
}
else if
{
}
else
{
}
*/
}
//add
}
void setup() {
Serial.begin(115200);
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
myservo1.attach(5);
myservo1.write(100);
}
void loop() {
if(customKeypad.getKey() == 'B')
{
myservo1.write(100);
heart();
myservo1.write(50);
}
}
//add
void heartform()
{
byte customChar1[] = {
B00000,
B11011,
B11111,
B11111,
B11111,
B01110,
B00100,
B00000
};
byte customChar2[] = {
B00000,
B00000,
B11011,
B11111,
B01110,
B00100,
B00000,
B00000
};
lcd.createChar(0, customChar1);
lcd.createChar(1, customChar2);
lcd.setCursor(0, 0);
lcd.write(0);
delay(500);
lcd.setCursor(0, 0);
lcd.write(1);
delay(500);
}
//add