![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Proejct2
Diff: main.cpp
- Revision:
- 10:781cd74506e9
- Parent:
- 9:73600707c93b
--- a/main.cpp Mon Mar 02 17:36:12 2020 +0000 +++ b/main.cpp Mon May 04 14:02:44 2020 +0000 @@ -1,34 +1,188 @@ #include "mbed.h" -#include "hcsr04.h" +#include <stdio.h> +#include <string.h> +#include "Servo.h" + Serial pc(USBTX,USBRX); -//Serial UART(USBTX,USBRX); -HCSR04 usensor(D8,D12); + //Bluetooth module declaration -Serial HC06(PTC17,PTC16); //BT TX RX -char snd[512],rcv[1000]; -unsigned int dist; +Serial blue(PTC17, PTC16); + +//Led declaration +DigitalOut Redl(LED1); +DigitalOut Bluel(LED3); +DigitalOut Greenl(LED2); + +Servo myservo(D3); + +char k; char a; - -Timer dt; +int c =0; +char ser = 'L'; int main() { pc.baud(9600); - unsigned char rx; - dt.start(); - while(1) { - usensor.start(); - wait_ms(500); - pc.printf("started"); - rx = HC06.getc(); - pc.printf("\n\r %ld",rx); - dist=usensor.get_dist_cm(); - dt.reset(); - if(dist>1 and dist<=30) - { - pc.printf("case: 1\r\n"); - pc.printf("\n\r %ld",dist ); - } - } -} \ No newline at end of file + Greenl = 1; //FRDM LED initially OFF + Bluel = 1; //FRDM LED initially OFF + Redl = 0; //FRDM LED initially OFF + k = 'T'; + pc.printf("\n\r Door locked"); + pc.printf("\n\r Lights off"); + + while(1) { + + myservo.write(0.5); //motor stays off + + if(blue.readable()> 0) + { + c = blue.getc(); + pc.printf("\n\rbluetooth"); + pc.printf("\n\r %ld",c); + k = 'T'; + } + + if (k == 'T') + { + if (c == 1) + { + pc.printf("\n\rDoor unlocked"); + pc.printf("\n\rKitchen light switched on"); + pc.printf("\n\rLiving room light switched on"); + Greenl = 0; + Bluel = 0; + if (ser == 'L') + { + myservo.write(0.6); + wait(0.3); + myservo.write(0.5); + ser = 'U'; + } + Redl = 1; + k = 'F'; + } + else if (c == 2) + { + pc.printf("\n\rDoor locked"); + pc.printf("\n\rKitchen light switched on"); + pc.printf("\n\rLiving room light switched on"); + Greenl = 0; + Bluel = 0; + if (ser == 'U') + { + myservo.write(0.4); + wait(0.3); + myservo.write(0.5); + ser = 'L'; + } + Redl = 1; + k = 'F'; + } + else if (c == 3) + { + pc.printf("\n\rDoor unlocked"); + pc.printf("\n\rKitchen light switched on"); + pc.printf("\n\rLiving room light switched off"); + Greenl = 1; + Bluel = 0; + pc.printf("\n\r %ld",ser); + if (ser == 'L') + { + myservo.write(0.6); + wait(0.3); + myservo.write(0.5); + ser = 'U'; + } + Redl = 1; + k = 'F'; + } + else if (c == 4) + { + pc.printf("\n\rDoor unlocked"); + pc.printf("\n\rKitchen light switched off"); + pc.printf("\n\rLiving room light switched on"); + Greenl = 0; + Bluel = 1; + if (ser == 'L') + { + myservo.write(0.6); + wait(0.3); + myservo.write(0.5); + ser = 'U'; + } + Redl = 1; + k = 'F'; + } + else if (c == 5) + { + pc.printf("\n\rDoor locked"); + pc.printf("\n\rKitchen light switched on"); + pc.printf("\n\rLiving room light switched off"); + Greenl = 1; + Bluel = 0; + if (ser == 'U') + { + myservo.write(0.4); + wait(0.3); + myservo.write(0.5); + ser = 'L'; + } + Redl = 1; + k = 'F'; + } + else if (c == 6) + { + pc.printf("\n\rDoor unlocked"); + pc.printf("\n\rKitchen light switched off"); + pc.printf("\n\rLiving room light switched off"); + Greenl = 1; + Bluel = 1; + if (ser == 'U') + { + myservo.write(0.4); + wait(0.3); + myservo.write(0.5); + ser = 'L'; + } + Redl = 1; + k = 'F'; + } + else if (c == 7) + { + pc.printf("\n\rDoor locked"); + pc.printf("\n\rKitchen light switched off"); + pc.printf("\n\rLiving room light switched on"); + Greenl = 0; + Bluel = 1; + if (ser == 'U') + { + myservo.write(0.4); + wait(0.3); + myservo.write(0.5); + ser = 'L'; + } + Redl = 1; + k = 'F'; + } + else if (c == 8) + { + pc.printf("\n\rDoor locked"); + pc.printf("\n\rKitchen light switched off"); + pc.printf("\n\rLiving room light switched off"); + Greenl = 1; + Bluel = 1; + if (ser == 'U') + { + myservo.write(0.4); + wait(0.3); + myservo.write(0.5); + ser = 'L'; + } + Redl = 1; + k = 'F'; + } + } + +} +} \ No newline at end of file