Esp 32 sensor bmp280 + buzzer

ttnnttnn
Posts: 6
Joined: Thu Nov 23, 2023 12:00 pm

Esp 32 sensor bmp280 + buzzer

Postby ttnnttnn » Tue Nov 28, 2023 5:45 am

Good morning everyone, I'm writing code for a variometer, and I can't make it so that as the meters per second relative to the climb rate increases, the buzzer emits a sound with increasing frequency (the more the m/s increase, the more the beeps increase) I hope I explained myself.
Everything works in my breadboard, but I can't give the correct beep sequence, with Arduino it's much easier with the "tone" function

At the moment I don't know how to do it with the code I attach below.
Please can you help me.
Thank you


Code: Select all

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>

#define BMP_SDA 21
#define BMP_SCL 22
#define BUZZER_PIN 19

Adafruit_BMP280 bmp;

void setup() {
  Serial.begin(115200);
  if (!bmp.begin()) {
    Serial.println(F("Could not find a valid BMP280 sensor, check wiring!"));
    while (1);
  }
  pinMode(BUZZER_PIN, OUTPUT);
}

void loop() {
  static unsigned long lastMillis = 0;
  static float lastAltitude = bmp.readAltitude();

  unsigned long currentMillis = millis();

  if (currentMillis - lastMillis >= 1000) {  // Leggi la quota ogni secondo
    float currentAltitude = bmp.readAltitude();
    float verticalSpeed = (currentAltitude - lastAltitude) / ((currentMillis - lastMillis) / 1000.0);

    { if (verticalSpeed > 0.2) {
      int newFrequency = map(verticalSpeed, 0, maxVerticalSpeed, 200, 500);
      tone(BUZZER_PIN, NOTE_B4, 100, BUZZER_CHANNEL); // Suona il buzzer con la nuova frequenza
        } else {
      noTone(BUZZER_PIN); // Spegni il buzzer se la velocità è zero o negativa
      }

    
    
    lastAltitude = currentAltitude;
    lastMillis = currentMillis;
  }
}
}

Who is online

Users browsing this forum: No registered users and 266 guests