11 years, 6 months ago.

How to Combine a program module

I'm a student at the IRD Lab and I'm new in modular programming. can some body help me how to merge the 2 program: for example, After push the Button I would like to display in LCD some word..and I try to make this program:

#include "mbed.h"
#include "TextLCD.h"
DigitalIn button(p21);
DigitalOut myled(LED2);
Timer t;
TextLCD lcd(p15, p16, p17, p18, p19, p20); // rs, e, d4-d7
int i; 
void pushButton() {
   t.start();
        while (1) {
            if (button) {
              //tombol telah didukung menyimpan Temp
              t.reset();
              while (button)wait (0.9);
              i = t.read_ms()-300;
              // button
              t.reset();
              while(t.read_ms()<i){
                myled= 1 ;
                wait(0.9);
                myled = 0;
                wait(0.5);
         }
      }
    }
 }

void TextLCD() {
  lcd.printf("I have check its Ok!\n");
}
int main() {
pushButton();
TextLCD();
}

The compiler success but I try to running this program to MBED..IT doesnt work....can some body help me

regard,

Ayi Rahmat-IPB indonesia

2 Answers

11 years, 6 months ago.

If you press right mouse button on a program in your compiler, you can choose the publish option. Then you can share the link here so we can see the code you are using (you can also copy-paste the code here and use the <<code>> <</code>> tags around it, but publishing it is often handier). The file you uploaded now is the binary code which is used to program the mbed, we cannot see what is in it.

Accepted Answer

Oke Thanks, I'll try

posted by Jacques Grelet 19 Oct 2012

Could you tell me, Whats wrong?

posted by Jacques Grelet 19 Oct 2012
11 years, 6 months ago.

Now in your main file you first run pushButton, and next TextLCD. However pushButton has a while(1) loop: an infinite loop. Since it will never exit the pushButton function, it will never enter TextLCD. (Also if it is possible I think it is clearer to put the while(1) loop in the main function).

In this case you could do as simple example:

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

DigitalIn button(p21);
DigitalOut myled(LED2);
TextLCD lcd(p15, p16, p17, p18, p19, p20); // rs, e, d4-d7

void TextLCD() {
  lcd.printf("I have check its Ok!\n");
}
 
void pushButton() {
    bool active = false;
        while (1) {
            if (button) {
              
              //This is where you arrive when the button is pushed
              //Check our new variable 'active'
              if (active==false) {  //If it isn't active, write to the lcd display
                TextLCD();
                active = true;
              } else {              //Otherwise: clear the LCD
                lcd.cls();
                active = false;
              }
              wait(1);              //Wait 1 second before checking again
      }
    }
 }
 

int main() {
pushButton();
}

(btw it is a better idea to not give your function TextLCD the same name as your library).

In the end while it is very easy to program for the mbed, you cannot directly merge programs generally. Consider what you want the program to do, and what the program flow is (so in this case it would get stuck on the infinite while(1) loop).

its very cools..its work..thanks!

posted by Jacques Grelet 19 Oct 2012

In this program DigitalOut myled(LED2); is not active? how, if in the same time we push the pushbutton then the led 2 is active? (Sorry my english is not good!)

posted by Jacques Grelet 19 Oct 2012

The if(button) part gets activated when the button is pressed, so we can add below that line: myled = 1;

Problem is then that it will go on, but never go off. So in the example above after the '}' which is after wait(1); add:

else myled = 0;

posted by Erik - 19 Oct 2012