Buen día, estoy empezando a programar en arduino y estoy desarrollando un proyecto con un sensor de pulso cardíaco. Mi idea es que el arduino consiga el bpm tomado del sensor por determinado tiempo, para poder realizar un promedio de los beats y después muestre ese promedio en una pantalla LCD 16x2, espere por 5 segundos con la lectura de los beats y vuelva a tomar los datos. Ademas con un botón quiero apagar/encender el sensor y la pantalla LCD. Me ayudarían mucho si me especificaran como funciona el ciclo while pues creo que ahí esta mi problema.
Adjunto mi código. Para utilizar el sensor estoy usando la libreria de PulseSensorPlayground
#include <LiquidCrystal_I2C.h>
#define USE_ARDUINO_INTERRUPTS true // Set-up low-level interrupts for most acurate BPM math.
#include <PulseSensorPlayground.h> // Includes the PulseSensorPlayground Library.
// Variables
const int PulseWire = 0; // PulseSensor PURPLE WIRE connected to ANALOG PIN 0
const int LED13 = 13; // The on-board Arduino LED, close to PIN 13.
int Threshold = 525; // Determine which Signal to "count as a beat" and which to ignore.
// Use the "Gettting Started Project" to fine-tune Threshold Value beyond default setting.
// Otherwise leave the default "550" value.
int estado = 0;
int estadoAnterior = 0;
int salida = 0;
unsigned long time;
long contador = 0;
unsigned long suma = 0;
float promedio =0;
PulseSensorPlayground pulseSensor; // Creates an instance of the PulseSensorPlayground object called "pulseSensor"
LiquidCrystal_I2C lcd(0x27,16,2);
void setup() {
pinMode(8, INPUT);
lcd.init(); // Se inicializa el LCD 16x2
lcd.backlight();
lcd.clear();
Serial.begin(9600); // For Serial Monitor
time = millis();
// Configure the PulseSensor object, by assigning our variables to it.
pulseSensor.analogInput(PulseWire);
pulseSensor.blinkOnPulse(LED13); //auto-magically blink Arduino's LED with heartbeat.
pulseSensor.setThreshold(Threshold);
// Double-check the "pulseSensor" object was created and "began" seeing a signal.
if (pulseSensor.begin()) {
}
}
void loop() {
estado = digitalRead(8);
if((estado == HIGH) && (estadoAnterior == LOW)){
salida = 1 - salida;
delay(20);
}
estadoAnterior = estado;
if (salida == 1){
lcd.backlight();
lcd.setCursor(1,0);
pulseSensor.resume();
while (time < 15000UL){
int myBPM = pulseSensor.getBeatsPerMinute();
suma += myBPM;
contador ++;
time = millis();
}
promedio = (float) suma/contador;
Serial.print("BPM: ");
Serial.print((int)promedio);
Serial.println(" ");
}
else{
digitalWrite(LED13,LOW);
pulseSensor.pause();
lcd.clear();
lcd.noBacklight();
}
delay(20); // considered best practice in a simple sketch.
}
También adjunto captura de la salida en el monitor serie
De antemano gracias