My_microcontroller
Fork of DHT-11Mine by
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 #include "airhumidity.h" 00003 #include <string> 00004 00005 DigitalOut myled(LED1); 00006 Serial pc(USBTX, USBRX); // tx, rx 00007 Serial bt(PTE22, PTE23); 00008 AnalogIn gas_output(PTB1); 00009 00010 ///////////////////sara's code/////////////////// 00011 00012 using namespace std; 00013 00014 DigitalOut R1 (D6); 00015 DigitalOut R2 (D7); 00016 DigitalOut R3 (D8); 00017 DigitalOut R4 (D9); 00018 00019 InterruptIn C1 (D10); 00020 DigitalIn C2 (D11); 00021 DigitalIn C3 (D12); 00022 00023 DigitalOut keyled(LED1); 00024 DigitalOut flash(LED4); 00025 00026 AnalogIn ain(PTB0); 00027 00028 Timer t; 00029 float tm ; 00030 00031 char key; 00032 int s=0; 00033 00034 float value_adc =0.0f; 00035 int dig_value = 0; 00036 Timer timeout; 00037 float current_time = 0.0; 00038 00039 00040 00041 unsigned char arrkey[12]={ '1' , '2' , '3' , 00042 '4' , '5' , '6' , 00043 '7' , '8' , '9' , 00044 '*' , '0' , '#' }; 00045 00046 ///////////////sara's part////////////// 00047 00048 char msg; 00049 00050 00051 float temp=0.0; 00052 float humid=0.0; 00053 int unit =0; 00054 string room_safety_status = "issafe"; 00055 bool someone_in = false; 00056 bool door_status = false; 00057 string room_status = "yes"; 00058 string room_gas_status = "safe"; 00059 float gas_sensor_value = 0.0; 00060 00061 bool authorized_person_inside = false; 00062 bool authorized_person_entered = false; 00063 string last_person = " "; 00064 00065 00066 void unlock (){ 00067 printf("DOOR UNLOCK: Welcome!\n\r"); 00068 door_status = true; 00069 } 00070 00071 void lock (){ 00072 printf("DOOR LOCK\n\r"); 00073 } 00074 00075 unsigned char char_string[4]= {'*' ,'*' ,'*' ,'*'}; 00076 unsigned char password[4]={'1' , '2' , '3' , '4'}; 00077 unsigned char passwordA[4]={'4' , '5' , '6' , '7'}; 00078 00079 unsigned char keypad(){ 00080 unsigned char r,b; 00081 00082 while(current_time<5){ 00083 current_time = timeout.read(); 00084 for (r=0; r<4; r++){ 00085 b=4; 00086 R1 = 1; R2 = 1 ; R3 = 1 ; R4 = 1; 00087 if (r == 0) R1 = 0; 00088 if (r == 1) R2 = 0; 00089 if (r == 2) R3 = 0; 00090 if (r == 3) R4 = 0; 00091 00092 if(C1==0) b=0; 00093 if(C2==0) b=1; 00094 if(C3==0) b=2; 00095 00096 if ((!(b==4))){ 00097 key=arrkey[(r*3)+b]; 00098 while(C1==0); 00099 while(C2==0); 00100 while(C3==0); 00101 00102 wait(0.2); 00103 00104 char_string[s] = key; 00105 s++; 00106 return key; 00107 } 00108 00109 } 00110 } 00111 return 'Q'; 00112 } 00113 00114 void handleKeypad (){ 00115 00116 bool finished = false ; 00117 printf("In Intruppt\n\r"); 00118 __disable_irq(); 00119 C1.disable_irq (); 00120 00121 int firsttime = 0; 00122 00123 if (firsttime == 0) 00124 { 00125 s = -1; 00126 timeout.reset(); 00127 timeout.start(); 00128 } 00129 00130 current_time = timeout.read(); 00131 00132 while ((! finished) && (current_time < 5.0)){ 00133 key = keypad(); 00134 current_time = timeout.read(); 00135 if (s == 4){ 00136 finished = true; 00137 firsttime = 1; 00138 00139 printf("Password: "); 00140 for (int i = 0 ; i < 4 ; i++) 00141 printf("%c", char_string[i]); 00142 printf("\n\r"); 00143 s = 0; 00144 00145 00146 /////////////check password functions////////// 00147 int j = 0; 00148 int matchSara = 1; 00149 while (j < 4){ 00150 if (char_string [j] == password[j]) 00151 j++; 00152 else{ 00153 matchSara = 0; 00154 break; 00155 } 00156 if(matchSara && j == 3) 00157 { 00158 last_person = "Sara"; 00159 authorized_person_inside = true; 00160 room_safety_status = "issafe"; 00161 } 00162 } 00163 00164 int k = 0; 00165 int matchAmir = 1; 00166 while (k < 4){ 00167 if (char_string [k] == passwordA[k]) 00168 k++; 00169 else{ 00170 matchAmir = 0; 00171 break; 00172 } 00173 if(matchAmir && k == 3) 00174 { 00175 last_person = "Amir"; 00176 authorized_person_inside = true; 00177 room_safety_status = "issafe"; 00178 } 00179 } 00180 00181 if (matchSara == 1){ 00182 printf("MATCH Sara\n\r"); 00183 unlock(); 00184 C1.enable_irq(); 00185 __enable_irq(); 00186 break; 00187 } 00188 else if (matchAmir == 1){ 00189 printf("MATCH Amir\n\r"); 00190 unlock(); 00191 C1.enable_irq(); 00192 __enable_irq(); 00193 break; 00194 } 00195 else{ 00196 printf("WRONG\n\r"); 00197 printf("lock for wrong password\n\r"); 00198 C1.enable_irq(); 00199 __enable_irq(); 00200 break; 00201 } 00202 /////////////check password functions////////// 00203 00204 } // end s=4 00205 00206 } //end while 00207 00208 00209 timeout.stop(); 00210 C1.enable_irq (); 00211 __enable_irq(); 00212 } 00213 00214 00215 00216 00217 void uart_interrupt_fun() { 00218 __disable_irq(); 00219 msg = bt.getc(); 00220 //pc.putc(msg); 00221 if(msg == 's'){ 00222 msg = 'n'; 00223 wait(1); 00224 //pc.printf(" \n\r") ; 00225 bt.printf("info %4.2f %4.2f %s %s %s %s end\n\r", temp, humid, room_status,room_gas_status, room_safety_status, last_person) ; 00226 wait(3); 00227 for(int i=0; i<2; i++) 00228 { 00229 myled = 1; 00230 wait(0.2); 00231 myled = 0; 00232 wait(0.2); 00233 } 00234 00235 } 00236 __enable_irq(); 00237 00238 } 00239 00240 void check_rooms_presence_safety() 00241 { 00242 if(someone_in) 00243 { 00244 if(authorized_person_inside) 00245 { 00246 room_safety_status = "issafe"; 00247 authorized_person_entered = true; 00248 } 00249 else 00250 { 00251 room_safety_status = "notsafe"; 00252 } 00253 } 00254 if(! someone_in) 00255 { 00256 if(authorized_person_entered) 00257 { 00258 authorized_person_inside = false; 00259 room_safety_status = "notsafe"; 00260 } 00261 } 00262 00263 } 00264 00265 00266 int main() { 00267 00268 00269 while(1) { 00270 00271 myled = 1; 00272 wait(0.2); 00273 myled = 0; 00274 wait(0.2); 00275 00276 temp = get_air_temp_inside(unit); 00277 humid = get_air_humid_inside(); 00278 00279 ///////////////////sara's code//////////// 00280 value_adc = ain.read(); 00281 gas_sensor_value = gas_output.read()*50; 00282 check_rooms_presence_safety(); 00283 00284 00285 if(gas_sensor_value < 45) 00286 { 00287 room_gas_status = "safe"; 00288 } 00289 else 00290 { 00291 room_gas_status = "danger"; 00292 } 00293 00294 printf("Analog %5.2f\n\r",value_adc); 00295 00296 if (value_adc < 0.2){ 00297 //printf("Nobody is here!\n\r"); 00298 someone_in = false; 00299 } 00300 else { 00301 //printf("Somebody is here!\n\r"); 00302 someone_in = true; 00303 } 00304 //////////////////////// 00305 00306 00307 00308 bt.baud(38400); 00309 bt.attach(&uart_interrupt_fun); 00310 00311 R4 = 0; 00312 C1.fall(&handleKeypad); 00313 00314 00315 00316 wait(1); 00317 00318 00319 if(someone_in) 00320 { 00321 room_status = "yes"; 00322 } 00323 else 00324 { 00325 room_status = "no"; 00326 } 00327 00328 printf("Temperature is %4.2f C \r\n",temp); 00329 printf("Humid is %4.2f \r\n",humid); 00330 printf("Presence: %s \r\n",room_status); 00331 printf("gas: %f\r\n", gas_sensor_value); 00332 printf("room safety: %s\r\n",room_safety_status); 00333 00334 } 00335 }
Generated on Wed Jul 13 2022 09:49:33 by
1.7.2
