/*############################################################# Program Name : ex1_HC06_leds_b Author : Grant Phillips Date Modified : 17/09/2016 Compiler : ARMmbed Tested On : STM32F4-Discovery Description : Example program that uses the STM32F4's USART module to establish a serial link via a HC-06 Bluetooth module with another device. If the STM32F4 receives the following messages: "A" - ORANGE LED (LD3) ON "B" - ORANGE LED (LD3) OFF "C" - RED LED (LD5) ON "D" - RED LED (LD5) OFF "E" - BLUE LED (LD6) ON "F" - BLUE LED (LD6) OFF "G" - GREEN LED (LD4) ON "H" - GREEN LED (LD4) OFF Requirements : * STM32F4-Discovery Board Circuit : * 4 LEDs connected to PD15 - PD12 (LD6 - LD4 ON STM32F4-DISCOVERY BOARD) * HC06 Bluetooth module is connected as follows: TXD - PB7 RXD - PB6 GND - GND VCC - 3.3V ##############################################################*/ #include "mbed.h" DigitalOut GreenLed(PD_12); DigitalOut OrangeLed(PD_13); DigitalOut RedLed(PD_14); DigitalOut BlueLed(PD_15); Serial bt(PB_6, PB_7); //create a Serial object to connect to the HC06 Bluetooth module // tx , rx char btmsg; //character variable to store incoming bt message int new_btmsg = 0; //flag to indicate if a new .message was received //this interrupt routine is called when a new character is received void btinterrupt() { if(new_btmsg == 0) //make sure previous message was handled first before fetching next instruction message { btmsg = bt.getc(); //read the incoming character new_btmsg = 1; //set the flag to indicate that there is a new message } } int main() { bt.attach(&btinterrupt); //if a new character arrives, call the interrupt service routine while(1) { if(new_btmsg) { //check if there is a new message if(btmsg=='A') //message for ORANGE LED (LD3) ON OrangeLed = 1; else if(btmsg=='B') //message for ORANGE LED (LD3) OFF OrangeLed = 0; else if(btmsg=='C') //RED LED (LD5) ON RedLed = 1; else if(btmsg=='D') //RED LED (LD5) OFF RedLed = 0; else if(btmsg=='E') //BLUE LED (LD6) ON BlueLed = 1; else if(btmsg=='F') //BLUE LED (LD6) OFF BlueLed = 0; else if(btmsg=='G') //GREEN LED (LD4) ON GreenLed = 1; else if(btmsg=='H') //GREEN LED (LD4) OFF GreenLed = 0; else bt.printf("Incorrect command\n"); //Reply incorrect command new_btmsg=0; //clear message flag } } }