Thx sonyhome for your help..i think i already get the result for the oxygen saturation..now i want to ask about measure heartbeat and oxygen saturation in one program..i have 2 sepatare coding
This is for measuring heartbeat:
// Pins
const int ledPin = 13;
const int sensePin = 0;
// LED blink variables
int ledState = LOW;
long ledOnMillis = 0;
long ledOnInterval = 50;
// Hearbeat detect variables
int newHeartReading = 0;
int lastHeartReading = 0;
int Delta = 0;
int recentReadings[8] = {0,0,0,0,0,0,0,0};
int historySize = 8;
int recentTotal = 0;
int readingsIndex = 0;
boolean highChange = false;
int totalThreshold = 2;
// Heartbeat Timing
long lastHeartbeatTime = 0;
long debounceDelay = 150;
int currentHeartrate = 0;
void setup() {
// initialize the serial communication:
Serial.begin(115200);
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
}
void loop() {
// Turn off LED
digitalWrite(ledPin, LOW);
// Read analogue pin.
newHeartReading = analogRead(sensePin);
//Serial.println(newHeartReading);
//Calculate Delta
Delta = newHeartReading - lastHeartReading;
lastHeartReading = newHeartReading;
// Find new recent total
recentTotal = recentTotal - recentReadings[readingsIndex] + Delta;
// replace indexed recent value
recentReadings[readingsIndex] = Delta;
// increment index
readingsIndex = (readingsIndex + 1) % historySize;
//Debug
//Serial.println(recentTotal);
// Decide whether to start an LED Blink.
if (recentTotal >= totalThreshold) {
// Possible heartbeart, check time
if (millis() - lastHeartbeatTime >= debounceDelay) {
// Heartbeat
digitalWrite(ledPin, HIGH);
currentHeartrate = 60000 / (millis() - lastHeartbeatTime);
lastHeartbeatTime = millis();
// Print Results
//Serial.println("Beat");
if (currentHeartrate <= 200)
{ Serial.println(currentHeartrate); } } } delay(10); }
This one for measure oxygen level;
int ledPin = 13;
int sensorPin = 0;
double alpha = 0.75; //using 75% of the average of the previous values and 25% of the current value
int period = 5; // This is how long the code delays in milliseconds between readings (10 mSec)
double change = 0.0; //to be used to detect the peaks
int z;
void setup()
{
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
}
void loop()
{
static double oldValue = 0; // used for averaging.
static double oldChange = 0;
int rawValue = analogRead(sensorPin)-700; // This reads in the value from the analog pin
double value = alpha * oldValue + (1 - alpha) * (rawValue);// Calculate an average using 75% of the previous value and 25% of the new
change = value - oldValue;
z = log(1/value)/(log(1/rawValue));
digitalWrite(ledPin, (change < 0.00 && oldChange > 0.00));
delay(2000);
Serial.println("IR Intensity led intensity");
Serial.print(rawValue);
Serial.print(" ");
Serial.print(value);
Serial.println(" ");
oldValue = value;
oldChange = change;
}
and also the formula is unread..when i click the serial monitor the value z was 0..
anyone can help?