#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Pin Definitions
const int ldrPin = A0; // LDR (Photoresistor) connected to A0
const int buzzerPin = 9; // Buzzer connected to digital pin 9
const int heartRateThreshold = 50; // Heart rate threshold for the alert
// Variables
int heartRate = 0; // Variable to store calculated heart rate
int ldrValue = 0; // Raw LDR value from analog input
// Initialize LCD (20x4) with I2C address
LiquidCrystal_I2C lcd(0x3F, 20, 4); // Replace 0x3F with 0x27 if needed
void setup() {
// Initialize Serial Monitor (for debugging)
Serial.begin(9600);
// Initialize Buzzer
pinMode(buzzerPin, OUTPUT);
digitalWrite(buzzerPin, LOW); // Ensure buzzer is off initially
// Initialize LCD
lcd.begin(20, 4);
lcd.backlight(); // Turn on LCD backlight
lcd.setCursor(0, 0);
lcd.print("Smartwatch Project");
delay(2000);
lcd.clear();
// Display initial message
lcd.setCursor(0, 0);
lcd.print("Heart Rate Monitor");
lcd.setCursor(0, 1);
lcd.print("Initializing...");
delay(2000);
lcd.clear();
}
void loop() {
// Read the value from the LDR
ldrValue = analogRead(ldrPin);
// Simulate heart rate using the LDR value
heartRate = calculateHeartRate(ldrValue);
// Display heart rate on the LCD
lcd.setCursor(0, 0);
lcd.print("Heart Rate: ");
lcd.print(heartRate);
lcd.print(" BPM"); // Beats Per Minute
// Check if the heart rate is below the threshold
if (heartRate < heartRateThreshold) {
lcd.setCursor(0, 1);
lcd.print("WARNING: Low HR! ");
activateBuzzer(); // Alert the user
} else {
lcd.setCursor(0, 1);
lcd.print("HR is Normal ");
deactivateBuzzer(); // Ensure the buzzer is off
}
// Debugging output in Serial Monitor
Serial.print("LDR Value: ");
Serial.print(ldrValue);
Serial.print(" | Heart Rate: ");
Serial.println(heartRate);
delay(1000); // Wait 1 second before the next reading
}
// Function to simulate heart rate based on LDR value
int calculateHeartRate(int ldrValue) {
// Map the LDR value (0-1023) to a heart rate range (60-120 BPM)
return map(ldrValue, 0, 1023, 60, 120);
}
// Function to activate the buzzer
void activateBuzzer() {
digitalWrite(buzzerPin, HIGH); // Turn on the buzzer
}
// Function to deactivate the buzzer
void deactivateBuzzer() {
digitalWrite(buzzerPin, LOW); // Turn off the buzzer
}