My LCD display is showing the name of my device (MACHA), and the diagnosis next and the BPM is supposed to be shown last.
But the BPM is varying and changing really fast, it's showing 6 numbers and it's supposed to be less than 3 numbers.
Please advice
#define PEAK_THRESHOLD 0.3
#define ROLLING_SIZE 3
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16,2);
int value = 0;
float voltage;
bool peaked = false;
int bpm;
unsigned long beat_times[ROLLING_SIZE] = {0, 0, 0};
unsigned long current_time;
bool contains_zero() {
for (int i = 0; i < ROLLING_SIZE; i++) {
if (beat_times[i] == 0) {
return true;
}
}
return false;
}
void shift_times() {
for (int i = 1; i < ROLLING_SIZE; i++) {
beat_times[i - 1] = beat_times[i];
}
}
void setup() {
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("MACHA TES");
Serial.begin(9600);
delay(500);
}
void loop() {
value = analogRead(A0);
voltage = (value * 5.0/1023);
current_time = millis();
if (voltage > PEAK_THRESHOLD && !peaked) {
peaked = true;
shift_times();
beat_times[ROLLING_SIZE - 1] = current_time;
} else if (voltage <= PEAK_THRESHOLD && peaked) {
peaked = false;
}
if (contains_zero()) {
return;
}
bpm = (ROLLING_SIZE - 1) * 60000 / ( beat_times[ROLLING_SIZE - 1] - beat_times[0]);
if (bpm >= 100){
lcd.setCursor(0,1);
lcd.print("Sinus Tachycardia");
}
else if (bpm <= 60){
lcd.setCursor(0,1);
lcd.print("Sinus Bradycardia");
}
else {
lcd.setCursor(0,1);
lcd.print("Normal Sinus");
}
lcd.setCursor(0,2);
lcd.print(bpm);
}