BTSmartFan Servo/DC motor controlled using Bluetooth

Dependencies:   Servo mbed-src

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002  ******************************************************************************
00003  * @project  Smart BT Fan
00004  * @author  Justin Kim
00005  * @version V1.0.0
00006  * @date    29-JUN-2015
00007  * @brief   Main program body
00008 *******************************************************************************
00009 **/
00010 
00011 /* Includes ------------------------------------------------------------------*/
00012 #include "mbed.h"
00013 #include "Servo.h"
00014 
00015 /* Private typedef -----------------------------------------------------------*/
00016 /* Private define ------------------------------------------------------------*/
00017 /* Private variables ---------------------------------------------------------*/
00018 Serial pc(USBTX, USBRX);
00019 Serial bt(PA_14, PA_13);
00020 Servo myservo(D14);
00021 PwmOut Motor(D3);
00022 DigitalOut direction(D12);
00023 
00024 /* Private function prototypes -----------------------------------------------*/
00025 
00026 /* Private functions ---------------------------------------------------------*/
00027 /**
00028    * @brief     Main Function
00029    * @param  None
00030    * @retval    None
00031    */
00032 int main(void)
00033 {
00034     char ch;
00035     pc.baud(115200);
00036     bt.baud(115200);
00037     pc.printf("Hello World!\n\r");
00038     bt.printf("Hello World!\r\n");
00039     
00040     direction.write(1);
00041     Motor.period_ms(1);
00042     
00043     while(1)
00044     {
00045         if(bt.readable())
00046         {
00047             ch=bt.getc();
00048             pc.printf("%c",ch);
00049             bt.printf("%c",ch);
00050                                   
00051             if(ch == '*')
00052             {
00053                 myservo = 0.5;
00054             }
00055             else if(ch == '+')
00056             {
00057                 myservo = myservo + 0.1;
00058             }
00059             else if(ch == '-')
00060             {
00061                 myservo = myservo - 0.1;
00062             }     
00063             else if(ch == '@')
00064             {
00065                 Motor.write(0.8);
00066             }
00067             else if(ch == '!')
00068             {
00069                 Motor.write(0);
00070             }
00071         }
00072         
00073         else if(pc.readable())
00074         {
00075             ch=pc.getc();
00076             bt.printf("%c",ch);
00077             pc.printf("%c",ch);            
00078                         
00079             if(ch == '*')
00080             {
00081                 myservo = 0.5;
00082             }
00083             else if(ch == '+')
00084             {
00085                 myservo = myservo + 0.1;
00086             }
00087             else if(ch == '-')
00088             {
00089                 myservo = myservo - 0.1;
00090             }
00091             else if(ch == '@')
00092             {
00093                 Motor.write(0.8);
00094             }
00095             else if(ch == '!')
00096             {
00097                 Motor.write(0);
00098             }
00099         }
00100     }
00101 }