Working on a project that counts voltage spikes from a heart monitor sensor that will represent pulse. This is the first time coding with Arduino, and I only want to count pulse for five seconds and then multiply that count by 12 to display beats per minute on an LCD screen.
When I tried to manipulate the millis() function to create a time interval condition for a while loop, the program basically skips over that entire portion and moves on to the next bit of code.
I have background in C++, but every coding tutorial that I've seen that works with timing as a loop condition hasn't worked in this code. I'm not sure if a while loop is the way to go.
Here's the code as it is right now:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <time.h>
#include <elapsedMillis.h>
LiquidCrystal_I2C lcd(0x27,16,2);
int count = 0;
byte heart[8] = {
B00000,
B01010,
B10101,
B10001,
B01010,
B00100,
B00000,
};
unsigned long interval = 5000;
unsigned long previousMillis = 0;
void setup()
{
lcd.init();
Serial.begin(9600);
lcd.backlight();
lcd.createChar(0,heart);
lcd.begin(16,2);
lcd.setCursor(0,0);
lcd.print("Gathering heart");
lcd.setCursor(0,1);
lcd.print("rate...");
}
void loop()
{
int sensorValue = analogRead(A0);
unsigned long currentMillis = millis();
while ((currentMillis - previousMillis) <= interval)
{
if(sensorValue > 10 && sensorValue < 1000)
{
count++;
}
else
{
}
delay(1000);
previousMillis = millis();
}
lcd.clear();
lcd.write(byte(0));
lcd.print(" Heart Rate: ");
lcd.setCursor(0,1);
lcd.print(count*12);
delay(5000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Press reset to");
lcd.setCursor(0,1);
lcd.print("measure again.");
exit(0);
}