10 years ago.

LCD Screen keeps on displaying without any Pressing of Switch

Hi some help would be great. Ive been trying to make an external LED blink. While the external LED is activated with an always open switch, I would like to have the LCD to display "Green" & " Red". Everything seems to be working fine but one problem. The LCD keeps on displaying "Green" & " Red" even without me pressing the switch . I dont want the LCD to display anything without me pressing the switch. PLEASE HELPP!!! Here is the program.The LCD is on the mbed application board.

  1. include "mbed.h"
  2. include "C12832_lcd.h" C12832_LCD lcd; DigitalOut led1(LED1); DigitalOut led2(LED2); DigitalOut led3(LED3); DigitalOut led4(LED4); PwmOut led5(p21); PwmOut led6(p22);

int main() { while (1) {

lcd.cls(); led4 = 0; led1 = 1; wait(0.2); led1 = 0; led2 = 1; wait(0.2); led2 = 0; led3 = 1; wait(0.2); led3 = 0; led4 = 1; wait(0.2); led4 = 0; wait(0.2);

if ( led5 == 1 ) {

led5 =0; wait(0.2); lcd.locate(0, 0); lcd.printf("red"); led5 =1; wait(0.2);

} else if(led5==0) { lcd.cls(); led5=1; wait(0.2);

}

if ( led6 == 1 ) {

led6 = 0 ; wait(0.2); lcd.locate(0, 0); lcd.printf("green"); led6 =1 ; wait(0.2);

} else if(led6==0) { lcd.cls(); led6=1; wait(0.2);

} } }

Some help would be great and appreciated.

2 Answers

10 years ago.

First of all, please use <<code>> <</code>> to format your code (note : both markers need to be placed on a separate line with the code in between - see editing tips).

include "mbed.h"
include "C12832_lcd.h"
C12832_LCD lcd;
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);
PwmOut led5(p21);
PwmOut led6(p22);

int main()
{
    while (1) {

        lcd.cls();
        led4 = 0;
        led1 = 1;
        wait(0.2);
        led1 = 0;
        led2 = 1;
        wait(0.2);
        led2 = 0;
        led3 = 1;
        wait(0.2);
        led3 = 0;
        led4 = 1;
        wait(0.2);
        led4 = 0;
        wait(0.2);

        if ( led5 == 1 ) {

            led5 =0;
            wait(0.2);
            lcd.locate(0, 0);
            lcd.printf("red");
            led5 =1;
            wait(0.2);

        } else if(led5==0) {
            lcd.cls();
            led5=1;
            wait(0.2);

        }

        if ( led6 == 1 ) {

            led6 = 0 ;
            wait(0.2);
            lcd.locate(0, 0);
            lcd.printf("green");
            led6 =1 ;
            wait(0.2);

        } else if(led6==0) {
            lcd.cls();
            led6=1;
            wait(0.2);

        }
    }
}

Both if statements set the LED to 0 and back to 1, so the else if statements are never executed. You need to remove LED5 = 1 , LED6 = 1 and the second wait(0.2) from both if statements and you can even replace each else if with else.
You mentioned a switch you have to push but no input is declared (DigitalIn) and no switch state detection is present in the code.

Accepted Answer

Thank you , your reply is much appreciated.

posted by Programmer Bake 22 Apr 2014
10 years ago.

Using the <<code>> and <</code>> tags; one line above, and one line below your program, makes it much more legible. Hopefully, you can edit your question to add them.

Your comment suggests you want to monitor some external switch? But in looking at the code, you are testing the current state of led5, which is configured as a PWM output.

Are you trying to use led5 as both an input and an output? You might switch it to a DigitalInput pin instead, but without knowing your schematic, this is only a guess. Perhaps the same is true for led6?

Thank you , your reply is much appreciated.

posted by Programmer Bake 22 Apr 2014