Hi guys. I’ve been trying to use a pulse sensor (Pulse Sensor Amped : ID 1093 : $25.00 : Adafruit Industries, Unique & fun DIY electronics and kits) to plot heartbeat and measure beats per minute. In order to calculate beats per minute, I’ve been using code from the following project: http://www.xtronical.com/basics/heart-beat-sensor-ecg-display/
Problem:
The problem I’ve been having is that printing the BPM piece of code makes my single plot turn into five plots.
Goal:
Accurate BPM measurements with single plot of heartbeat.
Attempt:
Here’s my code.
#include <Adafruit_GFX.h>
#include <UTFTGLUE.h>
UTFTGLUE myGLCD(0x9488,A2,A1,A3,A4,A0);
#if !defined(SmallFont)
extern uint8_t SmallFont[];
#endif
int buf[478];
int x = 0;
int LastTime = 0;
bool BPMTiming = false;
bool BeatComplete = false;
int BPM = 0;
#define UpperThreshold 560
#define LowerThreshold 500
void setup()
{
Serial.begin(9600);
randomSeed(analogRead(5));
pinMode(A0, OUTPUT);
digitalWrite(A0, HIGH);
myGLCD.InitLCD();
myGLCD.setFont(SmallFont);
myGLCD.clrScr();
}
void loop()
{
for (int i=1; i<(477*30); i++)
{
delay(20);
x++;
if (x==478)
x=1;
if (i>478)
{
if ((x==239)||(buf[x-1]==159))
myGLCD.setColor(0,0,255);
else
myGLCD.setColor(0,0,0);
myGLCD.drawPixel(x,buf[x-1]);
}
myGLCD.setColor(0,255,255);
int value=analogRead(5);
int y=(value/3); //Responsible for Y-axis.
myGLCD.drawPixel(x, y);
buf[x-1]=y;
// Calculate when a beat has been completed
if(value>UpperThreshold)
{
if(BeatComplete)
{
BPM=millis()-LastTime;
BPM=int(60/(float(BPM)/1000));
BPMTiming=false;
BeatComplete=false;
}
if(BPMTiming==false)
{
LastTime=millis();
BPMTiming=true;
}
}
if((value<LowerThreshold)&(BPMTiming))
BeatComplete=true;
// Display BPM
myGLCD.setColor(255,255,255);
myGLCD.printNumI(BPM, 40, 34); <---- This line is the problem, I think.
}
}
When “myGLCD.printNumI(BPM, 40, 34);” is commented out, the wave looks fine, as shown in the attached image. When it is not commented out, the wave looks like the second image, with multiple waves overlapping.
I’ve tried moving it to different positions in the loop to see if that changes anything. I can’t figure out why it’s having that effect when all it is doing is displaying a calculated integer.
Could anyone give me a hint as to why it may be doing this, or suggest a different way of displaying the required value? I’ve been through the UTFT manual and the only command that allows me to print an integer is “myGLCD.printNumI”. Perhaps someone knows another way around this, or why the interaction is happening. Thank you.