добрый день.изввините за делетанство.есть код необходимо только назначить пин для зумера.где это не знаю
буду рад любой помощиэ
/*
driver for the HCSR04 ultrasonic sensor, including a buzzer that blips at increasing frequency as the distance decreases
sensor is on D12 and D13
*/
#include <HCSR04.h>
#define NEAR_DISTANCE 0.2 // near point (cm) at which the sensor becomes unreliable
#define FAR_DISTANCE 400.0 // far point (cm) at which the sensor becomes unreliable
#define NEAR_PIP_DELAY 200 // inter-pip period (mS) at NEAR_DISTANCE
#define FAR_PIP_DELAY 2000 // inter-pip period (mS) at FAR_DISTANCE
#define PIP_TIME 20 // pip period (mS)
#define LOOP_DELAY 250 // delay in the loop (mS)
#define BUFFSIZE 10 // number of readings used to arrive at a moving average
float buffer[BUFFSIZE];
byte i = 0; // index of the next free element in buffer
float d, av, sum;
UltraSonicDistanceSensor distanceSensor(12, 13); // Initialize sensor useing digital pins 12 and 13
unsigned long last_pip_time;
unsigned long inter_pip_delay;
void setup () {
Serial.begin(9600); // We initialize serial connection so that we could print values from sensor.
for (byte j=0; j<BUFFSIZE; j++)
buffer[j] = 0.0;
last_pip_time = millis();
}
void loop () {
buffer[i] = distanceSensor.measureDistanceCm(); // read the sensor and buffer the result (in cm)
Serial.println(buffer[i]);
i = (i++)%BUFFSIZE;
// get the average of the readings in the buffer
sum = 0.0;
for (byte j=0; j<BUFFSIZE; j++)
sum += buffer[j];
av = sum/BUFFSIZE;
// work out a new inter-pip delay based on the distance measured
if(av > FAR_DISTANCE)
inter_pip_delay = FAR_PIP_DELAY;
else
if(av < NEAR_DISTANCE)
inter_pip_delay = NEAR_PIP_DELAY;
else
inter_pip_delay = (int)(((FAR_DISTANCE - NEAR_DISTANCE)/av)/(FAR_PIP_DELAY - NEAR_PIP_DELAY));
// see if it is time for a pip, bvased on the current distance
if((millis() - last_pip_time) > (last_pip_time + inter_pip_delay))
{
// time for a pip
// sound the pip for (PIP_TIME) mS
last_pip_time = millis();
}
delay(LOOP_DELAY);
}
class HCSR04
{
public:
HCSR04(int out, int echo); //initialisation class HCSR04 (trig pin , echo pin)
HCSR04(int out, int echo[], int n); //initialisation class HCSR04 (trig pin , echo pin)
~HCSR04(); //destructor
float dist() const; //return curent distance of element 0
float dist(int n) const; //return curent distance of element n
/*
driver for the HCSR04 ultrasonic sensor, including a buzzer that blips at increasing frequency as the distance decreases
sensor is on D12 and D13
*/
#include <HCSR04.h>
#define NEAR_DISTANCE 0.2 // near point (cm) at which the sensor becomes unreliable
#define FAR_DISTANCE 400.0 // far point (cm) at which the sensor becomes unreliable
#define NEAR_PIP_DELAY 200 // inter-pip period (mS) at NEAR_DISTANCE
#define FAR_PIP_DELAY 2000 // inter-pip period (mS) at FAR_DISTANCE
#define PIP_TIME 20 // pip period (mS)
#define LOOP_DELAY 250 // delay in the loop (mS)
#define BUFFSIZE 10 // number of readings used to arrive at a moving average
#define BUZZER_PIN D4
float buffer[BUFFSIZE];
byte i = 0; // index of the next free element in buffer
float d, av, sum;
/*HCSR04*/UltraSonicDistanceSensor distanceSensor(D1, D2); // Initialize sensor useing digital pins 12 and 13
unsigned long last_pip_time;
unsigned long inter_pip_delay;
void setup() {
Serial.begin(9600); // We initialize serial connection so that we could print values from sensor.
for (byte j = 0; j < BUFFSIZE; j++)
buffer[j] = 0.0;
last_pip_time = millis();
pinMode(BUZZER_PIN, OUTPUT);
}
void loop() {
buffer[i] = distanceSensor.measureDistanceCm(); // read the sensor and buffer the result (in cm)
Serial.println(buffer[i]);
i = (i++) % BUFFSIZE;
// get the average of the readings in the buffer
sum = 0.0;
for (byte j = 0; j < BUFFSIZE; j++)
sum += buffer[j];
av = sum / BUFFSIZE;
// work out a new inter-pip delay based on the distance measured
if (av > FAR_DISTANCE)
inter_pip_delay = FAR_PIP_DELAY;
else if (av < NEAR_DISTANCE)
inter_pip_delay = NEAR_PIP_DELAY;
else
inter_pip_delay = (int)(((FAR_DISTANCE - NEAR_DISTANCE) / av) / (FAR_PIP_DELAY - NEAR_PIP_DELAY));
// see if it is time for a pip, bvased on the current distance
if ((millis() - last_pip_time) > (last_pip_time + inter_pip_delay)) {
// time for a pip
// sound the pip for (PIP_TIME) mS
tone( BUZZER_PIN,1000,inter_pip_delay);
last_pip_time = millis();
}
delay(LOOP_DELAY);
}
C:\Users\chkanos\Documents\Arduino\HCSR04_driver\HCSR04_driver.ino:23:51: error: ‘D1’ was not declared in this scope; did you mean ‘y1’?
23 | /HCSR04/UltraSonicDistanceSensor distanceSensor(D1, D2); // Initialize sensor useing digital pins 12 and 13
| ^~
| y1
C:\Users\chkanos\Documents\Arduino\HCSR04_driver\HCSR04_driver.ino:23:55: error: ‘D2’ was not declared in this scope
23 | /HCSR04/UltraSonicDistanceSensor distanceSensor(D1, D2); // Initialize sensor useing digital pins 12 and 13
| ^~
C:\Users\chkanos\Documents\Arduino\HCSR04_driver\HCSR04_driver.ino: In function ‘void setup()’:
C:\Users\chkanos\Documents\Arduino\HCSR04_driver\HCSR04_driver.ino:16:20: error: ‘D14’ was not declared in this scope
16 | #define BUZZER_PIN D14
| ^~~
C:\Users\chkanos\Documents\Arduino\HCSR04_driver\HCSR04_driver.ino:33:11: note: in expansion of macro ‘BUZZER_PIN’
33 | pinMode(BUZZER_PIN, OUTPUT);
| ^~~~~~~~~~
C:\Users\chkanos\Documents\Arduino\HCSR04_driver\HCSR04_driver.ino: In function ‘void loop()’:
C:\Users\chkanos\Documents\Arduino\HCSR04_driver\HCSR04_driver.ino:16:20: error: ‘D14’ was not declared in this scope
16 | #define BUZZER_PIN D14
| ^~~
C:\Users\chkanos\Documents\Arduino\HCSR04_driver\HCSR04_driver.ino:60:11: note: in expansion of macro ‘BUZZER_PIN’
60 | tone( BUZZER_PIN,1000,inter_pip_delay);
| ^~~~~~~~~~
Несколько библиотек найдено для “HCSR04.h”
Используется: C:\Users\chkanos\Documents\Arduino\libraries\HCSR04
Не используется: C:\Users\chkanos\Documents\Arduino\libraries\HCSR04_ultrasonic_sensor
Не используется: C:\Users\chkanos\Documents\Arduino\libraries\Simple_HC-SR04_Control
Не используется: C:\Users\chkanos\Documents\Arduino\libraries\HC-SR04
exit status 1
Compilation error: ‘D1’ was not declared in this scope; did you mean ‘y1’?
Это важно. Заказчик было истерику устроил - подключил активный вместо пьезо (по условию) и говорит что хрипит. А затем включил дурку, типа, я не копенгаген. Повнимательнее.
спасибо важное замечание.я связался с автором и он переделал код зумеры есть и активные и пассивные.обновленный так же не работает издает несколько коротких сигналов и все при этом в маниторе порта растояние показывает правильнбуду признателен сообществу высказать свое компитентное мнение и конкретные ошибки указать.
#include <HCSR04.h>
/*
driver for the HCSR04 ultrasonic sensor, including a buzzer that blips at increasing frequency as the distance decreases
sensor is on D12 and D13
*/
#include <HCSR04.h>
#define NEAR_DISTANCE 1.0 // near point (cm) at which the sensor becomes unreliable #define FAR_DISTANCE 400.0 // far point (cm) at which the sensor becomes unreliable #define NEAR_PIP_DELAY 500 // inter-pip period (mS) at NEAR_DISTANCE #define FAR_PIP_DELAY 2000 // inter-pip period (mS) at FAR_DISTANCE #define PIP_TIME 50 // pip period (mS) #define PIEZO_PIN 14 #define TONE_HZ 700
#define LOOP_DELAY 250 // delay in the loop (mS) #define BUFFSIZE 10 // number of readings used to arrive at a moving average
float buffer[BUFFSIZE];
byte i = 0; // index of the next free element in buffer
float d, av, sum;
UltraSonicDistanceSensor distanceSensor(12, 13); // Initialize sensor useing digital pins 13 and 12
unsigned long last_pip_time;
unsigned long inter_pip_delay;
void setup () {
Serial.begin(9600); // We initialize serial connection so that we could print values from sensor.
for (byte j=0; j<BUFFSIZE; j++)
buffer[j] = 0.0;
last_pip_time = millis();
}
void loop () {
buffer[i] = distanceSensor.measureDistanceCm(); // read the sensor and buffer the result (in cm)
i = (i++)%BUFFSIZE;
// get the average of the readings in the buffer
sum = 0.0;
for (byte j=0; j<BUFFSIZE; j++)
sum += buffer[j];
av = sum/BUFFSIZE;
Serial.println(av);
// work out a new inter-pip delay based on the distance measured
if(av > FAR_DISTANCE)
inter_pip_delay = FAR_PIP_DELAY;
else
if(av < NEAR_DISTANCE)
inter_pip_delay = NEAR_PIP_DELAY;
else
inter_pip_delay = (int)(((((FAR_DISTANCE - NEAR_DISTANCE)*1000)/av)/(FAR_PIP_DELAY - NEAR_PIP_DELAY))*100);
// see if it is time for a pip, based on the current distance
if((millis() - last_pip_time) > (last_pip_time + inter_pip_delay))
{
Serial.print("inter pip delay "); Serial.println((int)inter_pip_delay);
// time for a pip
// sound the pip for (PIP_TIME) mS
tone(PIEZO_PIN, TONE_HZ, PIP_TIME);
last_pip_time = millis();
}
delay(LOOP_DELAY);
Моё “компитентное” мнение: судя по первому посту, код Вы вставлять умеете, а значит, мы имеем дело не с ошибкой а с осознанным, циничным издевательством над сообществом.