#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#define IR_LED_PIN 3 // Pin untuk IR LED
#define PHOTODIODE_PIN A0 // Pin untuk Fotodioda (Sensor Cahaya)
#define LED_PIN 10 // LED untuk indikator
#define BUZZER_PIN 12 // Buzzer untuk indikator
LiquidCrystal_I2C lcd(0x27, 16, 2); // LCD I2C alamat 0x27
int batasBawah = 60; // Batas bawah detak jantung (BPM)
int batasAtas = 100; // Batas atas detak jantung (BPM)
const byte ROW_NUM = 4; // Jumlah baris
const byte COLUMN_NUM = 4; // Jumlah kolom
char keys[ROW_NUM][COLUMN_NUM] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte pin_rows[ROW_NUM] = {9, 8, 7, 6};
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2};
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);
// Variabel untuk detak jantung
int sensorValue = 0;
int lastSensorValue = 0;
unsigned long lastBeatTime = 0;
int beatsPerMinute = 0;
unsigned long pulseStartTime = 0;
unsigned long pulseDuration = 0;
int beatCount = 0;
void setup() {
pinMode(IR_LED_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
lcd.begin(16, 2);
lcd.backlight();
// Inisialisasi IR LED untuk memancar cahaya
digitalWrite(IR_LED_PIN, HIGH); // Menyalakan IR LED
lcd.print("Heart Rate Monitor");
delay(2000);
}
void loop() {
// Membaca nilai dari fotodioda
sensorValue = analogRead(PHOTODIODE_PIN);
// Menghitung detak jantung berdasarkan fluktuasi nilai sensor
if (sensorValue < lastSensorValue && (millis() - lastBeatTime > 500)) {
// Deteksi titik puncak fluktuasi
pulseDuration = millis() - pulseStartTime;
pulseStartTime = millis();
beatCount++;
if (pulseDuration > 0) {
beatsPerMinute = 60000 / pulseDuration; // BPM = 60000 / durasi
}
lastBeatTime = millis();
}
// Update tampilan LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("BPM: ");
lcd.print(beatsPerMinute);
// Menampilkan indikator LED dan buzzer
if (beatsPerMinute < batasBawah || beatsPerMinute > batasAtas) {
digitalWrite(LED_PIN, HIGH); // LED merah
digitalWrite(BUZZER_PIN, HIGH); // Buzzer berbunyi
} else {
digitalWrite(LED_PIN, LOW); // LED mati
digitalWrite(BUZZER_PIN, LOW); // Buzzer mati
}
// Menangani input dari keypad
char key = keypad.getKey();
if (key) {
if (key == 'A') {
batasBawah += 5;
} else if (key == 'B') {
batasAtas += 5;
}
}
delay(100); // Waktu tunggu untuk pembacaan sensor
}