There is no response from the buzzer and LED. Please help me

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);
}

Welcome to the forum. It is well advised to read the forum guidelines to gain some insight about how best to get help.

Basically, we need a better understanding of your setup; a sketch of your wiring will help with that. Also, it's considered good form to

  • capture your code from the IDE using shift-ctrl-T
  • insert in your message using ctrl-V
  • and also, if relevant, to use the capture error tool the same way when advising us of errors you're seeing.

That allows us to visualize your code much more quickly, and also to transfer your code completely to our IDE software for further testing, etc.

1 Like

you did not configure
ledPin and buzzerPin as output

@StefanL38 see the code near end of setup(). Unusual place to hide it, but it's there.

what ever sets variable isexercising to true?
Since I see nothing doing so, we're down to the heart rate, so what is that value? If below threshold, then your description of the symptom is correct, nothing will happen.
I suggest you start diagnostics using the Serial Monitor, it should help you sort this out.

Not sure what you've done before this but I suggest you first start with the accelerometer example provided in the library. Prove the accelerometer is functioning properly.

Then go on to add you specific needs to the code.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.