We can handle lights in differents rooms and the air conditioner with our cellphone. We can also switch between two operation modes (manual and automatic). In automatic mode, garden lights and air conditioner will be activated when necessary.
Dependencies: mbed
main.cpp@0:bdebfb736149, 2014-08-06 (annotated)
- Committer:
- mdelloso
- Date:
- Wed Aug 06 15:56:10 2014 +0000
- Revision:
- 0:bdebfb736149
- Child:
- 1:21ff089ca074
Finished
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mdelloso | 0:bdebfb736149 | 1 | #include "mbed.h" |
mdelloso | 0:bdebfb736149 | 2 | |
mdelloso | 0:bdebfb736149 | 3 | Serial blue(D1,D0); //(TX,DX) |
mdelloso | 0:bdebfb736149 | 4 | Serial PC(USBTX, USBRX); // Used to debug |
mdelloso | 0:bdebfb736149 | 5 | Ticker timer; |
mdelloso | 0:bdebfb736149 | 6 | |
mdelloso | 0:bdebfb736149 | 7 | //Home lights |
mdelloso | 0:bdebfb736149 | 8 | DigitalOut ledBedroom(D9); |
mdelloso | 0:bdebfb736149 | 9 | DigitalOut ledLiving(D10); |
mdelloso | 0:bdebfb736149 | 10 | DigitalOut ledGarden(D11); |
mdelloso | 0:bdebfb736149 | 11 | |
mdelloso | 0:bdebfb736149 | 12 | // |
mdelloso | 0:bdebfb736149 | 13 | DigitalOut fan(D12); |
mdelloso | 0:bdebfb736149 | 14 | |
mdelloso | 0:bdebfb736149 | 15 | //Sensors |
mdelloso | 0:bdebfb736149 | 16 | AnalogIn temp(A0); |
mdelloso | 0:bdebfb736149 | 17 | AnalogIn light(A1); |
mdelloso | 0:bdebfb736149 | 18 | |
mdelloso | 0:bdebfb736149 | 19 | float tempRead,lightRead=0; |
mdelloso | 0:bdebfb736149 | 20 | int flagBlue,flagTimer=0; //Interrupts flags |
mdelloso | 0:bdebfb736149 | 21 | int manual=0; //to handle the ledGarden and the fan manually |
mdelloso | 0:bdebfb736149 | 22 | |
mdelloso | 0:bdebfb736149 | 23 | char c; |
mdelloso | 0:bdebfb736149 | 24 | |
mdelloso | 0:bdebfb736149 | 25 | void timerInterrupt() |
mdelloso | 0:bdebfb736149 | 26 | { |
mdelloso | 0:bdebfb736149 | 27 | flagTimer=1; |
mdelloso | 0:bdebfb736149 | 28 | } |
mdelloso | 0:bdebfb736149 | 29 | |
mdelloso | 0:bdebfb736149 | 30 | void blueInterrupt() |
mdelloso | 0:bdebfb736149 | 31 | { |
mdelloso | 0:bdebfb736149 | 32 | c = blue.getc(); //receives the command |
mdelloso | 0:bdebfb736149 | 33 | flagBlue=1; |
mdelloso | 0:bdebfb736149 | 34 | } |
mdelloso | 0:bdebfb736149 | 35 | |
mdelloso | 0:bdebfb736149 | 36 | int main() |
mdelloso | 0:bdebfb736149 | 37 | { |
mdelloso | 0:bdebfb736149 | 38 | //Bluetooth init |
mdelloso | 0:bdebfb736149 | 39 | blue.baud(9600); |
mdelloso | 0:bdebfb736149 | 40 | blue.attach(&blueInterrupt); |
mdelloso | 0:bdebfb736149 | 41 | //Timer init |
mdelloso | 0:bdebfb736149 | 42 | timer.attach(&timerInterrupt, 1.0); //time interval 1 sec |
mdelloso | 0:bdebfb736149 | 43 | while (1) { |
mdelloso | 0:bdebfb736149 | 44 | if (flagTimer) { |
mdelloso | 0:bdebfb736149 | 45 | tempRead = temp.read() * 333.333; //value in celsius |
mdelloso | 0:bdebfb736149 | 46 | lightRead=light.read(); //value between 0 - 1 |
mdelloso | 0:bdebfb736149 | 47 | if (manual==0) { //automatic mode |
mdelloso | 0:bdebfb736149 | 48 | if (lightRead<0.2) |
mdelloso | 0:bdebfb736149 | 49 | ledGarden=1; //The ledGarden turn on at night //VA S??? |
mdelloso | 0:bdebfb736149 | 50 | else |
mdelloso | 0:bdebfb736149 | 51 | ledGarden=0; //Se prende de dia |
mdelloso | 0:bdebfb736149 | 52 | if (tempRead>30) |
mdelloso | 0:bdebfb736149 | 53 | fan=1; //The fan turn on if it's hot |
mdelloso | 0:bdebfb736149 | 54 | else |
mdelloso | 0:bdebfb736149 | 55 | fan=0; |
mdelloso | 0:bdebfb736149 | 56 | } |
mdelloso | 0:bdebfb736149 | 57 | flagTimer=0; //clear flag |
mdelloso | 0:bdebfb736149 | 58 | } //End flagTimer |
mdelloso | 0:bdebfb736149 | 59 | |
mdelloso | 0:bdebfb736149 | 60 | if (flagBlue) { |
mdelloso | 0:bdebfb736149 | 61 | switch(c) { |
mdelloso | 0:bdebfb736149 | 62 | case 'b': //bedroom |
mdelloso | 0:bdebfb736149 | 63 | ledBedroom=!ledBedroom; |
mdelloso | 0:bdebfb736149 | 64 | break; |
mdelloso | 0:bdebfb736149 | 65 | case 'l': //living room |
mdelloso | 0:bdebfb736149 | 66 | ledLiving=!ledLiving; |
mdelloso | 0:bdebfb736149 | 67 | break; |
mdelloso | 0:bdebfb736149 | 68 | case 'g': //garden |
mdelloso | 0:bdebfb736149 | 69 | ledGarden = !ledGarden; |
mdelloso | 0:bdebfb736149 | 70 | break; |
mdelloso | 0:bdebfb736149 | 71 | case 'f': //fan |
mdelloso | 0:bdebfb736149 | 72 | fan=!fan; |
mdelloso | 0:bdebfb736149 | 73 | break; |
mdelloso | 0:bdebfb736149 | 74 | case 'm': //to activate manual/automatic mode |
mdelloso | 0:bdebfb736149 | 75 | if(manual) |
mdelloso | 0:bdebfb736149 | 76 | manual=0; //automatic mode |
mdelloso | 0:bdebfb736149 | 77 | else |
mdelloso | 0:bdebfb736149 | 78 | manual=1; //manual mode |
mdelloso | 0:bdebfb736149 | 79 | break; |
mdelloso | 0:bdebfb736149 | 80 | default: |
mdelloso | 0:bdebfb736149 | 81 | break; |
mdelloso | 0:bdebfb736149 | 82 | } |
mdelloso | 0:bdebfb736149 | 83 | flagBlue=0; |
mdelloso | 0:bdebfb736149 | 84 | } //End flagBlue |
mdelloso | 0:bdebfb736149 | 85 | |
mdelloso | 0:bdebfb736149 | 86 | __wfi(); //waiting for interrupt |
mdelloso | 0:bdebfb736149 | 87 | } |
mdelloso | 0:bdebfb736149 | 88 | } |