![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Proejct2
main.cpp@10:781cd74506e9, 2020-05-04 (annotated)
- Committer:
- Jamesan
- Date:
- Mon May 04 14:02:44 2020 +0000
- Revision:
- 10:781cd74506e9
- Parent:
- 9:73600707c93b
Project2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andcor02 | 8:25138f7b9309 | 1 | #include "mbed.h" |
Jamesan | 10:781cd74506e9 | 2 | #include <stdio.h> |
Jamesan | 10:781cd74506e9 | 3 | #include <string.h> |
Jamesan | 10:781cd74506e9 | 4 | #include "Servo.h" |
Jamesan | 10:781cd74506e9 | 5 | |
andcor02 | 8:25138f7b9309 | 6 | |
Jamesan | 9:73600707c93b | 7 | Serial pc(USBTX,USBRX); |
Jamesan | 10:781cd74506e9 | 8 | |
Jamesan | 9:73600707c93b | 9 | //Bluetooth module declaration |
Jamesan | 10:781cd74506e9 | 10 | Serial blue(PTC17, PTC16); |
Jamesan | 10:781cd74506e9 | 11 | |
Jamesan | 10:781cd74506e9 | 12 | //Led declaration |
Jamesan | 10:781cd74506e9 | 13 | DigitalOut Redl(LED1); |
Jamesan | 10:781cd74506e9 | 14 | DigitalOut Bluel(LED3); |
Jamesan | 10:781cd74506e9 | 15 | DigitalOut Greenl(LED2); |
Jamesan | 10:781cd74506e9 | 16 | |
Jamesan | 10:781cd74506e9 | 17 | Servo myservo(D3); |
Jamesan | 10:781cd74506e9 | 18 | |
Jamesan | 10:781cd74506e9 | 19 | char k; |
Jamesan | 9:73600707c93b | 20 | char a; |
Jamesan | 10:781cd74506e9 | 21 | int c =0; |
Jamesan | 10:781cd74506e9 | 22 | char ser = 'L'; |
Jamesan | 9:73600707c93b | 23 | |
andcor02 | 8:25138f7b9309 | 24 | int main() |
andcor02 | 8:25138f7b9309 | 25 | { |
Jamesan | 9:73600707c93b | 26 | pc.baud(9600); |
Jamesan | 10:781cd74506e9 | 27 | Greenl = 1; //FRDM LED initially OFF |
Jamesan | 10:781cd74506e9 | 28 | Bluel = 1; //FRDM LED initially OFF |
Jamesan | 10:781cd74506e9 | 29 | Redl = 0; //FRDM LED initially OFF |
Jamesan | 10:781cd74506e9 | 30 | k = 'T'; |
Jamesan | 10:781cd74506e9 | 31 | pc.printf("\n\r Door locked"); |
Jamesan | 10:781cd74506e9 | 32 | pc.printf("\n\r Lights off"); |
Jamesan | 10:781cd74506e9 | 33 | |
Jamesan | 10:781cd74506e9 | 34 | while(1) { |
Jamesan | 10:781cd74506e9 | 35 | |
Jamesan | 10:781cd74506e9 | 36 | myservo.write(0.5); //motor stays off |
Jamesan | 10:781cd74506e9 | 37 | |
Jamesan | 10:781cd74506e9 | 38 | if(blue.readable()> 0) |
Jamesan | 10:781cd74506e9 | 39 | { |
Jamesan | 10:781cd74506e9 | 40 | c = blue.getc(); |
Jamesan | 10:781cd74506e9 | 41 | pc.printf("\n\rbluetooth"); |
Jamesan | 10:781cd74506e9 | 42 | pc.printf("\n\r %ld",c); |
Jamesan | 10:781cd74506e9 | 43 | k = 'T'; |
Jamesan | 10:781cd74506e9 | 44 | } |
Jamesan | 10:781cd74506e9 | 45 | |
Jamesan | 10:781cd74506e9 | 46 | if (k == 'T') |
Jamesan | 10:781cd74506e9 | 47 | { |
Jamesan | 10:781cd74506e9 | 48 | if (c == 1) |
Jamesan | 10:781cd74506e9 | 49 | { |
Jamesan | 10:781cd74506e9 | 50 | pc.printf("\n\rDoor unlocked"); |
Jamesan | 10:781cd74506e9 | 51 | pc.printf("\n\rKitchen light switched on"); |
Jamesan | 10:781cd74506e9 | 52 | pc.printf("\n\rLiving room light switched on"); |
Jamesan | 10:781cd74506e9 | 53 | Greenl = 0; |
Jamesan | 10:781cd74506e9 | 54 | Bluel = 0; |
Jamesan | 10:781cd74506e9 | 55 | if (ser == 'L') |
Jamesan | 10:781cd74506e9 | 56 | { |
Jamesan | 10:781cd74506e9 | 57 | myservo.write(0.6); |
Jamesan | 10:781cd74506e9 | 58 | wait(0.3); |
Jamesan | 10:781cd74506e9 | 59 | myservo.write(0.5); |
Jamesan | 10:781cd74506e9 | 60 | ser = 'U'; |
Jamesan | 10:781cd74506e9 | 61 | } |
Jamesan | 10:781cd74506e9 | 62 | Redl = 1; |
Jamesan | 10:781cd74506e9 | 63 | k = 'F'; |
Jamesan | 10:781cd74506e9 | 64 | } |
Jamesan | 10:781cd74506e9 | 65 | else if (c == 2) |
Jamesan | 10:781cd74506e9 | 66 | { |
Jamesan | 10:781cd74506e9 | 67 | pc.printf("\n\rDoor locked"); |
Jamesan | 10:781cd74506e9 | 68 | pc.printf("\n\rKitchen light switched on"); |
Jamesan | 10:781cd74506e9 | 69 | pc.printf("\n\rLiving room light switched on"); |
Jamesan | 10:781cd74506e9 | 70 | Greenl = 0; |
Jamesan | 10:781cd74506e9 | 71 | Bluel = 0; |
Jamesan | 10:781cd74506e9 | 72 | if (ser == 'U') |
Jamesan | 10:781cd74506e9 | 73 | { |
Jamesan | 10:781cd74506e9 | 74 | myservo.write(0.4); |
Jamesan | 10:781cd74506e9 | 75 | wait(0.3); |
Jamesan | 10:781cd74506e9 | 76 | myservo.write(0.5); |
Jamesan | 10:781cd74506e9 | 77 | ser = 'L'; |
Jamesan | 10:781cd74506e9 | 78 | } |
Jamesan | 10:781cd74506e9 | 79 | Redl = 1; |
Jamesan | 10:781cd74506e9 | 80 | k = 'F'; |
Jamesan | 10:781cd74506e9 | 81 | } |
Jamesan | 10:781cd74506e9 | 82 | else if (c == 3) |
Jamesan | 10:781cd74506e9 | 83 | { |
Jamesan | 10:781cd74506e9 | 84 | pc.printf("\n\rDoor unlocked"); |
Jamesan | 10:781cd74506e9 | 85 | pc.printf("\n\rKitchen light switched on"); |
Jamesan | 10:781cd74506e9 | 86 | pc.printf("\n\rLiving room light switched off"); |
Jamesan | 10:781cd74506e9 | 87 | Greenl = 1; |
Jamesan | 10:781cd74506e9 | 88 | Bluel = 0; |
Jamesan | 10:781cd74506e9 | 89 | pc.printf("\n\r %ld",ser); |
Jamesan | 10:781cd74506e9 | 90 | if (ser == 'L') |
Jamesan | 10:781cd74506e9 | 91 | { |
Jamesan | 10:781cd74506e9 | 92 | myservo.write(0.6); |
Jamesan | 10:781cd74506e9 | 93 | wait(0.3); |
Jamesan | 10:781cd74506e9 | 94 | myservo.write(0.5); |
Jamesan | 10:781cd74506e9 | 95 | ser = 'U'; |
Jamesan | 10:781cd74506e9 | 96 | } |
Jamesan | 10:781cd74506e9 | 97 | Redl = 1; |
Jamesan | 10:781cd74506e9 | 98 | k = 'F'; |
Jamesan | 10:781cd74506e9 | 99 | } |
Jamesan | 10:781cd74506e9 | 100 | else if (c == 4) |
Jamesan | 10:781cd74506e9 | 101 | { |
Jamesan | 10:781cd74506e9 | 102 | pc.printf("\n\rDoor unlocked"); |
Jamesan | 10:781cd74506e9 | 103 | pc.printf("\n\rKitchen light switched off"); |
Jamesan | 10:781cd74506e9 | 104 | pc.printf("\n\rLiving room light switched on"); |
Jamesan | 10:781cd74506e9 | 105 | Greenl = 0; |
Jamesan | 10:781cd74506e9 | 106 | Bluel = 1; |
Jamesan | 10:781cd74506e9 | 107 | if (ser == 'L') |
Jamesan | 10:781cd74506e9 | 108 | { |
Jamesan | 10:781cd74506e9 | 109 | myservo.write(0.6); |
Jamesan | 10:781cd74506e9 | 110 | wait(0.3); |
Jamesan | 10:781cd74506e9 | 111 | myservo.write(0.5); |
Jamesan | 10:781cd74506e9 | 112 | ser = 'U'; |
Jamesan | 10:781cd74506e9 | 113 | } |
Jamesan | 10:781cd74506e9 | 114 | Redl = 1; |
Jamesan | 10:781cd74506e9 | 115 | k = 'F'; |
Jamesan | 10:781cd74506e9 | 116 | } |
Jamesan | 10:781cd74506e9 | 117 | else if (c == 5) |
Jamesan | 10:781cd74506e9 | 118 | { |
Jamesan | 10:781cd74506e9 | 119 | pc.printf("\n\rDoor locked"); |
Jamesan | 10:781cd74506e9 | 120 | pc.printf("\n\rKitchen light switched on"); |
Jamesan | 10:781cd74506e9 | 121 | pc.printf("\n\rLiving room light switched off"); |
Jamesan | 10:781cd74506e9 | 122 | Greenl = 1; |
Jamesan | 10:781cd74506e9 | 123 | Bluel = 0; |
Jamesan | 10:781cd74506e9 | 124 | if (ser == 'U') |
Jamesan | 10:781cd74506e9 | 125 | { |
Jamesan | 10:781cd74506e9 | 126 | myservo.write(0.4); |
Jamesan | 10:781cd74506e9 | 127 | wait(0.3); |
Jamesan | 10:781cd74506e9 | 128 | myservo.write(0.5); |
Jamesan | 10:781cd74506e9 | 129 | ser = 'L'; |
Jamesan | 10:781cd74506e9 | 130 | } |
Jamesan | 10:781cd74506e9 | 131 | Redl = 1; |
Jamesan | 10:781cd74506e9 | 132 | k = 'F'; |
Jamesan | 10:781cd74506e9 | 133 | } |
Jamesan | 10:781cd74506e9 | 134 | else if (c == 6) |
Jamesan | 10:781cd74506e9 | 135 | { |
Jamesan | 10:781cd74506e9 | 136 | pc.printf("\n\rDoor unlocked"); |
Jamesan | 10:781cd74506e9 | 137 | pc.printf("\n\rKitchen light switched off"); |
Jamesan | 10:781cd74506e9 | 138 | pc.printf("\n\rLiving room light switched off"); |
Jamesan | 10:781cd74506e9 | 139 | Greenl = 1; |
Jamesan | 10:781cd74506e9 | 140 | Bluel = 1; |
Jamesan | 10:781cd74506e9 | 141 | if (ser == 'U') |
Jamesan | 10:781cd74506e9 | 142 | { |
Jamesan | 10:781cd74506e9 | 143 | myservo.write(0.4); |
Jamesan | 10:781cd74506e9 | 144 | wait(0.3); |
Jamesan | 10:781cd74506e9 | 145 | myservo.write(0.5); |
Jamesan | 10:781cd74506e9 | 146 | ser = 'L'; |
Jamesan | 10:781cd74506e9 | 147 | } |
Jamesan | 10:781cd74506e9 | 148 | Redl = 1; |
Jamesan | 10:781cd74506e9 | 149 | k = 'F'; |
Jamesan | 10:781cd74506e9 | 150 | } |
Jamesan | 10:781cd74506e9 | 151 | else if (c == 7) |
Jamesan | 10:781cd74506e9 | 152 | { |
Jamesan | 10:781cd74506e9 | 153 | pc.printf("\n\rDoor locked"); |
Jamesan | 10:781cd74506e9 | 154 | pc.printf("\n\rKitchen light switched off"); |
Jamesan | 10:781cd74506e9 | 155 | pc.printf("\n\rLiving room light switched on"); |
Jamesan | 10:781cd74506e9 | 156 | Greenl = 0; |
Jamesan | 10:781cd74506e9 | 157 | Bluel = 1; |
Jamesan | 10:781cd74506e9 | 158 | if (ser == 'U') |
Jamesan | 10:781cd74506e9 | 159 | { |
Jamesan | 10:781cd74506e9 | 160 | myservo.write(0.4); |
Jamesan | 10:781cd74506e9 | 161 | wait(0.3); |
Jamesan | 10:781cd74506e9 | 162 | myservo.write(0.5); |
Jamesan | 10:781cd74506e9 | 163 | ser = 'L'; |
Jamesan | 10:781cd74506e9 | 164 | } |
Jamesan | 10:781cd74506e9 | 165 | Redl = 1; |
Jamesan | 10:781cd74506e9 | 166 | k = 'F'; |
Jamesan | 10:781cd74506e9 | 167 | } |
Jamesan | 10:781cd74506e9 | 168 | else if (c == 8) |
Jamesan | 10:781cd74506e9 | 169 | { |
Jamesan | 10:781cd74506e9 | 170 | pc.printf("\n\rDoor locked"); |
Jamesan | 10:781cd74506e9 | 171 | pc.printf("\n\rKitchen light switched off"); |
Jamesan | 10:781cd74506e9 | 172 | pc.printf("\n\rLiving room light switched off"); |
Jamesan | 10:781cd74506e9 | 173 | Greenl = 1; |
Jamesan | 10:781cd74506e9 | 174 | Bluel = 1; |
Jamesan | 10:781cd74506e9 | 175 | if (ser == 'U') |
Jamesan | 10:781cd74506e9 | 176 | { |
Jamesan | 10:781cd74506e9 | 177 | myservo.write(0.4); |
Jamesan | 10:781cd74506e9 | 178 | wait(0.3); |
Jamesan | 10:781cd74506e9 | 179 | myservo.write(0.5); |
Jamesan | 10:781cd74506e9 | 180 | ser = 'L'; |
Jamesan | 10:781cd74506e9 | 181 | } |
Jamesan | 10:781cd74506e9 | 182 | Redl = 1; |
Jamesan | 10:781cd74506e9 | 183 | k = 'F'; |
Jamesan | 10:781cd74506e9 | 184 | } |
Jamesan | 10:781cd74506e9 | 185 | } |
Jamesan | 10:781cd74506e9 | 186 | |
Jamesan | 10:781cd74506e9 | 187 | } |
Jamesan | 10:781cd74506e9 | 188 | } |