Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 #include "MQ2.h" 00003 DigitalOut led(D2); 00004 PwmOut PWM1(A5); 00005 int on = 1, off = 0; 00006 00007 Serial pc(USBTX, USBRX); // tx, rx 00008 MQ2 mq2(A1); // Analog Port to read from 00009 int main() 00010 { 00011 PWM1.period_ms(500); 00012 int x; x=1; 00013 pc.baud(115200); 00014 mq2.begin(); // 'Calibrate' sensor 00015 MQ2_data_t MQ2_data; // Structure to hold data. 00016 while (true) 00017 { 00018 pc.printf("CO PPM: %.0f\r\n",mq2.readCO()); // Read of CO 00019 pc.printf("Smoke PPM: %.0f\r\n",mq2.readSmoke()); // Read of Smoke 00020 pc.printf("LPG PPM: %.0f\r\n",mq2.readLPG()); // Read of LPG 00021 wait(.1); 00022 pc.printf("................................\r\n"); 00023 mq2.read(&MQ2_data); // Alt reading method, reading to structure 00024 pc.printf("CO PPM: %.0f\r\n",MQ2_data.co); // Return data from structure 00025 pc.printf("Smoke PPM: %.0f\r\n",MQ2_data.smoke); // Return data from structure 00026 pc.printf("LPG PPM: %.0f\r\n",MQ2_data.lpg); // Return data from structure 00027 pc.printf("................................\r\n"); 00028 wait(.1); 00029 if (MQ2_data.co > 300) 00030 { 00031 led = on; 00032 PWM1.pulsewidth_ms(x); 00033 x=x+1; 00034 wait(.1); 00035 } 00036 else 00037 { 00038 x=0; 00039 PWM1.pulsewidth_ms(x); 00040 led = off; 00041 } 00042 } 00043 } 00044
Generated on Tue Jul 12 2022 18:37:04 by
1.7.2