Group Project
Dependencies: mbed Servo mbed-rtos MQ2 DHT
main.cpp@0:872bdbfc6c15, 2019-04-23 (annotated)
- Committer:
- roycoll
- Date:
- Tue Apr 23 15:14:58 2019 +0000
- Revision:
- 0:872bdbfc6c15
- Child:
- 1:98989411e334
Edit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
roycoll | 0:872bdbfc6c15 | 1 | #include "mbed.h" |
roycoll | 0:872bdbfc6c15 | 2 | #include "DHT.h" |
roycoll | 0:872bdbfc6c15 | 3 | #include "MQ2.h" |
roycoll | 0:872bdbfc6c15 | 4 | #include "rtos.h" |
roycoll | 0:872bdbfc6c15 | 5 | #include "Servo.h" |
roycoll | 0:872bdbfc6c15 | 6 | |
roycoll | 0:872bdbfc6c15 | 7 | |
roycoll | 0:872bdbfc6c15 | 8 | DigitalIn Occupant(D10); //Hall Switch input shows to determine if vehicle is occupied |
roycoll | 0:872bdbfc6c15 | 9 | DigitalIn EngSw(D3); //Button D3 turns Engine on and off |
roycoll | 0:872bdbfc6c15 | 10 | DigitalOut Buzz(D2); //Alarm signaling high gas levels |
roycoll | 0:872bdbfc6c15 | 11 | DigitalOut Engine(D9); //Autoflash LED represents Engine running |
roycoll | 0:872bdbfc6c15 | 12 | DigitalOut Red(PTC3); //RED LED (RGB) D7 |
roycoll | 0:872bdbfc6c15 | 13 | DigitalOut Green(PTC2); //GREEN LED (RGB) D6 |
roycoll | 0:872bdbfc6c15 | 14 | DigitalOut Blue(PTA2); //BLUE LED (RGB) D5 |
roycoll | 0:872bdbfc6c15 | 15 | DigitalOut Heat(LED1); //Red on-board LED |
roycoll | 0:872bdbfc6c15 | 16 | DigitalOut AC(LED3); //Blue on-board LED |
roycoll | 0:872bdbfc6c15 | 17 | DigitalOut led2(LED2); //Green on-board LED |
roycoll | 0:872bdbfc6c15 | 18 | DHT sensor(D4, DHT11); //Temperature and humidity sensor |
roycoll | 0:872bdbfc6c15 | 19 | PwmOut PWM1(A4); //pin A4 == pin A5 |
roycoll | 0:872bdbfc6c15 | 20 | Serial pc(USBTX, USBRX); |
roycoll | 0:872bdbfc6c15 | 21 | MQ2 mq2(A1); // Analog Port to read from MQ2_gas sensor |
roycoll | 0:872bdbfc6c15 | 22 | Servo myservo(A5); //Servo signal to simulate a window opening or closing |
roycoll | 0:872bdbfc6c15 | 23 | InterruptIn sw2(SW2); //Switch used to manually switch between AC/Heater setting(led color) |
roycoll | 0:872bdbfc6c15 | 24 | InterruptIn sw3(SW3); //Switch used to manually open or close the window |
roycoll | 0:872bdbfc6c15 | 25 | float p = 0; |
roycoll | 0:872bdbfc6c15 | 26 | float h = 0.0f, c = 0.0f, f = 0.0f, k = 0.0f, dp = 0.0f, dpf = 0.0f; |
roycoll | 0:872bdbfc6c15 | 27 | int x = 1; |
roycoll | 0:872bdbfc6c15 | 28 | int win = 0; |
roycoll | 0:872bdbfc6c15 | 29 | int fan = 0; /*DC motor signal*/ |
roycoll | 0:872bdbfc6c15 | 30 | |
roycoll | 0:872bdbfc6c15 | 31 | |
roycoll | 0:872bdbfc6c15 | 32 | int main() { |
roycoll | 0:872bdbfc6c15 | 33 | myservo = p; |
roycoll | 0:872bdbfc6c15 | 34 | pc.baud(115200); |
roycoll | 0:872bdbfc6c15 | 35 | mq2.begin(); // 'Calibrate' sensor |
roycoll | 0:872bdbfc6c15 | 36 | MQ2_data_t MQ2_data; // Structure to hold data. |
roycoll | 0:872bdbfc6c15 | 37 | sensor.readData(); |
roycoll | 0:872bdbfc6c15 | 38 | Engine = 0; |
roycoll | 0:872bdbfc6c15 | 39 | Heat = 1; |
roycoll | 0:872bdbfc6c15 | 40 | AC = 1; |
roycoll | 0:872bdbfc6c15 | 41 | led2 = 1; |
roycoll | 0:872bdbfc6c15 | 42 | int on = 1, off = 0; |
roycoll | 0:872bdbfc6c15 | 43 | int open = 1, closed = 0; |
roycoll | 0:872bdbfc6c15 | 44 | Buzz = !off; |
roycoll | 0:872bdbfc6c15 | 45 | win = closed; |
roycoll | 0:872bdbfc6c15 | 46 | PWM1.period_ms(500); |
roycoll | 0:872bdbfc6c15 | 47 | PWM1.pulsewidth_ms(fan); |
roycoll | 0:872bdbfc6c15 | 48 | |
roycoll | 0:872bdbfc6c15 | 49 | while (true) |
roycoll | 0:872bdbfc6c15 | 50 | { mq2.read(&MQ2_data); |
roycoll | 0:872bdbfc6c15 | 51 | PWM1.period_ms(500); |
roycoll | 0:872bdbfc6c15 | 52 | PWM1.pulsewidth_ms(fan); |
roycoll | 0:872bdbfc6c15 | 53 | int error = 0; //Thread::wait(250); |
roycoll | 0:872bdbfc6c15 | 54 | error = sensor.readData(); |
roycoll | 0:872bdbfc6c15 | 55 | if (0 == error) { |
roycoll | 0:872bdbfc6c15 | 56 | c = sensor.ReadTemperature(CELCIUS); |
roycoll | 0:872bdbfc6c15 | 57 | f = sensor.ReadTemperature(FARENHEIT); |
roycoll | 0:872bdbfc6c15 | 58 | k = sensor.ReadTemperature(KELVIN); |
roycoll | 0:872bdbfc6c15 | 59 | h = sensor.ReadHumidity(); |
roycoll | 0:872bdbfc6c15 | 60 | dp = sensor.CalcdewPoint(c, h); |
roycoll | 0:872bdbfc6c15 | 61 | dpf = sensor.CalcdewPointFast(c, h); |
roycoll | 0:872bdbfc6c15 | 62 | pc.printf("Temperature in Celcius: %4.2f\r\nTemperature in Farenheit: %4.2f\r\n", c, f); |
roycoll | 0:872bdbfc6c15 | 63 | //printf("Temperature in Kelvin: %4.2f,\r\nCelcius: %4.2f,\r\n Farenheit %4.2f\r\n", k, c, f); |
roycoll | 0:872bdbfc6c15 | 64 | //printf("Humidity is %4.2f, Dewpoint: %4.2f, Dewpoint fast: %4.2f\n", h, dp, dpf); |
roycoll | 0:872bdbfc6c15 | 65 | } |
roycoll | 0:872bdbfc6c15 | 66 | else { |
roycoll | 0:872bdbfc6c15 | 67 | pc.printf("Temperature in Celcius: %4.2f\r\nTemperature in Farenheit: %4.2f\r\n", c, f); //pc.printf("Error: %d\n", error); |
roycoll | 0:872bdbfc6c15 | 68 | } |
roycoll | 0:872bdbfc6c15 | 69 | //pc.printf("................................\r\n"); |
roycoll | 0:872bdbfc6c15 | 70 | //mq2.read(&MQ2_data); // Alt reading method, reading to structure |
roycoll | 0:872bdbfc6c15 | 71 | pc.printf("CO PPM: %.0f\r\n",MQ2_data.co); // Return data from structure |
roycoll | 0:872bdbfc6c15 | 72 | //pc.printf("Smoke PPM: %.0f\r\n",MQ2_data.smoke); // Return data from structure |
roycoll | 0:872bdbfc6c15 | 73 | //pc.printf("LPG PPM: %.0f\r\n",MQ2_data.lpg); // Return data from structure |
roycoll | 0:872bdbfc6c15 | 74 | pc.printf("................................\r\n"); |
roycoll | 0:872bdbfc6c15 | 75 | // wait(.1); |
roycoll | 0:872bdbfc6c15 | 76 | if(EngSw == 0) { |
roycoll | 0:872bdbfc6c15 | 77 | if(Engine == off){ |
roycoll | 0:872bdbfc6c15 | 78 | Engine = on;} |
roycoll | 0:872bdbfc6c15 | 79 | else{ |
roycoll | 0:872bdbfc6c15 | 80 | Engine = off; |
roycoll | 0:872bdbfc6c15 | 81 | AC = !off; |
roycoll | 0:872bdbfc6c15 | 82 | Heat = !off; |
roycoll | 0:872bdbfc6c15 | 83 | } |
roycoll | 0:872bdbfc6c15 | 84 | } |
roycoll | 0:872bdbfc6c15 | 85 | if((sw2 == 0) && (win == closed)){ |
roycoll | 0:872bdbfc6c15 | 86 | for(; p<1.0 && sw2 == 0; p += 0.05) { //**Servo simulates opening a window if CarbonMonoxide level inside becomes too high while occupant is present |
roycoll | 0:872bdbfc6c15 | 87 | myservo = p; |
roycoll | 0:872bdbfc6c15 | 88 | wait(0.05); |
roycoll | 0:872bdbfc6c15 | 89 | } |
roycoll | 0:872bdbfc6c15 | 90 | win = open; |
roycoll | 0:872bdbfc6c15 | 91 | } |
roycoll | 0:872bdbfc6c15 | 92 | else if((win == open) && (sw2 == 0)){ |
roycoll | 0:872bdbfc6c15 | 93 | for(; p>0 && sw2 ==0; p -= 0.05) { |
roycoll | 0:872bdbfc6c15 | 94 | myservo = p; |
roycoll | 0:872bdbfc6c15 | 95 | wait(0.05); |
roycoll | 0:872bdbfc6c15 | 96 | } |
roycoll | 0:872bdbfc6c15 | 97 | win = closed; |
roycoll | 0:872bdbfc6c15 | 98 | } |
roycoll | 0:872bdbfc6c15 | 99 | if(sw3 == 0 && Engine == on){ //***If switch3 is pressed |
roycoll | 0:872bdbfc6c15 | 100 | if(AC == !on) |
roycoll | 0:872bdbfc6c15 | 101 | { Heat = !on;AC = !off;} |
roycoll | 0:872bdbfc6c15 | 102 | else if(Heat == !on) |
roycoll | 0:872bdbfc6c15 | 103 | {Heat=!off;AC=!off; |
roycoll | 0:872bdbfc6c15 | 104 | fan=off; } |
roycoll | 0:872bdbfc6c15 | 105 | else |
roycoll | 0:872bdbfc6c15 | 106 | AC = !on; |
roycoll | 0:872bdbfc6c15 | 107 | } |
roycoll | 0:872bdbfc6c15 | 108 | if(Heat == !on || AC == !on) { |
roycoll | 0:872bdbfc6c15 | 109 | fan = 4;} |
roycoll | 0:872bdbfc6c15 | 110 | else if(Heat == !off && AC == !off){ |
roycoll | 0:872bdbfc6c15 | 111 | fan = off;} |
roycoll | 0:872bdbfc6c15 | 112 | if(Occupant == 1){ |
roycoll | 0:872bdbfc6c15 | 113 | Red = 0; // |
roycoll | 0:872bdbfc6c15 | 114 | Blue = 0; //(r,g,b) = (0,0,0) = white |
roycoll | 0:872bdbfc6c15 | 115 | Green = 0; |
roycoll | 0:872bdbfc6c15 | 116 | Buzz = !off; } // |
roycoll | 0:872bdbfc6c15 | 117 | else if(Occupant == 0) |
roycoll | 0:872bdbfc6c15 | 118 | { |
roycoll | 0:872bdbfc6c15 | 119 | if(MQ2_data.co>=300) |
roycoll | 0:872bdbfc6c15 | 120 | { // wait(.5); // |
roycoll | 0:872bdbfc6c15 | 121 | Red = 0; // |
roycoll | 0:872bdbfc6c15 | 122 | Blue = 1; //(r,g,b) = (0,1,1) = red |
roycoll | 0:872bdbfc6c15 | 123 | Green = 1; // |
roycoll | 0:872bdbfc6c15 | 124 | Buzz = !on; |
roycoll | 0:872bdbfc6c15 | 125 | if(win == closed){ |
roycoll | 0:872bdbfc6c15 | 126 | for(; p<1.0; p += 0.05) { //**Servo simulates opening a window if CarbonMonoxide level inside becomes too high while occupant is present |
roycoll | 0:872bdbfc6c15 | 127 | myservo = p; |
roycoll | 0:872bdbfc6c15 | 128 | wait(0.05); |
roycoll | 0:872bdbfc6c15 | 129 | } |
roycoll | 0:872bdbfc6c15 | 130 | win = open; |
roycoll | 0:872bdbfc6c15 | 131 | } |
roycoll | 0:872bdbfc6c15 | 132 | if(win==open && Engine == on){ |
roycoll | 0:872bdbfc6c15 | 133 | Engine = off;} |
roycoll | 0:872bdbfc6c15 | 134 | } // |
roycoll | 0:872bdbfc6c15 | 135 | else if(MQ2_data.co >=50 && MQ2_data.co <= 300) |
roycoll | 0:872bdbfc6c15 | 136 | { //wait(.5); // |
roycoll | 0:872bdbfc6c15 | 137 | Buzz = !off; |
roycoll | 0:872bdbfc6c15 | 138 | Blue = 1; //(r,g,b) = (0,0,1) = yellow |
roycoll | 0:872bdbfc6c15 | 139 | Green = 0; // |
roycoll | 0:872bdbfc6c15 | 140 | Red = 0; // |
roycoll | 0:872bdbfc6c15 | 141 | }//wait(.5); //**RGB end** |
roycoll | 0:872bdbfc6c15 | 142 | else{ |
roycoll | 0:872bdbfc6c15 | 143 | Red = 1; // |
roycoll | 0:872bdbfc6c15 | 144 | Blue = 1; //(r,g,b) = (1,1,0) = green |
roycoll | 0:872bdbfc6c15 | 145 | Green = 0; |
roycoll | 0:872bdbfc6c15 | 146 | Buzz = !off; |
roycoll | 0:872bdbfc6c15 | 147 | } |
roycoll | 0:872bdbfc6c15 | 148 | if(f >= 80 || f <= 70) //Temperature from DHT |
roycoll | 0:872bdbfc6c15 | 149 | { Engine = on; //x = 10; |
roycoll | 0:872bdbfc6c15 | 150 | fan = 4; |
roycoll | 0:872bdbfc6c15 | 151 | if(f>=80){ |
roycoll | 0:872bdbfc6c15 | 152 | AC = !on;Heat=!off; |
roycoll | 0:872bdbfc6c15 | 153 | if(win == closed){ //**Servo simulates opening a window if Temperature inside becomes too high while occupant is present |
roycoll | 0:872bdbfc6c15 | 154 | for(; p<1.0; p += 0.05) { |
roycoll | 0:872bdbfc6c15 | 155 | myservo = p; |
roycoll | 0:872bdbfc6c15 | 156 | wait(0.05); |
roycoll | 0:872bdbfc6c15 | 157 | } |
roycoll | 0:872bdbfc6c15 | 158 | win = open; |
roycoll | 0:872bdbfc6c15 | 159 | } |
roycoll | 0:872bdbfc6c15 | 160 | else if(f<=70) |
roycoll | 0:872bdbfc6c15 | 161 | { Heat = !on;AC=!off; |
roycoll | 0:872bdbfc6c15 | 162 | if(win == open){ |
roycoll | 0:872bdbfc6c15 | 163 | for(; p>0; p -= 0.05) { |
roycoll | 0:872bdbfc6c15 | 164 | myservo = p; |
roycoll | 0:872bdbfc6c15 | 165 | wait(0.05); |
roycoll | 0:872bdbfc6c15 | 166 | } |
roycoll | 0:872bdbfc6c15 | 167 | win = closed; |
roycoll | 0:872bdbfc6c15 | 168 | } |
roycoll | 0:872bdbfc6c15 | 169 | } |
roycoll | 0:872bdbfc6c15 | 170 | } |
roycoll | 0:872bdbfc6c15 | 171 | } |
roycoll | 0:872bdbfc6c15 | 172 | else if(Occupant == 0 && AC == !on){ |
roycoll | 0:872bdbfc6c15 | 173 | Engine = on; //x = 10; |
roycoll | 0:872bdbfc6c15 | 174 | fan= 4; |
roycoll | 0:872bdbfc6c15 | 175 | } |
roycoll | 0:872bdbfc6c15 | 176 | else if(Occupant == 0 && Heat == !on){ |
roycoll | 0:872bdbfc6c15 | 177 | Engine = on; //x = 10; |
roycoll | 0:872bdbfc6c15 | 178 | fan=4; |
roycoll | 0:872bdbfc6c15 | 179 | } //**DC motor sig end |
roycoll | 0:872bdbfc6c15 | 180 | } |
roycoll | 0:872bdbfc6c15 | 181 | } |
roycoll | 0:872bdbfc6c15 | 182 | } |