5 years, 11 months ago.

stm32L476 - Blinky Example

How can i modify the code to print "good bye" every time the LED turns off?

1 Answer

5 years, 10 months ago.

Hello Moe,

To print “goodbye” when the LED turns off, you can create an if statement within the while loop:

#include "mbed.h"
 
DigitalOut led1(LED1);
 
// main() runs in its own thread in the OS

int main() {
    while (true) {
       led1 = !led1;
      //new code
       if(!led1){
	     printf(“Goodbye!!\n\r”);
       }
       wait(0.5);
   }
}

This will allow the code to print when the led if off.

NOTE: For other boards, make sure you know whether they are active low or active high. If the LED is active low (LED is on when led1 = 0), change the if statement to be:

if(led1){
    printf(“Goodbye!!\n\r”);
}

Hope this helps!

-Karen, team Mbed

If this solved your question, please make sure to click the "Thanks" link below!