Hi All,
I was hoping that someone could help me out with a problem I'm having with attachInterrupt and trying to smooth values that it is creating. This code is for a heart rate monitor that works through an IR LED sensor and phototransistor. When I run it, the sensor reads the time between pulses well using attachInterrupt and millis().
The problem is that I want to smooth the values, as they are pretty aperiodic. If it could smooth somewhere between 5 and 10 values, that would be great. The thing is that since the function that is called by the interrupt has to be void, I can't return a value (BPM in this case). Can someone help me out?
int heartBeatIn = 2; // Digital pin 2 selected for input
byte HRbpm = 0; // Declare this as a byte if the count is always less than 255
// so you don't have to disable interrupts when using in loop
long lastFire = 0;
long currentTime = 0;
int BPM;
void setup()
{
Serial.begin(9600); //Configure serial output
pinMode(heartBeatIn, INPUT); //Pin 2 declared as an input
digitalWrite(heartBeatIn, HIGH); // Turn on pullup resistor
attachInterrupt(0, count, RISING); // Call function "count" when pin 2 goes high
lastFire = millis(); // lastFire = the time (in ms) since the program started running
currentTime = millis(); // currentTime = the time (in ms) since the program started running
}
void loop()
{
HRbpm= 0;
byte HRcount = HRbpm; // Get a snapshot of the count
}
void count()
{
HRbpm++;
currentTime = millis(); // Value of currentTime becomes whatever the
// the time is since the program started running
float temp = (60.0/((currentTime-lastFire)/1000.0)); // (60 seconds / frequency)= BPM
if (temp < 220 && temp > 40)// Default outlying values to previous BPM
BPM = temp;
Serial.println(BPM); // Print the BPM
lastFire=millis(); // Value of lastFire becomes time program has been running
}