In this part, there is not much coding for the measurement respiration rate. i found some coding but there are certain part that need to be change. in order to measure respiration rate, the temperature change must be faster to the calculation in the micro controller to be correct. The coding for the respiration rate measure the temperature sensor in a ADC value. So, the voltage has be read as a ADC value. here the coding for the respiration rate measurement.
#include <LiquidCrystal.h>
enum respState
{
Inhaling,
Exhaling
};
unsigned long prevRespTime = 0;
unsigned long currRespTime = 0;
respState currentRespState;
int movingAvgRespRate = 0;
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup()
{
lcd.begin(16, 2);
Serial.begin(9600);
pinMode(9,OUTPUT); //buzzer
}
void loop()
{
//delay(500);
// don't need to delay because we'll only fire once per inhalation and once per exhalation
int sensorValue = analogRead(A0);
if (sensorValue > 510 && currentRespState == Exhaling)
{
currentRespState = Inhaling;
prevRespTime = currRespTime; // save time from last cycle
currRespTime = millis();
float period_respiration = currRespTime - prevRespTime;
float breathrate = 60000 / period_respiration;
Serial.print("Inhaling \tRespirations per minute: ");
Serial.println(breathrate);
lcd.setCursor(0, 1);
lcd.print("RPM");
lcd.print(breathrate);
if (breathrate>1 && breathrate<9)
{
digitalWrite(9,HIGH);
delay(2000);
digitalWrite(9,LOW);
delay(200);
}
if (breathrate>60 && breathrate<100)
{
digitalWrite(9,HIGH);
delay(1000);
digitalWrite(9,LOW);
delay(200);
}
else if (breathrate>101 && breathrate<500 )
{
digitalWrite(9,HIGH);
delay(500);
digitalWrite(9,LOW);
delay(200);
}
else
{
digitalWrite(9,LOW);
}
}
else if (currentRespState == Inhaling && sensorValue < 490)
{
currentRespState = Exhaling;
Serial.println("Exhaling");
lcd.setCursor(0, 0);
lcd.print("Exhaling ");
}
}
The sensor value was set from the voltage of the temperature changes. When exhale, the sensor value is more than 510 and when inhale the voltage less than 490.
But the value for the sensor value not a fix value.