Displaying timestamp on serial monitor [biomedical engg.]

I wanna display on the serial monitor the peak amplitude of a periodic signal and the time interval between two consecutive peaks. The signal is read from the sensor.

Signal frequency = 1Hz - 35Hz
Baud selected = 115200

I used the following code with shows the peak values correctly but not the time instants. Sometimes it shows 0 for different peaks [monitor screenshot attached ]. Ideally peaks occur in >600 ms in real time.

/* -------my Sketch -----*/

const int ledPin = 13;
const int signalPin = A0;

const int thresh = 620;
unsigned long startTime =0;

void setup()
{
  pinMode(signalPin, INPUT);
  pinMode(ledPin, OUTPUT);

  Serial.begin(115200);
}  

void loop()
{
    int val = analogRead(signalPin);
   
    if(val > thresh)
   {
     digitalWrite(ledPin, HIGH);
     Serial.println(val);

     Serial.println((unsigned long)millis()-startTime);
     startTime = millis();

     Serial.println();
   } 
  else
    digitalWrite(ledPin, LOW);    
}

/*-----------END----------*/

I hate anonymous printing. I won't even bother trying to decipher code that does so, especially without seeing the actual output.

Define "peak".

1) Please use code tags when posting code to this forum.

2) Do you really want to be using an Arduino as a heart rate monitor?

odometer: 2) Do you really want to be using an Arduino as a heart rate monitor?

With a frequency up to

Signal frequency = 1Hz - 35Hz 35 Hz that would be 35*60= 2100 beats per minute

That would be a higher frequency than the heartbeat of a colibri (hummingbird) in full flight mode. So I don't think it is for a heartrate monitor, except perhaps a heartrate monitor for a colibri.

But it would be a good thing if he'd take a look on working code for Arduino heartrate monitoring as the problem (but not the frequency range) is the same as what he wants to do.

It might be a great help if he at least would try to catch peaks and not create a whole lot of mess taking every signal above threshold value as a peak (which is complete nonsense).

The heart rate is calculated from R-R interval [+/- 750ms], R being the peak of the ECG signal ( normal cases). The maximum frequency is not used to calculate heart rate. [1-35 hz is only the range, including diseases]

From the features of ecg signal, we have seen apart from R peak, all other signal amplitudes lie in very low range. Thus the threshold, above which only R peak can rise (exception- critical heart disease)

I am only trying to see the time instant of the peaks occurring, which is further needed for the calculation of the heart rate, based on the popular R-peak detection algorithm.

I have seen the regular codes available for heart rate monitoring. They are not based on any approved algorithm, hence are not being helpful.

Bdw, thank you all for replying.

If I can even get an idea on how to display the time instants of all the positive peak amplitudes occurring in a sinusoidal wave, it too would be helpful.

Another important point to be noted that the sensor data I am going to work on, would probably be read for 2 mins and the final program won't at all display time instants or bulk data alike. It is just for debugging purpose.

If I can even get an idea on how to display the time instants of all the positive peak amplitudes occurring in a sinusoidal wave, it too would be helpful.

You are NOT detecting a peak. You are simply printing the actual reading and the time since the last reading whenever the reading is above some threshold.

You REALLY do need to look at some of the example that show how to detect a peak (this reading is less than that reading, so that reading was probably the peak, so its time matters).

They are not based on any approved algorithm, hence are not being helpful.

Bullshit. If you want to use the techniques illustrated based on “an approved algorithm”, you are free to do so. Whatever the hell “an approved algorithm” means. A peak of a sine wave is a peak.

I do not understand what you mean by

DishaCU: I have seen the regular codes available for heart rate monitoring. They are not based on any approved algorithm, hence are not being helpful.

and:

DishaCU: If I can even get an idea on how to display the time instants of all the positive peak amplitudes occurring in a sinusoidal wave, it too would be helpful.

For example, if a diagram of pressure values over time would look like that:

What do you want to get as a result from your data values: - heart rate in beats per minute - time stamps of the top peaks in milliseconds since start of test - and what else?

jurs: For example, if a diagram of pressure values over time would look like that:

What do you want to get as a result from your data values: - heart rate in beats per minute - time stamps of the top peaks in milliseconds since start of test - and what else?

The aim is to see the heart rate in beats per minute

But initially I want to see time stamps of the top peaks in milliseconds since start of test

DishaCU: The aim is to see the heart rate in beats per minute

But initially I want to see time stamps of the top peaks in milliseconds since start of test

This is how to program a "beat detection": - create a "long time average" value of your analog signal by lowpass filtering - compare "current value" against "average value" - if current value has been below average and then rises above "average+delta" ==> beat detected

The "delta" has to be chosen carefully, so that only the highest peak in a period is detected, in this picture the peak at "R", but not the peaks at "P" or "T":

I think a while ago I posted some code for BPM calculation in another thread. Do You need some example code for "beat detection" and "BPM calculation" for a simple heartrate monitor? In that case I could possibly look through my old codes and then post some example again.

@ jurs

Thank you very much for your hints. I would definitely look at your example codes.

I would like you to know that the beat detection algorithm I have to use in my project strictly requires R to be detected by maximum slope, not as the highest peak. This is due to the fact that, some of the subjects are having T waves almost as high as R, thereby indicating problems like false peak detection.

I would appreciate your suggestion regarding this.

As far as time-stamp display is concerned, I modified my previous code and getting some viable result. Posting it for your perusal.

const int ledPin = 13;
const int ecgPin = A0;

const int thresh = 620;
unsigned int startTime =0;
int next=0;


void setup()
{
  pinMode(ecgPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(115200);
}  

void loop()
{
    int val = analogRead(ecgPin);
   
    if(val > thresh)
   {
     int amp = maxVal(val);
     digitalWrite(ledPin, HIGH);
     Serial.println(amp);
     Serial.println((unsigned long)millis()-startTime);
     startTime = millis();
     delay(600);
     Serial.println();
   } 
  else
    digitalWrite(ledPin, LOW);    
}

int maxVal(int val)
{
  if((analogRead(ecgPin)-val) >0)
  {
    next = analogRead(ecgPin);
    maxVal(next);
  }
  else  
     return(next);     
}

I am also doing a project similar to this.I want to detect the peaks and hence count the peaks for particular time.Also I want to display the counted number in an LCD display.

@Jurs can you please post the link for the code used for detecting the peaks.