Vehicle Air Quality warning system/driver safety system, using MQ2 sensor and mq2 library

Dependencies:   MQ2

Committer:
roycoll
Date:
Thu Mar 07 14:58:39 2019 +0000
Revision:
3:2e06207b1e90
Parent:
1:e589b168e253
Version 1, Midterm project -  Vehicle Air quality warning system/safety system.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
azazeal88 0:1e2a25e50796 1 #include "mbed.h"
azazeal88 0:1e2a25e50796 2 #include "MQ2.h"
roycoll 3:2e06207b1e90 3 DigitalOut led(D2);
roycoll 3:2e06207b1e90 4 PwmOut PWM1(A5);
roycoll 3:2e06207b1e90 5 int on = 1, off = 0;
azazeal88 0:1e2a25e50796 6
roycoll 3:2e06207b1e90 7 Serial pc(USBTX, USBRX); // tx, rx
azazeal88 0:1e2a25e50796 8 MQ2 mq2(A1); // Analog Port to read from
roycoll 3:2e06207b1e90 9 int main()
roycoll 3:2e06207b1e90 10 {
roycoll 3:2e06207b1e90 11 PWM1.period_ms(500);
roycoll 3:2e06207b1e90 12 int x; x=1;
azazeal88 0:1e2a25e50796 13 pc.baud(115200);
azazeal88 0:1e2a25e50796 14 mq2.begin(); // 'Calibrate' sensor
azazeal88 0:1e2a25e50796 15 MQ2_data_t MQ2_data; // Structure to hold data.
roycoll 3:2e06207b1e90 16 while (true)
roycoll 3:2e06207b1e90 17 {
roycoll 3:2e06207b1e90 18 pc.printf("CO PPM: %.0f\r\n",mq2.readCO()); // Read of CO
roycoll 3:2e06207b1e90 19 pc.printf("Smoke PPM: %.0f\r\n",mq2.readSmoke()); // Read of Smoke
roycoll 3:2e06207b1e90 20 pc.printf("LPG PPM: %.0f\r\n",mq2.readLPG()); // Read of LPG
roycoll 3:2e06207b1e90 21 wait(.1);
roycoll 3:2e06207b1e90 22 pc.printf("................................\r\n");
roycoll 3:2e06207b1e90 23 mq2.read(&MQ2_data); // Alt reading method, reading to structure
roycoll 3:2e06207b1e90 24 pc.printf("CO PPM: %.0f\r\n",MQ2_data.co); // Return data from structure
roycoll 3:2e06207b1e90 25 pc.printf("Smoke PPM: %.0f\r\n",MQ2_data.smoke); // Return data from structure
roycoll 3:2e06207b1e90 26 pc.printf("LPG PPM: %.0f\r\n",MQ2_data.lpg); // Return data from structure
azazeal88 1:e589b168e253 27 pc.printf("................................\r\n");
roycoll 3:2e06207b1e90 28 wait(.1);
roycoll 3:2e06207b1e90 29 if (MQ2_data.co > 300)
roycoll 3:2e06207b1e90 30 {
roycoll 3:2e06207b1e90 31 led = on;
roycoll 3:2e06207b1e90 32 PWM1.pulsewidth_ms(x);
roycoll 3:2e06207b1e90 33 x=x+1;
roycoll 3:2e06207b1e90 34 wait(.1);
roycoll 3:2e06207b1e90 35 }
roycoll 3:2e06207b1e90 36 else
roycoll 3:2e06207b1e90 37 {
roycoll 3:2e06207b1e90 38 x=0;
roycoll 3:2e06207b1e90 39 PWM1.pulsewidth_ms(x);
roycoll 3:2e06207b1e90 40 led = off;
roycoll 3:2e06207b1e90 41 }
azazeal88 0:1e2a25e50796 42 }
azazeal88 0:1e2a25e50796 43 }
azazeal88 0:1e2a25e50796 44