Skip to content

Instantly share code, notes, and snippets.

@c010

c010/bpm.ino Secret

Last active December 7, 2015 22:35
Show Gist options
  • Save c010/90d61f970580bb5f6fc1 to your computer and use it in GitHub Desktop.
Save c010/90d61f970580bb5f6fc1 to your computer and use it in GitHub Desktop.
void bpm()
{
permin = max_count*6;//to calculate your breaths per min
}
float x = 0;//the starting value
int stats = 0;
void inOrout()
{
float reading;
reading = analogRead(THERMISTORPIN);
// convert the value to resistance
reading = (1023 / reading) - 1;
reading = SERIESRESISTOR / reading;// the numbers that the rubber is getting
if ( reading > x)//inhaling
// if the current restistence is greater then previos resistence
{
x = reading;// constantly store the conductive rubber reading in x
if (stats == 0)// has yet to exhale
{
max_count++;// increase the number of breaths by 1
stats = 1;
}
}
else if (reading < x)//exhaling
{
x = reading;
if (stats == 1) //while reading is getting smaller the state is 0
//therefore when reading gets bigger again it will know
//that you are inhaling agian adding to the counter
{
stats = 0;
}
}
}
int max_count = 0;
int permin;
#define THERMISTORPIN A5
#define SERIESRESISTOR 10000
//int lightValue = 0;
String inString = "";
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(THERMISTORPIN, INPUT);
}
void loop()
{
timer();
}
unsigned long interval = 10000;
unsigned long previousMillis = 0;
void timer()
{
unsigned long currentMillis = millis();
inOrout();
// if time is greater than 10 secconds do this if statment
if ((unsigned long)(currentMillis - previousMillis) >= interval)
{
previousMillis = currentMillis;
bpm();
Serial.print("Breaths per minute: ");Serial.println(permin);
max_count= 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment