Converting Arduino to Mbed code

Dear Sir/Madam

Please advise as to how I can convert the below Arduino code to MBED OS ,

const int RecordTime = 3 ; //Define Measuring Time (Seconds)

const int SensorPin = 3 ; //Define Interrupt Pin (2 or 3 @ Arduino Uno)

int InterruptCounter;

float WindSpeed;

void setup ()

{

Serial.begin(9600 );

}

void loop () {

meassure();

Serial.print("Wind Speed: ");

Serial.print(WindSpeed); //Speed in km/h

Serial.print(" km/h - ");

Serial.print(WindSpeed / 3.6 ); //Speed in m/s

Serial.println(" m/s");

}

void meassure () {

InterruptCounter = 0 ;

attachInterrupt(digitalPinToInterrupt(SensorPin), countup, RISING);

delay(1000 * RecordTime);

detachInterrupt(digitalPinToInterrupt(SensorPin));

WindSpeed = (float )InterruptCounter / (float )RecordTime * 2.4 ;

}

void countup () {

InterruptCounter++;

}

HI,
something like this (not tested):

#include "mbed.h"

InterruptIn sensor(PA_6);  //Change the pin to the pin you will use

int InterruptCounter=0;

const int RecordTime = 3 ; //Define Measuring Time (Seconds)
const int SensorPin = 3 ; //Define Interrupt Pin (2 or 3 @ Arduino Uno)
float WindSpeed;

void countup () {
    InterruptCounter++;
}

void meassure () {
   InterruptCounter=0;
    sensor.rise(&countup);
     ThisThread::sleep_for(1s); //wait for 1 sec  //---> change this to be like  delay(1000 * RecordTime);
   
    sensor.rise(NULL);
    WindSpeed = (float )InterruptCounter / (float )RecordTime * 2.4 ;
}

void loop () {
    meassure();
    printf("Wind Speed: ");
    printf(WindSpeed); //Speed in km/h
    printf(" km/h - ");
    printf(WindSpeed / 3.6 ); //Speed in m/s
    printf(" m/s\n");
}

int main(){
    
    while(1){
    loop();
    ThisThread::sleep_for(1s); //wait for 1 sec
    }
}

more examples InterruptIn - API references and tutorials | Mbed OS 6 Documentation

Hello

Please find below the code which modified accordingly but am getting an error on PrintF , please advise on reason for error .

#include “mbed.h”

InterruptIn Anemometer(PB_10);

int Anemometer_Interrupt=0;

const int RecordTime = 3 ;
float WindSpeed;

void countup () {
Anemometer_Interrupt++;
}

void meassure () {
Anemometer.rise(&countup);
ThisThread::sleep_for(3s);

Anemometer.rise(NULL);
WindSpeed = (float )Anemometer_Interrupt / (float )RecordTime * 2.4 ;

}

void loop () {
meassure();
printf("Wind Speed: “);
printf(WindSpeed); //Speed in km/h
printf(” km/h - ");
}

int main(){

while(1){
loop();
ThisThread::sleep_for(1s); //wait for 1 sec
}

}

Thanks

HI,
This should compile … I tested to compile.

#include "mbed.h"

InterruptIn Anemometer(PB_10);

int Anemometer_Interrupt=0;

const int RecordTime = 3 ;
float WindSpeed;

void countup ()
{
    Anemometer_Interrupt++;
}

void meassure ()
{
    Anemometer.rise(&countup);
    ThisThread::sleep_for(3s);

    Anemometer.rise(NULL);
    WindSpeed = (float )Anemometer_Interrupt / (float )RecordTime * 2.4 ;

}

void loop ()
{
    meassure();
    printf("Wind Speed: ");
    printf("%f",WindSpeed); //Speed in km/h
    printf(" km/h - ");
}

       int main()
{

    while(1) {
        loop();
        ThisThread::sleep_for(1s); //wait for 1 sec
    }

}
1 Like

Hello,

for printing floats you need to change settings in mbed_app.json

{

    "target_overrides": {
        "*": {
            "target.printf_lib": "std"
        }
    }
}
  • The variable Anemometer_Interrupt have to be cleared to zero at the start of the measure function for next iteration.
  • the variable Anemometer_Interrupt should be volatile

BR, Jan

1 Like

Hello

Thanks it did just have an issue with serial output result as per below showing %f ?

Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f km/h - Wind Speed: %f

Thanks for the assistance

Hello,

yeah and the reason is

BR, Jan