Hello, I need help with my code.
I am building a heart rate monitor that also includes a temperature measurement. The code for the heartbeat measurement is from [here] (https://www.dofbot.com/post/heart-beat-sensor-interface-with-arduino-and-oled-display). The code for the temperature measurement is from the example of the sensor.
Now to my question: how do I do this where BPM is currently displayed, so that every 10 seconds the temperature is also displayed for 5 seconds and then it switches back to the BPM for 10 seconds etc.? However, the graph showing the heart rate should continue to run and the BPM measurement too. That's why it doesn't work if I do it the classic way with a counter and an if else loop. In my current code it starts with showing the heartbeat for 10 seconds, then it changes to the temperature for 10 seconds and then the BPM flashes again for a very short time. could someone please tell me where my problem lies and preferably also attach suitable code for it.
best regards
Jonathan
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Configure OLED screen size in pixels
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Variable Declarations
unsigned long previousMillisGetHR = 0;
unsigned long previousMillisResultHR = 0;
unsigned long previousMillisGetTemp = 0;
unsigned long previousMillisResultTemp = 0;
const long intervalGetHR = 20; // Interval for reading heart rate (Heartbeat) = 10ms.
const long intervalGetTemp = 20;
const long intervalResultHR = 10000; // The reading interval for the result of the Heart Rate calculation is in 10 seconds.
const long intervalTemp = 10000;
int PulseSensorSignal;
const int PulseSensorHRWire = 0; // PulseSensor connected to ANALOG PIN 0 (A0 / ADC 0).
const int LED_A1 = A1; // LED to detect when the heart is beating. The LED is connected to PIN A1 on the Arduino UNO.
int UpperThreshold = 550; // Determine which Signal to "count as a beat," and which to ignore.
int LowerThreshold = 500;
int cntHB = 0; // Variable for counting the number of heartbeats.
int cntT = 0;
boolean ThresholdStat = true; // Variable for triggers in calculating heartbeats.
int BPMval = 0; // Variable to hold the result of heartbeats calculation.
int TEMPval = 0;
int x = 0; // Variable axis x graph values to display on OLED
int y = 0; // Variable axis y graph values to display on OLED
int lastx = 0; // The graph's last x axis variable value to display on the OLED
int lasty = 0; // The graph's last y axis variable value to display on the OLED
float temp = 0;
// 'Heart_Icon', 16x16px
// I drew this heart icon at : http://dotmatrixtool.com/
const unsigned char Heart_Icon[] PROGMEM = {
0x00, 0x00, 0x18, 0x30, 0x3c, 0x78, 0x7e, 0xfc, 0xff, 0xfe, 0xff, 0xfe, 0xee, 0xee, 0xd5, 0x56,
0x7b, 0xbc, 0x3f, 0xf8, 0x1f, 0xf0, 0x0f, 0xe0, 0x07, 0xc0, 0x03, 0x80, 0x01, 0x00, 0x00, 0x00};
void setup()
{
pinMode(LED_A1, OUTPUT); // Set LED_3 PIN as Output.
Serial.begin(9600); // Set up Serial Communication at a certain speed.
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))
{
Serial.println(F("SSD1306 allocation failed"));
for (;;)
; // Don't proceed, loop forever
}
// Show initial display buffer contents on the screen
// the library initializes this with an Adafruit splash screen.
display.display();
delay(1000);
// Displays BPM value reading information
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 12);
display.print(" Please wait");
display.setCursor(0, 22);
display.print(" 10 seconds");
display.setCursor(0, 32);
display.print(" to get");
display.setCursor(0, 42);
display.print(" the Heart Rate value");
display.display();
delay(3000);
// Displays the initial display of BPM value
display.clearDisplay();
display.drawBitmap(0, 47, Heart_Icon, 16, 16, WHITE);
display.drawLine(0, 43, 127, 43, WHITE);
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(20, 48);
display.print(": 0 BPM");
display.display();
Serial.println();
Serial.println("Please wait 10 seconds to get the BPM Value");
}
void loop()
{
GetHeartRate();
GetTemperature();
}
void GetHeartRate()
{
// Process of reading heart rate.
unsigned long currentMillisGetHR = millis();
if (currentMillisGetHR - previousMillisGetHR >= intervalGetHR)
{
previousMillisGetHR = currentMillisGetHR;
PulseSensorSignal = analogRead(PulseSensorHRWire);
if (PulseSensorSignal > UpperThreshold && ThresholdStat == true)
{
cntHB++;
ThresholdStat = false;
digitalWrite(LED_A1, HIGH);
}
if (PulseSensorSignal < LowerThreshold)
{
ThresholdStat = true;
digitalWrite(LED_A1, LOW);
}
DrawGraph(); // Calling the DrawGraph() subroutine
}
// The process for getting the BPM value.
unsigned long currentMillisResultHR = millis();
if (currentMillisResultHR - previousMillisResultHR >= intervalResultHR)
{
previousMillisResultHR = currentMillisResultHR;
BPMval = cntHB * 6; // The taken heart rate is for 10 seconds. So to get the BPM value, the total heart rate in 10 seconds x 6.
Serial.print("BPM : ");
Serial.println(BPMval);
display.fillRect(0, 48, 128, 18, BLACK);
display.drawBitmap(0, 47, Heart_Icon, 16, 16, WHITE);
display.drawLine(0, 43, 127, 43, WHITE);
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(20, 48);
display.print(": ");
display.print(BPMval);
display.print(" BPM");
display.display();
cntHB = 0;
}
}
void GetTemperature()
{
unsigned long currentMillisResultTemp = millis();
if(currentMillisResultTemp - previousMillisGetTemp >= intervalTemp)
{
previousMillisResultTemp = currentMillisResultTemp;
int reading = analogRead(A2); // Use A2 pin for the temperature sensor
temp = reading * 0.0048828125 * 100;
display.fillRect(0, 48, 108, 18, BLACK);
display.drawLine(0, 43, 127, 43, WHITE);
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(10, 48);
display.print("Temp:");
display.print(temp);
display.display();
}
}
// Subroutines for drawing or displaying heart rate graphic signals
void DrawGraph()
{
// Condition to reset the graphic display if it fills the width of the OLED screen
if (x > 127)
{
display.fillRect(0, 0, 128, 42, BLACK);
x = 0;
lastx = 0;
}
// Process signal data to be displayed on OLED in graphic form
int ySignal = PulseSensorSignal;
if (ySignal > 850)
ySignal = 850;
if (ySignal < 350)
ySignal = 350;
int ySignalMap = map(ySignal, 350, 850, 0, 40);
y = 40 - ySignalMap;
// Displays the heart rate graph
display.writeLine(lastx, lasty, x, y, WHITE);
display.display();
lastx = x;
lasty = y;
x++;
}
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.