project
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 #include "TextLCD.h" 00003 00004 PwmOut motor(p21); //Control the speed of the fan 00005 DigitalInOut DHT11(p5); // Activate digital in as dht11 Pin 00006 TextLCD lcd(p6, p7, p14, p15, p16, p17); // rs, e, d4-d7 00007 00008 00009 Timer tmr; //initialize timer 00010 uint64_t adat; // 64 bit variable for temporary data 00011 int i; 00012 void dht_read(void) { 00013 DHT11.output(); // Set p5 as output 00014 // Initialize measurement > 18 ms low 00015 DHT11 = 0; 00016 wait_ms(20); 00017 // After high and release the pin switch input mode 00018 DHT11 = 1; 00019 DHT11.input(); 00020 // Wait until the end of 80 us low 00021 while(!DHT11.read()) {} 00022 // Wait until end of 80 us high 00023 while(DHT11.read()) {} 00024 // 40 bit, 40 read out cycle 00025 for(i=0; i<40; i++) { 00026 adat = adat << 1; // Shift for new number 00027 tmr.stop(); // Stop timer if runs 00028 tmr.reset(); // Reset timer 00029 // Wait until pin 00030 while(!DHT11.read()) {} 00031 tmr.start(); 00032 while(DHT11.read()) {} 00033 // If DHT11 HIGH longer than 40 micro seconds (hopefully 70 us) 00034 if(tmr.read_us() > 40) { 00035 // bit is 1 00036 adat++; 00037 } 00038 } 00039 } 00040 00041 int main() { 00042 while(1) { 00043 adat = 0; 00044 motor.period(0.010); 00045 dht_read(); // Call the function 00046 int tem = (adat & 0x0000000000ff0000) >> 16; 00047 lcd.printf("Temperature:"); 00048 lcd.printf("%d\n",tem); // Temperature 00049 if(tem <= 20){ 00050 motor = 0; 00051 lcd.locate(4,1); 00052 lcd.printf("Fan OFF"); 00053 } 00054 else if(tem > 20 && tem <= 22) { 00055 motor = 0.2; 00056 lcd.printf("Fan Speed:"); 00057 lcd.locate(12,1); 00058 lcd.printf("20%"); 00059 } 00060 else if(tem > 22 && tem <= 24) { 00061 motor = 0.4; 00062 lcd.printf("Fan Speed:"); 00063 lcd.locate(12,1); 00064 lcd.printf("40%"); 00065 } 00066 else if(tem > 24 && tem <= 26) { 00067 motor = 0.6; 00068 lcd.printf("Fan Speed:"); 00069 lcd.locate(12,1); 00070 lcd.printf("60%"); 00071 } 00072 else if(tem > 26 && tem <= 28) { 00073 motor = 0.8; 00074 lcd.printf("Fan Speed:"); 00075 lcd.locate(12,1); 00076 lcd.printf("80%"); 00077 } 00078 else { 00079 motor = 1; 00080 lcd.printf("Fan Speed:"); 00081 lcd.locate(12,1); 00082 lcd.printf("100%"); 00083 } 00084 wait(2); // Wait 2 sec till continue. 00085 lcd.cls(); // Clear the screen 00086 } 00087 }
Generated on Sat Aug 6 2022 23:26:51 by
1.7.2