// constants won’t change
const int TRIG_PIN = 6; // Arduino pin connected to Ultrasonic Sensor’s TRIG pin
const int ECHO_PIN = 7; // Arduino pin connected to Ultrasonic Sensor’s ECHO pin
const int BUZZER_PIN = 3; // Arduino pin connected to Piezo Buzzer’s pin
const int DISTANCE_THRESHOLD = 50; // centimeters
// variables will change:
float duration_us, distance_cm;
void setup() {
Serial.begin (9600); // initialize serial port
pinMode(TRIG_PIN, OUTPUT); // set arduino pin to output mode
pinMode(ECHO_PIN, INPUT); // set arduino pin to input mode
pinMode(BUZZER_PIN, OUTPUT); // set arduino pin to output mode
}
ultrasonic mu(D12, D11, .1, 1, &dist); //Set the trigger pin to p8 and the echo pin to p9
//have updates every .1 seconds and a timeout after 1
//second, and call dist when the distance changes
int main()
{
PwmOut buzzer(D5);
mu.startUpdates();//start mesuring the distance
while(1)
{
buzzer.period(1.0/969.0);
buzzer=0.5;
wait(1);
buzzer=0.0; // turn off audio
wait(1);
mu.checkDistance(); //call checkDistance() as much as possible, as this is where
//the class checks if dist needs to be called.
}
I want to know how to code when I want my buzzer to turn on when the distance is less than 200 mm and buzzer to turn off when distance is greater than 300 mm #include “mbed.h” #include “ultrasonic.h”
void dist(int distance)
{
PwmOut buzzer(D5);
//put code here to happen when the distance is changed
printf(“Distance changed to %dmm\r\n”, distance);
ultrasonic mu(D12, D11, .1, 1, &dist); //Set the trigger pin to p8 and the echo pin to p9
//have updates every .1 seconds and a timeout after 1
//second, and call dist when the distance changes
int main()
{
PwmOut buzzer(D5);
mu.startUpdates();//start mesuring the distance
while(1)
{
buzzer.period(1.0/969.0);
buzzer=0.5;
wait(1);
buzzer=0.0; // turn off audio
wait(1);
mu.checkDistance(); //call checkDistance() as much as possible, as this is where
//the class checks if dist needs to be called.
}
how I wrote above, use these 3 symbols ``` before and after your code for correct code formating in your post.
However, your code seems to be functional, I made only small correction.
under 200mm it plays yours tones
between 200-300mm it plays last tone
above 300mm it plays nothing
#include "mbed.h"
#include "ultrasonic.h"
PwmOut buzzer(D5);
void dist(int distance)
{
//put code here to happen when the distance is changed
printf("Distance changed to %dmm\r\n", distance);
if ( distance < 200 ) {
for (float i=1; i<26; i=i+5) {
buzzer.period(1.0/969.0);
buzzer = i/50.0f;
//wait(5);
wait(0.2);
}
}
else if(distance >= 300){
buzzer.period(1.0/969.0);
buzzer = 0.0; // turn off audio
}
}
ultrasonic mu(D12, D11, .1, 1, &dist); //Set the trigger pin to p8 and the echo pin to p9
//have updates every .1 seconds and a timeout after 1
//second, and call dist when the distance changes
int main()
{
mu.startUpdates();//start mesuring the distance
while(1)
{
/*buzzer.period(1.0/969.0);
buzzer=0.5;
wait(1);
buzzer=0.0; // turn off audio */
wait(0.5);
mu.checkDistance(); //call checkDistance() as much as possible, as this is where
//the class checks if dist needs to be called.
}
}