Group Project

Dependencies:   mbed Servo mbed-rtos MQ2 DHT

Committer:
roycoll
Date:
Wed Apr 24 03:21:58 2019 +0000
Revision:
2:823794c0ec6d
Parent:
1:98989411e334
Video Presentation version

Who changed what in which revision?

UserRevisionLine numberNew 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 1:98989411e334 19 PwmOut PWM1(D11); //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 1:98989411e334 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 1:98989411e334 87 while(p<=1.0 && sw2 ==0){
roycoll 1:98989411e334 88 p=p+0.05;
roycoll 0:872bdbfc6c15 89 myservo = p;
roycoll 0:872bdbfc6c15 90 wait(0.05);
roycoll 0:872bdbfc6c15 91 }
roycoll 0:872bdbfc6c15 92 win = open;
roycoll 0:872bdbfc6c15 93 }
roycoll 0:872bdbfc6c15 94 else if((win == open) && (sw2 == 0)){
roycoll 1:98989411e334 95 //for(; p>0 && sw2 ==0; p -= 0.05) {
roycoll 1:98989411e334 96 while(p>=0.0 && sw2 ==0){
roycoll 1:98989411e334 97 p=p-0.05;
roycoll 0:872bdbfc6c15 98 myservo = p;
roycoll 0:872bdbfc6c15 99 wait(0.05);
roycoll 0:872bdbfc6c15 100 }
roycoll 0:872bdbfc6c15 101 win = closed;
roycoll 0:872bdbfc6c15 102 }
roycoll 0:872bdbfc6c15 103 if(sw3 == 0 && Engine == on){ //***If switch3 is pressed
roycoll 0:872bdbfc6c15 104 if(AC == !on)
roycoll 0:872bdbfc6c15 105 { Heat = !on;AC = !off;}
roycoll 0:872bdbfc6c15 106 else if(Heat == !on)
roycoll 0:872bdbfc6c15 107 {Heat=!off;AC=!off;
roycoll 0:872bdbfc6c15 108 fan=off; }
roycoll 0:872bdbfc6c15 109 else
roycoll 0:872bdbfc6c15 110 AC = !on;
roycoll 0:872bdbfc6c15 111 }
roycoll 0:872bdbfc6c15 112 if(Heat == !on || AC == !on) {
roycoll 1:98989411e334 113 fan = 10;}
roycoll 0:872bdbfc6c15 114 else if(Heat == !off && AC == !off){
roycoll 0:872bdbfc6c15 115 fan = off;}
roycoll 0:872bdbfc6c15 116 if(Occupant == 1){
roycoll 0:872bdbfc6c15 117 Red = 0; //
roycoll 0:872bdbfc6c15 118 Blue = 0; //(r,g,b) = (0,0,0) = white
roycoll 0:872bdbfc6c15 119 Green = 0;
roycoll 0:872bdbfc6c15 120 Buzz = !off; } //
roycoll 0:872bdbfc6c15 121 else if(Occupant == 0)
roycoll 0:872bdbfc6c15 122 {
roycoll 0:872bdbfc6c15 123 if(MQ2_data.co>=300)
roycoll 0:872bdbfc6c15 124 { // wait(.5); //
roycoll 0:872bdbfc6c15 125 Red = 0; //
roycoll 0:872bdbfc6c15 126 Blue = 1; //(r,g,b) = (0,1,1) = red
roycoll 0:872bdbfc6c15 127 Green = 1; //
roycoll 0:872bdbfc6c15 128 Buzz = !on;
roycoll 0:872bdbfc6c15 129 if(win == closed){
roycoll 1:98989411e334 130 //for(; p<1.0; p += 0.05) { //**Servo simulates opening a window if CarbonMonoxide level inside becomes too high while occupant is present
roycoll 1:98989411e334 131 while(p<=1.0){
roycoll 1:98989411e334 132 p=p+0.05;
roycoll 0:872bdbfc6c15 133 myservo = p;
roycoll 0:872bdbfc6c15 134 wait(0.05);
roycoll 0:872bdbfc6c15 135 }
roycoll 0:872bdbfc6c15 136 win = open;
roycoll 0:872bdbfc6c15 137 }
roycoll 1:98989411e334 138 // if(win == open && Engine == on){
roycoll 1:98989411e334 139 // Engine = off;}
roycoll 0:872bdbfc6c15 140 } //
roycoll 0:872bdbfc6c15 141 else if(MQ2_data.co >=50 && MQ2_data.co <= 300)
roycoll 0:872bdbfc6c15 142 { //wait(.5); //
roycoll 0:872bdbfc6c15 143 Buzz = !off;
roycoll 0:872bdbfc6c15 144 Blue = 1; //(r,g,b) = (0,0,1) = yellow
roycoll 0:872bdbfc6c15 145 Green = 0; //
roycoll 0:872bdbfc6c15 146 Red = 0; //
roycoll 0:872bdbfc6c15 147 }//wait(.5); //**RGB end**
roycoll 0:872bdbfc6c15 148 else{
roycoll 0:872bdbfc6c15 149 Red = 1; //
roycoll 0:872bdbfc6c15 150 Blue = 1; //(r,g,b) = (1,1,0) = green
roycoll 0:872bdbfc6c15 151 Green = 0;
roycoll 0:872bdbfc6c15 152 Buzz = !off;
roycoll 0:872bdbfc6c15 153 }
roycoll 1:98989411e334 154 if(f >= 90 || f <= 50) //Temperature from DHT
roycoll 0:872bdbfc6c15 155 { Engine = on; //x = 10;
roycoll 1:98989411e334 156 fan = 10;
roycoll 1:98989411e334 157 if(f>=90){
roycoll 0:872bdbfc6c15 158 AC = !on;Heat=!off;
roycoll 0:872bdbfc6c15 159 if(win == closed){ //**Servo simulates opening a window if Temperature inside becomes too high while occupant is present
roycoll 1:98989411e334 160 //for(; p<1.0; p += 0.05) {
roycoll 1:98989411e334 161 while(p<=1.0){
roycoll 1:98989411e334 162 p=p+0.05;
roycoll 0:872bdbfc6c15 163 myservo = p;
roycoll 0:872bdbfc6c15 164 wait(0.05);
roycoll 0:872bdbfc6c15 165 }
roycoll 0:872bdbfc6c15 166 win = open;
roycoll 0:872bdbfc6c15 167 }
roycoll 1:98989411e334 168 else if(f<=50)
roycoll 0:872bdbfc6c15 169 { Heat = !on;AC=!off;
roycoll 0:872bdbfc6c15 170 if(win == open){
roycoll 1:98989411e334 171 //for(; p>0.0; p -= 0.05) {
roycoll 1:98989411e334 172 while(p>=0.0){
roycoll 1:98989411e334 173 p=p-0.05;
roycoll 0:872bdbfc6c15 174 myservo = p;
roycoll 0:872bdbfc6c15 175 wait(0.05);
roycoll 0:872bdbfc6c15 176 }
roycoll 0:872bdbfc6c15 177 win = closed;
roycoll 0:872bdbfc6c15 178 }
roycoll 0:872bdbfc6c15 179 }
roycoll 0:872bdbfc6c15 180 }
roycoll 0:872bdbfc6c15 181 }
roycoll 1:98989411e334 182 /* else if(Occupant == 0 && AC == !on){
roycoll 0:872bdbfc6c15 183 Engine = on; //x = 10;
roycoll 0:872bdbfc6c15 184 fan= 4;
roycoll 0:872bdbfc6c15 185 }
roycoll 0:872bdbfc6c15 186 else if(Occupant == 0 && Heat == !on){
roycoll 0:872bdbfc6c15 187 Engine = on; //x = 10;
roycoll 0:872bdbfc6c15 188 fan=4;
roycoll 2:823794c0ec6d 189 } */
roycoll 0:872bdbfc6c15 190 }
roycoll 0:872bdbfc6c15 191 }
roycoll 0:872bdbfc6c15 192 }