using millis() function to read input for a given interval of time

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);
  
}

maddiedev:
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.

Either that or your code reads the sensor precisely once at the top of loop.

With millis() you need to set variables and constants. You need one to be set to 5000, and another equal to millis() and another that is either 0 or 1 (on or off, effectively). let's call c the constant 5000, t time in milliseconds, and x as the on/off variable.

//unless x is on, millis() won't start
if (x == 0)
{millis()=0}
// button is pushed, x turns on, and it starts counting.
if (button is pushed)
{x == 1}
//stop counting after 5 seconds
if (t >= c)
{x = 0}

that is the kind of idea I would start it off with. Another thing to consider is that if you take the number of beats and multiply by 12, it will be less accurate and only able to read in 12's. What I would do is have it save the count when it reads a heart beat. So if it read 5 beats, but the fifth beat was after 4.732 seconds, it would remember that number. I'm going to call this variable y

//when it registers a heart beat, save y  as millis()
if (heart beats)
{y = millis}

after the 5 seconds is done, then take 60 times the number of heart beats and divide it by the value of the variable y to get a more accurate number.

Serial.println((60*count)/y)

This way, in the example I made earlier, it would give you 63.4 BPM whereas multiplying the number of beats by 12 would give you 60 BPM.

Remember, nothing happens while you have delay(1000) going. So for one second you count no beats.