9 years, 4 months ago.

How to write a function with mbed in C

Hello, I wrote you because I will wish to write a function to my program but I do not know how. For example if I want to do sommen 2 numbers a and b , but using a function. do you know how to proceed ? Thank you for your answers

yes I have also followed a tutorial , but apparently the way to do that with mbed is different from that C

posted by AGON Juppé 19 May 2015

It is not, it is identical. Mbed = C++. Which supports all normal C functions also. (If you name a file .c it will be really compiled as C, but then you cannot use mbed library functions).

posted by Erik - 19 May 2015

Perhaps you can post a short example of what you have tried so that those offering help can offer alterations.

posted by Stephen Paulger 19 May 2015

Well now I'll Explain . I am writing in June " fonction_dwell " function that mite afford to Calculate stay according to an alpha ratio ( stuff pour saver lines of code ) and Make a function call in my main function. Here's how I wrote my fonction_dwell .

#include "mbed.h"
#include "TextLCD.h"

InterruptIn in(p10);
Timer t;
Timer t_on;
Timer t_off;
TextLCD lcd(p15, p16, p17, p18, p19, p20); // rs, e, d4-d7

// la fonction qui va retourner le dwell

int fonction_dwell(alpha);
{
    int d;
    d=alpha*100;
    return d;
    }
int main()
{
    float alpha;
    int calcul;
    int dwell;
    if (in==1) {  // <--- the input that you have created not the pin number.
    t_on.start();
    t_off.stop();
  } else {
    t_on.stop();
    t_off.start();
    alpha=t_on/(t_on+t_off);
    calcul=fonction_dwell(alpha);
  }  
    
     while (1) {
       
        lcd.printf("\n %d dwell\r\n",dwell);    
}
    
    }



posted by AGON Juppé 19 May 2015

but it does not work

posted by AGON Juppé 19 May 2015

Realise that you are printf 'dwell' variable (may you want to show 'calcul' variable?). However:

1 - Needless ';' of the function: int fonction_dwell(alpha)

2 - You need to specify the type of variable you pass into the function (int fonction_dwell(float alpha))

code changes

#include "mbed.h"
#include "TextLCD.h"
 
InterruptIn in(p10);
Timer t;
Timer t_on;
Timer t_off;
TextLCD lcd(p15, p16, p17, p18, p19, p20); // rs, e, d4-d7
 
// la fonction qui va retourner le dwell
 
int fonction_dwell(float alpha)
{
    int d;
    d=alpha*100;
    return d;
    }
int main()
{
    float alpha;
    int calcul;
    int dwell;
    if (in==1) {  // <--- the input that you have created not the pin number.
    t_on.start();
    t_off.stop();
  } else {
    t_on.stop();
    t_off.start();
    alpha=t_on/(t_on+t_off);
    calcul=fonction_dwell(alpha);
  }  
    
     while (1) {
       
        lcd.printf("\n %d calcul\r\n",calcul);    
}
    
    }
posted by Jose Rios 19 May 2015

You're right Completely .The power walking . I'll be able to create other functions and move forward. I wanted to ask you , how can I make the call to the function that I just created in my main program?

posted by AGON Juppé 19 May 2015

I wanted to ask you know how to make a function call ? This est'à'dire I want utilser a function that already ja create and put it in another program.

posted by AGON Juppé 19 May 2015

1 Answer

9 years, 4 months ago.

read the c++ tutorial, this is the easy way http://www.cplusplus.com/doc/tutorial/
if you really need to write a C function within a C++ main program as Mbed, you may use google "call c function within c++" or look at the Mbed source code, because most of the code is C
One answer is http://www.thegeekstuff.com/2013/01/mix-c-and-cpp/

I wanted to ask you know how to make a function call ? This est'à'dire I want utilser a function that already ja create and put it in another program.

posted by AGON Juppé 19 May 2015