/*############################################################# Program Name : ex1_HC06_leds Author : Grant Phillips Date Modified : 12/05/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: "OrangeON" - ORANGE LED (LD3) ON "OrangeOFF" - ORANGE LED (LD3) OFF "RedON" - RED LED (LD5) ON "RedOFF" - RED LED (LD5) OFF "BlueON" - BLUE LED (LD6) ON "BlueOFF" - BLUE LED (LD6) OFF "GreenON" - GREEN LED (LD4) ON "GreenOFF" - 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[50]; //incoming message string int btmsg_counter = 0; //character position counter in the message string int new_btmsg = 0; //flag to indicate if a new complete message was received //this interrupt routine is called when a new character is received void btinterrupt() { char c; c = bt.getc(); //read the incoming character if(c == '\n') { //if it is the terminating character if(btmsg[btmsg_counter - 1] == '\r') btmsg[btmsg_counter - 1] = '\0'; else btmsg[btmsg_counter] = '\0'; new_btmsg = 1; //enable the new message flag to indicate a COMPLETE message was received btmsg_counter = 0; //clear the message string } else { btmsg[btmsg_counter] = c; //add the character to the message string btmsg_counter++; //move to the next character in the string } } 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(strcmp(btmsg, "OrangeON") == 0) //message for ORANGE LED (LD3) ON OrangeLed = 1; else if(strcmp(btmsg, "OrangeOFF") == 0) //message for ORANGE LED (LD3) OFF OrangeLed = 0; else if(strcmp(btmsg, "RedON") == 0) //RED LED (LD5) ON RedLed = 1; else if(strcmp(btmsg, "RedOFF") == 0) //RED LED (LD5) OFF RedLed = 0; else if(strcmp(btmsg, "BlueON") == 0) //BLUE LED (LD6) ON BlueLed = 1; else if(strcmp(btmsg, "BlueOFF") == 0) //BLUE LED (LD6) OFF BlueLed = 0; else if(strcmp(btmsg, "GreenON") == 0) //GREEN LED (LD4) ON GreenLed = 1; else if(strcmp(btmsg, "GreenOFF") == 0) //GREEN LED (LD4) OFF GreenLed = 0; else bt.printf("Incorrect command\n"); //Reply incorrect command new_btmsg=0; //clear message flag } } }