I am currently doing the project in pulse measurement using an Arduino Yún. I am using the pulse sensor SEN-11574.
When I verify the code I get this error:
Arduino: 1.5.8 (Linux), Board: "Arduino Yún"
pulse-sensor-arduino/pulse-sensor-arduino.cpp.o: In function `PulseSensor::begin(int)':
/home/system1/Arduino/libraries/pulse-sensor-arduino/pulse-sensor-arduino.cpp:39: multiple definition of `pulsePin'
sketch_feb10a.cpp.o:/home/system1/Downloads/arduino-1.5.8/sketch_feb10a.ino:18: first defined here
pulse-sensor-arduino/pulse-sensor-arduino.cpp.o: In function `PulseSensor::begin(int)':
/home/system1/Arduino/libraries/pulse-sensor-arduino/pulse-sensor-arduino.cpp:39: multiple definition of `Pulse'
sketch_feb10a.cpp.o:/home/system1/Downloads/arduino-1.5.8/sketch_feb10a.ino:18: first defined here
collect2: error: ld returned 1 exit status
Error compiling.
This is my code:
#include <pulse-sensor-arduino.h>
int pulsePin = 0;
int blinkPin = 13;
int fadePin = 5;
int fadeRate = 0;
volatile int BPM;
volatile int Signal;
volatile int IBI = 600;
volatile boolean Pulse = false;
volatile boolean QS = false;
void setup() {
pinMode(blinkPin, OUTPUT);
pinMode(fadePin, OUTPUT);
Serial.begin(115200);
}
void loop() {
sendDataToProcessing('S', Signal);
if (QS == true) {
fadeRate = 255;
sendDataToProcessing('B',BPM);
sendDataToProcessing('Q',IBI);
QS = false;
}
ledFadeToBeat();
delay(5000);
}
void ledFadeToBeat() {
fadeRate -= 15;
fadeRate = constrain(fadeRate,0,255);
analogWrite(fadePin,fadeRate);
}
void sendDataToProcessing(char symbol, int data ) {
Serial.print(symbol);
Serial.println(data);
}
This is my pulse Arduino cpp file:
#include "pulse-sensor-arduino.h"
volatile int rate[10]; // Array to hold last ten IBI values
volatile unsigned long sampleCounter = 0; // Used to determine pulse timing
volatile unsigned long lastBeatTime = 0; // Used to find IBI
volatile int P =512; // Used to find peak in pulse wave, seeded
volatile int T = 512; // Used to find trough in pulse wave, seeded
volatile int thresh = 512; // Used to find instant moment of heart beat, seeded
volatile int amp = 100; // Used to hold amplitude of pulse waveform, seeded
volatile boolean firstBeat = true; // Used to seed rate array so we startup with reasonable BPM
volatile boolean secondBeat = false; // Used to seed rate array so we startup with reasonable BPM
volatile boolean Pulse = false; // True when pulse wave is high, false when it's low
volatile int pulsePin;
volatile int PulseSensor::IBI = 600; // Holds the time between beats, must be seeded!
volatile int PulseSensor::BPM; // Used to hold the pulse rate
volatile int PulseSensor::Signal; // Holds the incoming raw data
volatile boolean PulseSensor::QS = false; // Becomes true when Arduino finds a beat
void PulseSensor::begin(int pPin)
{
pinMode(pulsePin, INPUT);
pulsePin = pPin;
// Initializes Timer1 to throw an interrupt every 2 ms.
TCCR1A = 0x00;
TCCR1B = 0x0C; // CTC (Compare match mode) and ClockIO/256
OCR1A = 0x7C; // 2 ms
TIMSK1 = 0x02;
sei(); // MAKE SURE GLOBAL INTERRUPTS ARE ENABLED
}
// THIS IS THE TIMER 1 INTERRUPT SERVICE ROUTINE.
// Timer 1 makes sure that we take a reading every 2 miliseconds
ISR(TIMER1_COMPA_vect)
{ // Triggered when Timer1 counts to 124
cli(); // Disable interrupts while we do this
PulseSensor::Signal = analogRead(pulsePin); // Read the Pulse Sensor
sampleCounter += 2; // Keep track of the time in ms with this variable
int N = sampleCounter - lastBeatTime; // Monitor the time since the last beat to avoid noise
// Find the peak and trough of the pulse wave
if(PulseSensor::Signal < thresh && N > (PulseSensor::IBI/5)*3){ // Avoid dichrotic noise by waiting 3/5 of last IBI
if (PulseSensor::Signal < T){ // T is the trough
T = PulseSensor::Signal; // Keep track of lowest point in pulse wave
}
}
if(PulseSensor::Signal > thresh && PulseSensor::Signal > P){ // Threshold condition helps avoid noise
P = PulseSensor::Signal; // P is the peak
} // Keep track of highest point in pulse wave
// NOW IT'S TIME TO LOOK FOR THE HEART BEAT
//
// The signal surges up in value every time there is a pulse
if (N > 250){ // Avoid high-frequency noise
if ( (PulseSensor::Signal > thresh) && (Pulse == false) && (N > (PulseSensor::IBI/5)*3) )
{
Pulse = true; // Set the Pulse flag when we think there is a pulse
PulseSensor::IBI = sampleCounter - lastBeatTime; // Measure time between beats in mS
lastBeatTime = sampleCounter; // Keep track of time for next pulse
if(secondBeat){ // If this is the second beat, if secondBeat == TRUE
secondBeat = false; // Clear secondBeat flag
for(int i=0; i<=9; i++){ // Seed the running total to get a realisitic BPM at startup
rate[i] = PulseSensor::IBI;
}
}
if(firstBeat){ // If it's the first time we found a beat, if firstBeat == TRUE
firstBeat = false; // Clear firstBeat flag
secondBeat = true; // Set the second beat flag
sei(); // Enable interrupts again
return; // IBI value is unreliable so discard it
}
// Keep a running total of the last 10 IBI values
word runningTotal = 0; // Clear the runningTotal variable
for(int i=0; i<=8; i++){ // Shift data in the rate array
rate[i] = rate[i+1]; // And drop the oldest IBI value
runningTotal += rate[i]; // Add up the 9 oldest IBI values
}
rate[9] = PulseSensor::IBI; // add the latest IBI to the rate array
runningTotal += rate[9]; // Add the latest IBI to runningTotal
runningTotal /= 10; // Average the last 10 IBI values
PulseSensor::BPM = 60000/runningTotal; // How many beats can fit into a minute? that's BPM!
PulseSensor::QS = true; // Set Quantified Self flag
// QS FLAG IS NOT CLEARED INSIDE THIS ISR
}
}
if (PulseSensor::Signal < thresh && Pulse == true){ // When the values are going down, the beat is over
Pulse = false; // Reset the Pulse flag so we can do it again
amp = P - T; // Get amplitude of the pulse wave
thresh = amp/2 + T; // Set thresh at 50% of the amplitude
P = thresh; // Reset these for next time
T = thresh;
}
if (N > 2500){ // If 2.5 seconds go by without a beat
thresh = 512; // Set thresh default
P = 512; // Set P default
T = 512; // Set T default
lastBeatTime = sampleCounter; // Bring the lastBeatTime up to date
firstBeat = true; // Set these to avoid noise
secondBeat = false; // When we get the heartbeat back
}
sei(); // Enable interrupts when youre done!
} // end ISR
This is my pulse Arduino header file:
#ifndef PULSE_SENSOR_ARDUINO_H
#define PULSE_SENSOR_ARDUINO_H
#include "Arduino.h"
class PulseSensor
{
public:
static void begin(int pulsePin);
static volatile int BPM;
static volatile int Signal;
static volatile int IBI;
static volatile boolean QS;
};
#endif
I modified the program, but there is a new error present:
Arduino: 1.5.8 (Linux), Board: "Arduino Yún"
pulse-sensor-arduinom/pulse-sensor-arduino.cpp.o: In function `PulseSensor::begin(int)':
/home/system1/Arduino/libraries/pulse-sensor-arduinom/pulse-sensor-arduino.cpp:39: multiple definition of `PulseSensor::begin(int)'
pulsesensor.cpp.o:pulsesensor.cpp:39: first defined here
pulse-sensor-arduinom/pulse-sensor-arduino.cpp.o: In function `PulseSensor::begin(int)':
/home/system1/Arduino/libraries/pulse-sensor-arduinom/pulse-sensor-arduino.cpp:39: multiple definition of `pulsePin'
pulsesensor.cpp.o:pulsesensor.cpp:39: first defined here
pulse-sensor-arduinom/pulse-sensor-arduino.cpp.o: In function `__vector_17':
/home/system1/Arduino/libraries/pulse-sensor-arduinom/pulse-sensor-arduino.cpp:54: multiple definition of `__vector_17'
pulsesensor.cpp.o:pulsesensor.cpp:54: first defined here
pulse-sensor-arduinom/pulse-sensor-arduino.cpp.o: In function `PulseSensor::begin(int)':
/home/system1/Arduino/libraries/pulse-sensor-arduinom/pulse-sensor-arduino.cpp:39: multiple definition of `PulseSensor::Signal'
pulsesensor.cpp.o:pulsesensor.cpp:39: first defined here
pulse-sensor-arduinom/pulse-sensor-arduino.cpp.o: In function `PulseSensor::begin(int)':
/home/system1/Arduino/libraries/pulse-sensor-arduinom/pulse-sensor-arduino.cpp:39: multiple definition of `sampleCounter'
pulsesensor.cpp.o:pulsesensor.cpp:39: first defined here
pulse-sensor-arduinom/pulse-sensor-arduino.cpp.o: In function `PulseSensor::begin(int)':
/home/system1/Arduino/libraries/pulse-sensor-arduinom/pulse-sensor-arduino.cpp:39: multiple definition of `lastBeatTime'
pulsesensor.cpp.o:pulsesensor.cpp:39: first defined here
pulse-sensor-arduinom/pulse-sensor-arduino.cpp.o:(.data.thresh+0x0): multiple definition of `thresh'
pulsesensor.cpp.o:(.data.thresh+0x0): first defined here
pulse-sensor-arduinom/pulse-sensor-arduino.cpp.o:(.data._ZN11PulseSensor3IBIE+0x0): multiple definition of `PulseSensor::IBI'
pulsesensor.cpp.o:(.data._ZN11PulseSensor3IBIE+0x0): first defined here
pulse-sensor-arduinom/pulse-sensor-arduino.cpp.o:(.data.T+0x0): multiple definition of `T'
pulsesensor.cpp.o:(.data.T+0x0): first defined here
pulse-sensor-arduinom/pulse-sensor-arduino.cpp.o:(.data.P+0x0): multiple definition of `P'
pulsesensor.cpp.o:(.data.P+0x0): first defined here
pulse-sensor-arduinom/pulse-sensor-arduino.cpp.o: In function `PulseSensor::begin(int)':
/home/system1/Arduino/libraries/pulse-sensor-arduinom/pulse-sensor-arduino.cpp:39: multiple definition of `Pulse'
pulsesensor.cpp.o:pulsesensor.cpp:39: first defined here
pulse-sensor-arduinom/pulse-sensor-arduino.cpp.o: In function `PulseSensor::begin(int)':
/home/system1/Arduino/libraries/pulse-sensor-arduinom/pulse-sensor-arduino.cpp:39: multiple definition of `rate'
pulsesensor.cpp.o:pulsesensor.cpp:39: first defined here
pulse-sensor-arduinom/pulse-sensor-arduino.cpp.o: In function `PulseSensor::begin(int)':
/home/system1/Arduino/libraries/pulse-sensor-arduinom/pulse-sensor-arduino.cpp:39: multiple definition of `PulseSensor::BPM'
pulsesensor.cpp.o:pulsesensor.cpp:39: first defined here
pulse-sensor-arduinom/pulse-sensor-arduino.cpp.o: In function `PulseSensor::begin(int)':
/home/system1/Arduino/libraries/pulse-sensor-arduinom/pulse-sensor-arduino.cpp:39: multiple definition of `PulseSensor::QS'
pulsesensor.cpp.o:pulsesensor.cpp:39: first defined here
pulse-sensor-arduinom/pulse-sensor-arduino.cpp.o:(.data.firstBeat+0x0): multiple definition of `firstBeat'
pulsesensor.cpp.o:(.data.firstBeat+0x0): first defined here
pulse-sensor-arduinom/pulse-sensor-arduino.cpp.o: In function `PulseSensor::begin(int)':
/home/system1/Arduino/libraries/pulse-sensor-arduinom/pulse-sensor-arduino.cpp:39: multiple definition of `secondBeat'
pulsesensor.cpp.o:pulsesensor.cpp:39: first defined here
pulse-sensor-arduinom/pulse-sensor-arduino.cpp.o:(.data.amp+0x0): multiple definition of `amp'
pulsesensor.cpp.o:(.data.amp+0x0): first defined here
collect2: error: ld returned 1 exit status
Error compiling.
Here is my change code
#include <pulse-sensor-arduino.h>
int blinkPin = 13; // Pin to blink LED at each beat
int fadePin = 5; // Pin to do fancy classy fading blink at each beat
int fadeRate = 0; // Used to fade LED on with PWM on fadePin
void setup(){
pinMode(blinkPin,OUTPUT); // Pin that will blink to your heartbeat!
pinMode(fadePin,OUTPUT); // Pin that will fade to your heartbeat!
Serial.begin(115200); // We agree to talk fast!
}
void loop(){
sendDataToProcessing('P', PulseSensor::Signal); // Send Processing the raw Pulse Sensor data
if (PulseSensor::QS == true){ // Quantified Self flag is true when arduino finds a heartbeat
fadeRate = 255; // Set 'fadeRate' Variable to 255 to fade LED with pulse
sendDataToProcessing('B',PulseSensor::BPM); // Send heart rate with a 'B' prefix
sendDataToProcessing('Q',PulseSensor::IBI); // Send time between beats with a 'Q' prefix
PulseSensor::QS = false; // Reset the Quantified Self flag for next time
}
ledFadeToBeat();
delay(5000); // Take a break
}
void ledFadeToBeat(){
fadeRate -= 15; // Set LED fade value
fadeRate = constrain(fadeRate,0,255); // Keep LED fade value from going into negative numbers!
analogWrite(fadePin,fadeRate); // Fade LED
}
void sendDataToProcessing(char symbol, int data ){
Serial.print(symbol); // Symbol prefix tells Processing what type of data is coming
Serial.println(data); // The data to send culminating in a carriage return
}
My cpp file:
#include "pulse-sensor-arduino.h"
volatile int rate[10]; // Array to hold last ten IBI values
volatile unsigned long sampleCounter = 0; // Used to determine pulse timing
volatile unsigned long lastBeatTime = 0; // Used to find IBI
volatile int P =512; // Used to find peak in pulse wave, seeded
volatile int T = 512; // Used to find trough in pulse wave, seeded
volatile int thresh = 512; // Used to find instant moment of heart beat, seeded
volatile int amp = 100; // Used to hold amplitude of pulse waveform, seeded
volatile boolean firstBeat = true; // Used to seed rate array so we startup with reasonable BPM
volatile boolean secondBeat = false; // Used to seed rate array so we startup with reasonable BPM
volatile boolean Pulse = false; // True when pulse wave is high, false when it's low
volatile int pulsePin;
volatile int PulseSensor::IBI = 600; // Holds the time between beats, must be seeded!
volatile int PulseSensor::BPM; // Used to hold the pulse rate
volatile int PulseSensor::Signal; // Holds the incoming raw data
volatile boolean PulseSensor::QS = false; // Becomes true when Arduino finds a beat
void PulseSensor::begin(int pPin)
{
pinMode(pulsePin, INPUT);
pulsePin = pPin;
// Initializes Timer1 to throw an interrupt every 2 ms.
TCCR1A = 0x00;
TCCR1B = 0x0C; // CTC (Compare match mode) and ClockIO/256
OCR1A = 0x7C; // 2 ms
TIMSK1 = 0x02;
sei(); // MAKE SURE GLOBAL INTERRUPTS ARE ENABLED
}
// THIS IS THE TIMER 1 INTERRUPT SERVICE ROUTINE.
// Timer 1 makes sure that we take a reading every 2 miliseconds
ISR(TIMER1_COMPA_vect)
{ // Triggered when Timer1 counts to 124
cli(); // Disable interrupts while we do this
PulseSensor::Signal = analogRead(pulsePin); // read the Pulse Sensor
sampleCounter += 2; // Keep track of the time in ms with this variable
int N = sampleCounter - lastBeatTime; // Monitor the time since the last beat to avoid noise
// Find the peak and trough of the pulse wave
if(PulseSensor::Signal < thresh && N > (PulseSensor::IBI/5)*3){ // Avoid dichrotic noise by waiting 3/5 of last IBI
if (PulseSensor::Signal < T){ // T is the trough
T = PulseSensor::Signal; // Keep track of lowest point in pulse wave
}
}
if(PulseSensor::Signal > thresh && PulseSensor::Signal > P){ // Threshold condition helps avoid noise
P = PulseSensor::Signal; // P is the peak
} // Keep track of highest point in pulse wave
// NOW IT'S TIME TO LOOK FOR THE HEART BEAT
// signal surges up in value every time there is a pulse
if (N > 250){ // Avoid high frequency noise
if ( (PulseSensor::Signal > thresh) && (Pulse == false) && (N > (PulseSensor::IBI/5)*3) )
{
Pulse = true; // Set the Pulse flag when we think there is a pulse
PulseSensor::IBI = sampleCounter - lastBeatTime; // measure time between beats in mS
lastBeatTime = sampleCounter; // Keep track of time for next pulse
if(secondBeat){ // If this is the second beat, if secondBeat == TRUE
secondBeat = false; // Clear secondBeat flag
for(int i=0; i<=9; i++){ // Seed the running total to get a realisitic BPM at startup
rate[i] = PulseSensor::IBI;
}
}
if(firstBeat){ // If it's the first time we found a beat, if firstBeat == TRUE
firstBeat = false; // Clear firstBeat flag
secondBeat = true; // Set the second beat flag
sei(); // Enable interrupts again
return; // IBI value is unreliable so discard it
}
// Keep a running total of the last 10 IBI values
word runningTotal = 0; // Clear the runningTotal variable
for(int i=0; i<=8; i++){ // Shift data in the rate array
rate[i] = rate[i+1]; // And drop the oldest IBI value
runningTotal += rate[i]; // Add up the 9 oldest IBI values
}
rate[9] = PulseSensor::IBI; // Add the latest IBI to the rate array
runningTotal += rate[9]; // Add the latest IBI to runningTotal
runningTotal /= 10; // Average the last 10 IBI values
PulseSensor::BPM = 60000/runningTotal; // How many beats can fit into a minute? that's BPM!
PulseSensor::QS = true; // Set Quantified Self flag
// QS FLAG IS NOT CLEARED INSIDE THIS ISR
}
}
if (PulseSensor::Signal < thresh && Pulse == true){ // When the values are going down, the beat is over
Pulse = false; // Reset the Pulse flag so we can do it again
amp = P - T; // Get amplitude of the pulse wave
thresh = amp/2 + T; // Set thresh at 50% of the amplitude
P = thresh; // Reset these for next time
T = thresh;
}
if (N > 2500){ // If 2.5 seconds go by without a beat
thresh = 512; // Set thresh default
P = 512; // Set P default
T = 512; // Set T default
lastBeatTime = sampleCounter; // Bring the lastBeatTime up to date
firstBeat = true; // Set these to avoid noise
secondBeat = false; // When we get the heartbeat back
}
sei(); // Enable interrupts when youre done!
} // end ISR