Go Down

Topic: Calculating heartbeat frequency using PPG method - Help needed! (Read 2 times) previous topic - next topic

wildbill

I should have been more clear: don't bring back all the prevcheck stuff you had. Just put this as the last line in loop:
Code: [Select]

check = prevcheck;

willing2learn

Thanks Wildbill, getting closer!

The LEDs flicker now and the printed lines are like:

Code: [Select]
5965
2852
3460
1240
5687
4362
3984
7007
6946
6566
6641
2015


The lines are printed about every 6 seconds, so that works.

I'm guessing the processor is running through the loop several times faster than i want it - is this anything to do with the 9600 baud rate?

Thanks

wildbill


willing2learn

Now the LED flashes correctly - with the heartbeat, but there is nothing printed. I've also tried going back, but still nothing is printed. Here's the code now:
Code: [Select]

int LED = 13; //LED to blink when pulsing
int din = 2; // digital pin 2 to read 0V or 5V
int check; // checks current status of digital pin 2
int prevcheck;// previous check status
int pulsecount = 0; //number of pulses (highs) seen on the digital pin
int BPM = 0; //beats perminute = counter value * 10
unsigned long firstpulse =0; //time of first pulse
unsigned long timecount = 0;//cumulative time of total pulses
unsigned long latestpulse=0;
unsigned long interval = 6000; //setting 6 seconds as max read time

void setup()

{
  pinMode(LED, OUTPUT);
  pinMode(din,INPUT);
  Serial.begin(9600);
}

void loop()

{
  check = digitalRead(din);
 
  //if (check != prevcheck ) // comparing current state to previous state
  {
    if (check == LOW)
    {
      digitalWrite(LED, LOW);
      //prevcheck == LOW;
    }
   
    if (check == HIGH)
    {
      digitalWrite(LED, HIGH);
      //prevcheck == HIGH; //storing current state of input pin
      check == LOW; //reset the check

      if (pulsecount == 0 ) // if this is the first count
     
      {
        firstpulse = millis(); // recording time of first pulse
        pulsecount = pulsecount +1; //increment the counter
      }
     
      if (pulsecount >=1 )
      {
        pulsecount = pulsecount + 1; // increment pulse counter when detected
        //timecount = millis() + firstpulse + timecount; //sumation of all pulse periods
        latestpulse = millis(); // recording latest pulse time
      }
     

    }
  }
       
        if (latestpulse - firstpulse > interval) //checking if 6 seconds has elapsed
      {
        BPM = pulsecount;
        Serial.println(BPM); //display the number of counts over 6 seconds
        pulsecount = 0; // counter restarted
        firstpulse = 0; // restart for time of new first-pulse read
        latestpulse = 0; // restart for new time readings
        //delay(100);
      }
      prevcheck = check;
    }

 
  Thanks

 
 



wildbill

ok. try this:
Code: [Select]

void loop()
{
check = digitalRead(din);
if(check != prevcheck)
  {
  if (check == LOW)
    {
    digitalWrite(LED, LOW);
    }

  if (check == HIGH)
    {
    digitalWrite(LED, HIGH);

    if (pulsecount == 0 ) // if this is the first count
      {
      firstpulse = millis(); // recording time of first pulse
      pulsecount = pulsecount +1; //increment the counter
      }

    if (pulsecount >=1 )
      {
      pulsecount = pulsecount + 1; // increment pulse counter when detected
      latestpulse = millis(); // recording latest pulse time
      }
    }
  }

if (latestpulse - firstpulse > interval) //checking if 6 seconds has elapsed
  {
  BPM = pulsecount;
  Serial.println(BPM); //display the number of counts over 6 seconds
  pulsecount = 0; // counter restarted
  firstpulse = 0; // restart for time of new first-pulse read
  latestpulse = 0; // restart for new time readings
  }
prevcheck = check;
}   

Go Up