Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
- const int mq7_a0 = A0;
- //float ppm;
- //float sensorVoltage;
- const int pushButtonPin = 8;
- // Calibration resistance values
- float Ro = 10; // Calibrate by measuring sensor resistance in clean air
- float cleanAirFactor = 9.83;
- const byte contrastBlock[8] = {
- B11111,
- B11111,
- B11111,
- B11111,
- B11111,
- B11111,
- B11111,
- B00000
- };
- const char* co_results[] = {"No Diabetes", "Not Determined", "Diabetes Type 1", "Diabetes Type 2"};
- float calibrateSensor() {
- int numReadings = 10;
- float totalVoltage = 0.0;
- for (int i = 0; i < numReadings; i++) {
- int raw_value = analogRead(mq7_a0);
- float voltage = (raw_value / 1023.0) * 5.0;
- totalVoltage += voltage;
- delay(100);
- }
- float averageVoltage = totalVoltage / numReadings;
- float baselineConcentration = averageVoltage / 0.1; // Assuming 100mV per 1 ppm CO concentration
- return baselineConcentration;
- }
- void displayResult(float ppm) {
- lcd.clear();
- lcd.print("CO Level: ");
- lcd.print(ppm, 1); // Display CO concentration with 1 decimal place
- int result = -1;
- if (ppm >= 3.3 && ppm < 4.7) {
- result = 2; // Diabetes Type 1
- } else if (ppm <= 5.4 && ppm >= 4.8) {
- result = 3; // Diabetes Type 2
- } else {
- result = 0; // No Diabetes
- }
- lcd.setCursor(0, 1);
- lcd.print(co_results[result]);
- delay(11000); // Display result for 11 seconds
- }
- void setup() {
- pinMode(mq7_a0, INPUT);
- pinMode(pushButtonPin, INPUT_PULLUP); // Set the push button pin as input with pull-up
- lcd.init();
- lcd.init();
- lcd.backlight();
- lcd.createChar(0, contrastBlock); // Create custom character for contrast control
- lcd.print("Welcome!");
- lcd.setCursor(0, 1);
- lcd.print("Press the button");
- }
- void loop() {
- int coLevel = analogRead(mq7_a0);
- lcd.setCursor(8, 0);
- lcd.print(" ppm:");
- lcd.print(coLevel);
- //sensorVoltage = analogRead(mq7_a0) * (5.0 / 1024.0);
- int buttonState = digitalRead(pushButtonPin);
- if (buttonState == LOW) {
- lcd.clear();
- lcd.print("Please Exhale");
- lcd.setCursor(0, 1);
- lcd.print("and Breathe Out!");
- delay(6000); // Wait for 6 seconds (Exhale time)
- lcd.clear();
- lcd.print("Detecting");
- for (int i = 0; i < 3; i++) {
- lcd.print(".");
- delay(500); // Wait for 0.5 seconds
- }
- // Calibrate the sensor for 1 second
- float baselineConcentration = calibrateSensor();
- // Read CO concentration from sensor and convert to ppm
- float ppm = map(coLevel, 0, 1023, 0, 50);
- displayResult(ppm);
- lcd.clear();
- lcd.print("Calibrating..."); // Display calibration screen
- baselineConcentration = calibrateSensor(); // Calibrate the sensor again after displaying the result
- delay(1000); // Delay to show calibration screen for 1 second
- lcd.clear();
- lcd.print("Welcome!");
- lcd.setCursor(0, 1);
- lcd.print("Press the button");
- while (digitalRead(pushButtonPin) == LOW) {
- // Wait for the button to be released before continuing
- }
- }
- }
- /*
- void calculateCO_PPM_Log() {
- float sensorResistance = (5.0 - sensorVoltage) / sensorVoltage;
- // Take log of resistance to linearize sensor response
- sensorResistance = log(sensorResistance);
- // Apply clean air factor
- float correctedResistance = sensorResistance / cleanAirFactor;
- // Calculate PPM
- ppm = pow(10, (log(Ro) - correctedResistance));
- }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement