/*Heart pulse sensor with Arduino and OLED display
*/
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 circuit = Adafruit_SSD1306(128, 32, &Wire); // Change OLED display size to 128x32
#define sensor A0
#define Highpulse 540
int sX = 0;
int sY = 30; // Change Y position of the pulse graph to make room for BPM display
int x = 0;
int Svalue;
int value;
long Stime = 0;
long Ltime = 0;
int count = 0;
int Bpm = 0;
void setup() {
Serial.begin(9600);
circuit.begin(SSD1306_SWITCHCAPVCC, 0x3C);// Address 0x3C for 128x32
delay(1000);
circuit.clearDisplay();
}
void loop() {
Svalue = analogRead(sensor);
Serial.println(Svalue);
value = map(Svalue, 0, 1024, 0, 45);
int y = 30 - value; // Change Y position of the pulse graph to make room for BPM display
if (x > 128) {
x = 0;
sX = 0;
circuit.clearDisplay();
}
circuit.drawLine(sX, sY, x, y, WHITE);
sX = x;
sY = y;
x ++;
BPM();
circuit.setCursor(0, 0);
circuit.setTextSize(2);
circuit.setTextColor(SSD1306_WHITE);
circuit.print("BPM :");
circuit.print(BPM); // Display BPM value instead of count
circuit.display();
}
void BPM() {
if (Svalue > Highpulse) {
Stime = millis() - Ltime;
count++;
if (Stime / 1000 >= 60) {
Ltime = millis();
Serial.println(count);
Bpm = count; // Store BPM value instead of count
count = 0;
}
}
}