I am creating a sensor that are able to detect pulse rate (Pulse sensor amped :
http://pulsesensor.myshopify.com/products/pulse-sensor-amped) and body temperature (LM35) and send the result wirelessly using XBee.
The component I used are:
1. Arduino Uno - R3
2. XBee Shield v1.1 - Enhanced XBee Breakout for Arduino
3. Xbee USB Adapter
4. XBee 1mW Wire Antenna - Series 1
So what I do is, Put the XBee Shield v1.1 on top of Arduino UNO-R3 with XBee 1mW Wire Antenna attached to it.
Then connect jumper wire from the Xbee Shield to breadboard, connect the LM35 ad pulse sensor both to the breadboard.
But I came across some problem.
1st the pulse sensor is not working correctly. It give wrong reading. Here are the code:
unsigned char pin = 13;
unsigned char counter=0;
unsigned int heart_rate=0;
unsigned long temp[21];
unsigned long sub=0;
volatile unsigned char state = LOW;
bool data_effect=true;
const int max_heartpluse_duty=2000;//you can change it follow your system's request.2000 meams 2 seconds.
//System return error if the duty overtrip 2 second.
void setup()
{
pinMode(pin, OUTPUT);
Serial.begin(9600);
Serial.println("Please ready your eat clip.");
delay(5000);
array_init();
Serial.println("Heart rate test begin.");
attachInterrupt(1, interrupt, RISING);//set interrupt 0,digital port 2
}
void loop()
{
digitalWrite(pin, state);
}
void sum()//calculate the heart rate
{
if(data_effect)
{
heart_rate=120000/(temp[20]-temp[0]);//6*20*1000/20_total_time
Serial.print("Heart_rate_is:\t");
Serial.println(heart_rate);
}
data_effect=1;//sign bit
}
void interrupt()
{
temp[counter]=millis();
state = !state;
Serial.println(counter,DEC);
Serial.println(temp[counter]);
switch(counter)
{
case(0):
sub=temp[counter]-temp[20];
Serial.println(sub);
break;
default:
sub=temp[counter]-temp[counter-1];
Serial.println(sub);
break;
}
if(sub>max_heartpluse_duty)//set 2 seconds as max heart pluse duty
{
data_effect=0;//sign bit
counter=0;
Serial.println("Heart rate measure error,test will restart!" );
array_init();
}
if (counter==20&&;data_effect)
{
counter=0;
sum();
}
else if(counter!=20&&;data_effect)
counter++;
else
{
counter=0;
data_effect=1;
}
}
void array_init()
{
for(unsigned char i=0;i!=20;++i)
{
temp[i]=0;
}
temp[20]=millis();
}
2nd, the temperature sensor (LM35) is working properly before I attached the XBEE, but after I attached the XBee, it give wrong temperature numbers. Do I need to do any voltage adjustment or stuff like that? Here is my code for the temperature sensor:
int potPin = 0;
float temperature = 0;
void setup()
{
Serial.begin(9600);
Serial.println("LM35 Thermometer ");
analogReference(INTERNAL);
}
void printTenths(int value) {
// prints a value of 123 as 12.3
Serial.print(value / 10);
Serial.print(".");
Serial.println(value % 10);
}
void loop() {
int span = 20;
int aRead = 0;
for (int i = 0; i < span; i++) {
aRead = aRead+analogRead(potPin);
}
aRead = aRead / 20;
temperature = ((100*1.1*aRead)/1024)*10;
// convert voltage to temperature
Serial.print("Analog in reading: ");
Serial.print(long(aRead));
// print temperature value on serial monitor
Serial.print(" - Calculated Temp: ");
printTenths(long(temperature));
delay(500);
}