Programming breathing frequency counter

Hi guys,

I have finally finished my breathing sensor- Ive done it with DS18B20 put right under the nostrils and whenever person breaths out it increaser the temperature on the sensor- that means one breathing cycle is done.
Im having a little trouble putting that into the progamming code...Ive found some codes for DS18B20 that show the temperatures, but I need to find the way to convert that into respiration rate. I counted that average time between 2 exhales is 3.75s. Im not sure how to program that I take the period of lets say 20 seconds and count how many exhales were in that period (the exhale part should be easy with IF loop, something like if(temp*>temp[i-1])...) and multiply that by 3 to get average exhales per minute...*
Does anyone have any idea on how the code should look like?

or detemine max to max peak time
or determine min to min peak time
or determine min to max peak time
or ...
or could work with the first derivative

several ways,

what is the delta in temperature ?
max temp, min temp?
what resolution do you use? 9 bit? 10 bit?

what code do you have so far?

So far I have done the temperature reading code from DS18B20. Its 9bit resolution (0.5C). The temperature varies from the temperature of the enviroment, but the exhaled air gives (once when its already heated) 0.5C-1C temperature rise. I dont think I could use min to max because it varies on the surrindings...Maybe max to max peak time should do, if it is counted in seconds then i just devide 60 with that number and get respiratory cycles per min? I was wondering if there is some way to limit the program that counts number of exhales to 20secs, and then when it reaches 20secs it restarts again from 0-20sec and then I just multiply the number of exhales I have got in that 20 secs by 3 to get number of respiratory cycles per minute. I have looked for timer and interupts and only idea I could get is to perhaps write the code in normal program, program the timer with interupt after 20 secs and then in that interupt put the code to restart the counter so it could start again when it goes back to program??? Is something like that possible? But your idea seems easier to program with max to max peak time...

if you have only one max to max period, let say t = 3142 milliseconds then the

respirations = 60000UL / t = 60000UL / 3142 = 19.1 per minute.

you can update this number every time you find a new max

robtillaart:
respirations = 60000UL / t = 60000UL / 3142 = 19.1 per minute.

no, you have 19 per minute

:wink:

BulldogLowell:
no, you have 19 per minute

:wink:

you are absolutely right

Ok thanx...btw when You mentioned max to max peak period i did some search on it and all I could find was how to determine the value of the peak...is there any special function that determines max to max peak time or?

P.S.
Here is what I tried to do so far but I get nothing on the output in Arduino serial monitor.
The idea was to save 5 values of temperatures being measured every 1.2 secs (because unless you are running You cant breath out faster than that, and I need this to work for lie detector). Than I compare 2 values that are right next to each other and if the i+1 is higher than i that means 1 exhale and the first counter starts. then again I do the same for next 2 exhales and basically the difference between those 2 times is one period. The rest is jsut converting it to breaths per min.
P.P.S
LOL I just figured that 2 if loops should be inside for loop (I edited it now). I will try it again tomorrow to see if it works as it should but in the meantime You can get me Your opinions on this code.

#include <OneWire.h>
#include <DallasTemperature.h>


#define ONE_WIRE_BUS 2

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);


//numbers
float vals[5];
float samples = 5.0;
unsigned long time, period_respiration;
float p, f;




void setup(void) {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  Serial.println("Dallas Temperature IC Control Library Demo");

  // Start up the library
  sensors.begin();
}

// the loop routine runs over and over again forever:
void loop(void) 
{
  // read the input on analog pin 0:
  
  int i = 0;
  
  
  for (i = 0; i < samples; i++)
  {      
    //grab the sensor value
    sensors.requestTemperatures();
    int sensorValue = sensors.getTempCByIndex(0);
    delay(1200);
    vals[i] = sensorValue;

    if(vals[i+1]>vals[i])
  {
     time=millis();
     
     if(vals[i+2]>vals[i+1])
     {unsigned long currentTime = millis();
      period_respiration = currentTime-time;
      p = (float)period_respiration;
      f=p/60000.0;
      Serial.println("Respirations per minute:");
      Serial.println(f);}
     
  }

  }

  
  }

i did some search on it and all I could find was how to determine the value of the peak

If you can do that, you can surely notice WHEN that happened. After noticing two peaks, surely you can determine how far apart they are.

Maybe by using millis() when the first is detected and then another when the next one is detected, storing that in 2 variables and the difference between them is peak period?

robtillaart:
or detemine max to max peak time
or determine min to min peak time
or determine min to max peak time
or ...
or could work with the first derivative

several ways,

I agree,
I'd also throw in mention of:

  • low pass filtering (of both the raw signal, and perhaps the output also)
  • built in hysteresis when using thresholds IF (signal > someThreshold) ...