object is to make a program which detect heart beat through ecg and count the pulse for 1 min ...and show no of pulse on serial monitor after 1 min... but i'm having a problem right now ..this program is working perfectly ... i used potentiometer as ecg input and by comparing it by fix threshold m making pulse and counting it and showing it on S.monitor ... but the problem is ... i dont know ... how i can make this program to run only for 1 min and show no of pulse on S.monitor after 1 min ..and reset all its value.. n run the program from beginning.
const int analogPin = A0;
const int ledPin = 2;
const int threshold = 3;
int cs=0;
int ps=0;
int counter=0;
Do not use delay(...). It wastes the considerable power of your Arduino by keeping you from doing anything useful. (There are exceptions but you probably want to avoid them until you get more experience.)
millis() will give you the time in milliseconds since the Arduino was started. So...
const unsigned long measurementInterval = 60UL * 1000 ; // Define this globally, ahead of setup()
const unsigned long startTime ; // Define this globally, ahead of setup()
startTime = millis() ; // Execute this when/where you take your first measurement;
// This could be inside setup(), inside loop(), or inside some other function.
// Inside of loop(), do this...
//
if (millis()-startTime>=measurementInterval)
{
// Display the number of beats.
while(1) ; // Arduino will be "stopped" (actually, stuck) here until reset.
}
I would like to help more, but I have no idea what is going on with cs and ps. If cs != ps, of course cs is 1.
So at the end of the if (cs != ps) do a ps = cs; and Get rid of the delay(100) it's not needed if you maintain ps
To run this for 1 minute just put code into a while loop
unsigned long lastTime;
lastTime = millis();
while (millis()-lastTime <= 60000ul) {// 1 minute = 60 seconds = 60 000 ms. This needs to be an unsigned long so use ul to tell the compiler to do so as this does not fit in a signed int
// do the measure
}
// here we have measured for 1 minute
// counter has the number of bpm to display
// then reset counter,cs and ps to 0 and you are good for a new 1 minute measurement as the loop loops