#include<LiquidCrystal_I2C.h>
const int buttonPin = 2;
int buttonState = 0;
int previousButtonState = LOW;
unsigned long startTime;
int pulseCount = 0;
LiquidCrystal_I2C lcd(0x27,16,2);
void setup() {
lcd.init();
lcd.backlight();
pinMode(buttonPin, INPUT);
Serial.begin(9600);
startTime = millis();
}
void loop() {
buttonState = digitalRead(buttonPin);
// Check if button is pressed
if (buttonState == HIGH && previousButtonState == LOW) {
pulseCount++;
delay(50);
}
previousButtonState = buttonState;
// Check if 15 seconds have passed
if (millis() - startTime >= 60000) {
// Calculate BPM: multiply count by 4 to estimate BPM for 15s interval
double bpm = (double)pulseCount/120;
// Output BPM to Serial Monitor
Serial.print("BPM: ");
lcd.print("BPM: ");
Serial.println(bpm);
lcd.print(bpm);
delay(2000);
lcd.clear();
// Reset variables for next measurement
pulseCount = 0;
startTime = millis();
}
}