Hello! After connecting an Arduino Uno to an Arduino sensor kit, and then connecting a cardiac sensor, I wrote code to make the LED and buzzer work in two scenarios: when I'm exercising and my heart rate exceeds the normal range, and when I'm not exercising but my heart rate exceeds the normal range. However, the LED and buzzer are not functioning at all. I'm wondering how to fix the problem
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>
#include <PulseSensorPlayground.h>
Adafruit_ADXL345_Unified accelerometer = Adafruit_ADXL345_Unified(0x53);
PulseSensorPlayground pulseSensor;
const int ledPin = 9;
const int buzzerPin = 10;
const int restingThreshold = 60;
const int exerciseThreshold = 100;
int heartRate;
bool isExercising;
void setup() {
Wire.begin();
pinMode(0, INPUT);
pinMode(1, OUTPUT);
Serial.begin(9600);
if (!accelerometer.begin()) {
Serial.println("Failed to initialize accelerometer!");
while (1);
}
pulseSensor.analogInput(A0);
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
isExercising = false;
}
void loop() {
sensors_event_t event;
accelerometer.getEvent(&event);
heartRate = pulseSensor.getBeatsPerMinute();
if (heartRate > restingThreshold) {
if (isExercising) {
// μ΄λ μ€μΈ κ²½μ°
digitalWrite(ledPin, HIGH); // LED μλ
noTone(buzzerPin);
} else {
// μ΄λ μ€μ΄ μλ κ²½μ°
digitalWrite(ledPin, HIGH); // LED μλ
tone(buzzerPin, 1000); // λΆμ μλ
}
} else {
// μ¬λ°μκ° μ μ μμΉ μ΄νμΈ κ²½μ°
digitalWrite(ledPin, LOW); // LED λκΈ°
noTone(buzzerPin); // λΆμ λκΈ°
}
delay(100);
}