Arm Mbed OS support forum

Convert arduino code to mbed

/*

// 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
}

void loop() {
// generate 10-microsecond pulse to TRIG pin
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);

// measure duration of pulse from ECHO pin
duration_us = pulseIn(ECHO_PIN, HIGH);
// calculate the distance
distance_cm = 0.017 * duration_us;

if(distance_cm < DISTANCE_THRESHOLD)
digitalWrite(BUZZER_PIN, HIGH); // turn on Piezo Buzzer
else
digitalWrite(BUZZER_PIN, LOW); // turn off Piezo Buzzer

// print the value to Serial Monitor
Serial.print(“distance: “);
Serial.print(distance_cm);
Serial.println(” cm”);

delay(500);
}

Hello,

  • how is your question related to a bug in MbedOS?
  • code formating with these 3 symbols ``` before and after your code
  • this is community forum around Mbed and not an online automatic code convertor
  • You also not provide any information but you probably need this - HC-SR04
  • Here you can start Arm Mbed OS quick start

If you will facing a problem let us know with information about where you stuck.

BR, Jan

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

if ( distance < 200 ) {
	for (float i=0; i<26; i=i+5) {
        buzzer.period(1.0/969.0);
        buzzer = i/50.0f;
        wait(5);
        }
    }
else if(distance >= 300){
    buzzer.period(1.0/969.0);
    buzzer=0.0; // turn off audio
    buzzer = 0;
    
}

}

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

if ( distance < 200 ) {
	for (float i=0; i<26; i=i+5) {
        buzzer.period(1.0/969.0);
        buzzer = i/50.0f;
        wait(5);
        }
    }
else if(distance >= 300){
    buzzer.period(1.0/969.0);
    buzzer=0.0; // turn off audio
    buzzer = 0;
    
}

}

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.  
 
}

}

Hello again,

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.  
	}
}

BR, Jan