Pulse Rate Monitoring

Hi :slight_smile: I saw this codes somewhere here in the forum… (from UKHeliBob)
Can someone tell me what is the meaning of 156 and 146 there? (currentSensorValue > 156 and currentSensorValue < 146)
what is the use of that numbers??

Thank you in advance :slight_smile:

long measurementStartTime = 0;
int beats = 0;
byte sensorPin = A0;
int currentSensorValue;
boolean counted = false;

void setup() 
{
  Serial.begin(9600);
  pinMode(sensorPin, INPUT);
}

void loop() 
{
  if ((millis() - measurementStartTime > 10000) && (beats > 0))  //time is up
  {
    Serial.print("Beats read in 6 seconds : ");
    Serial.println(beats);
    measurementStartTime = millis();
    beats = 0;
  }
  currentSensorValue = analogRead(sensorPin);
  if (currentSensorValue > 156 && counted == false)
  {
    beats++;
    counted = true;  
  }
  else if (currentSensorValue < 146)
  {
    counted = false; 
  }
}

what is the use of that numbers??

Can’t you see that they are used as thresholds? If the value read from the pins is above one value, do something. If it is below the other value, do something else. If it is between the magic numbers, do nothing.

Now, I suppose your question is really what those magic numbers mean. And, now you know why magic numbers are discouraged. You’ll have to ask the magician that used them, and, of course, magicians never explain their tricks.

PaulS:
Can't you see that they are used as thresholds? If the value read from the pins is above one value, do something. If it is below the other value, do something else. If it is between the magic numbers, do nothing.

Now, I suppose your question is really what those magic numbers mean. And, now you know why magic numbers are discouraged. You'll have to ask the magician that used them, and, of course, magicians never explain their tricks.

Sorry sir, i asked because i am just new in arduino coding so i am not sure with my understanding .. just to be clear it for me, i joined forums :slight_smile:
but anyway,. thank you!

magicians never explain their tricks.

I don't even remember performing it let alone explain it.

What I should have done, as Paul has hinted, is to make the 2 numbers into constants with meaningful names so that they lost their magic qualities. As it is all I can say is that they were the upper and lower limits that I was looking for at the time.

EDIT: No wonder I don't remember what the numbers are for because I did not write the code that they came from, I merely modified someone elses.

UKHeliBob:
I don't even remember performing it let alone explain it.

What I should have done, as Paul has hinted, is to make the 2 numbers into constants with meaningful names so that they lost their magic qualities. As it is all I can say is that they were the upper and lower limits that I was looking for at the time.

you mean sir, those two numbers can be change in any number i want?

you mean sir, those two numbers can be change in any number i want?

Yes, of course.

haha.

So to answer the question posted here then.

UKHeliBob:
How did you arrive at the value of 156 used in your original code ?
How is the sensor wired ?
How are the heartbeats turned into a value that the sensor can read ?

febihailyharshi:
we used a piezoelectric accelerometer to sense the signal from wrist.it was then amplified and fed to a filter to filter noise, and it was given to a comparator which converted the peak pulse corresponding to beats into square pulses of frequency 1.2 to 1.7 hz.. this slight variation is because of noise in between i suppose. we get a peak amplitude of nearly 5V. our purpose is to use arduino as a counter and display the number of pulses arriving in 60 secs.

UKHeliBob:
Yes, of course.

okay sir. thanks :slight_smile: