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
- int calibratingGlucoseLevel = 700000;
- #define mq7_a0 A0
- #define mq135_a1 A1
- #define mq2_a2 A2
- #define mq5_a3 A3
- int coLevel = 0;
- int mq135_Level = 0;
- int mq2_Level = 0;
- int mq5_Level = 0;
- const int pushButtonPin = 8;
- //----------------------------------------------------------------------------------------------------
- // MQ7
- // Calibration resistance value (Ro)
- const float Ro = 10.0;
- // Coefficients for the non-linear relation
- const float coefficientA = 84.771349776;
- const float coefficientB = -0.6505447222;
- const char* co_results[] = {"No Diabetes", "Diabetes Type 1", "Diabetes Type 2"};
- float baselineRsRo = 1.0; // Initialize baseline Rs/Ro to a known value
- // Calculate Rs/ro ratio and PPM using baselineRsRo
- float voltage;
- float Rs_ro_ratio;
- float PPM;
- void calculatePPM_MQ7() {
- // Calculate Rs/ro ratio and PPM using baselineRsRo
- voltage = (coLevel / 1023.0) * 5.0;
- Rs_ro_ratio = (5.0 - voltage) / voltage * (Ro / baselineRsRo);
- PPM = coefficientA * pow(Rs_ro_ratio, coefficientB);
- }
- //----------------------------------------------------------------------------------------------------
- //----------------------------------------------------------------------------------------------------
- // MQ135
- // Calibration resistance value (Ro) for MQ-135 sensor
- const float mq135Ro = 10.0;
- // Coefficients for the non-linear relation for MQ-135 sensor (acetone)
- const float mq135CoefficientA = 57.8082622183;
- const float mq135CoefficientB = -0.2569841436;
- const char* acetone_results[] = {"Healthy", "T2DM"};
- float mq135BaselineRsRo = 1.0; // Initialize baseline Rs/Ro for MQ-135 sensor
- float mq135PPM;
- int healthResult;
- float MQ135calculatePPM(int rawValue, float Ro, float coefficientA, float coefficientB) {
- float voltage = (rawValue / 1023.0) * 5.0;
- float Rs_ro_ratio = (5.0 - voltage) / voltage * (Ro / mq135BaselineRsRo);
- return coefficientA * pow(Rs_ro_ratio, coefficientB);
- }
- int determineHealth(float mq135PPM) {
- if (mq135PPM >= 10.73 && mq135PPM <= 57.13) {
- return 0; // Healthy
- } else if (mq135PPM >= 78.0 && mq135PPM <= 444.0) {
- return 1; // T2DM
- }
- return -1; // Invalid result
- }
- //----------------------------------------------------------------------------------------------------
- //----------------------------------------------------------------------------------------------------
- // MQ2
- // Calibration resistance value (Ro) for MQ-2 sensor
- const float mq2Ro = 10.0;
- // Coefficients for the non-linear relation for MQ-2 sensor (ethylene)
- const float mq2CoefficientA = 2233.153446891;
- const float mq2CoefficientB = -2.757374515;
- const char* diabetes_results[] = {"Healthy", "Diabetes Type 2"};
- float mq2BaselineRsRo = 1.0; // Initialize baseline Rs/Ro for MQ-2 sensor
- float mq2PPM;
- int diabetesResult;
- float MQ2calculatePPM(int rawValue, float Ro, float coefficientA, float coefficientB) {
- float voltage = (rawValue / 1023.0) * 5.0;
- float Rs_ro_ratio = (5.0 - voltage) / voltage * (Ro / mq2BaselineRsRo);
- return coefficientA * pow(Rs_ro_ratio, coefficientB);
- }
- int detectDiabetes(float mq2PPM) {
- if (mq2PPM < 10.73) {
- return 0; // Healthy
- } else if (mq2PPM >= 10.73 && mq2PPM <= 57.13) {
- return 0; // Healthy
- } else if (mq2PPM >= 78 && mq2PPM <= 444) {
- return 1; // Diabetes Type 2
- } else {
- return 1; // Diabetes Type 2
- }
- }
- //----------------------------------------------------------------------------------------------------
- //----------------------------------------------------------------------------------------------------
- // MQ5
- // Calibration resistance value (Ro) for MQ-5 sensor
- const float mq5Ro = 10.0;
- // Coefficients for the non-linear relation for MQ-5 sensor
- const float mq5CoefficientA = 4122.204007;
- const float mq5CoefficientB = -3.539510656;
- // Correlation equation parameters
- const float correlationCoefficient = 0.9098882772;
- const float correlationIntercept = 12.6462318032;
- const char* MQ5diabetes_results[] = {"Healthy", "Diabetes Type 1"};
- float mq5BaselineRsRo = 1.0; // Initialize baseline Rs/Ro for MQ-5 sensor
- float mq5PPM;
- float plasmaGlucose;
- int MQ5diabetesResult;
- float MQ5calculatePPM(int rawValue, float Ro, float coefficientA, float coefficientB) {
- float voltage = (rawValue / 1023.0) * 5.0;
- Serial.println(voltage);
- float Rs_ro_ratio = (5.0 - voltage) / voltage * (Ro / mq5BaselineRsRo);
- Serial.println(Rs_ro_ratio);
- return coefficientA * pow(Rs_ro_ratio, coefficientB);
- }
- float calculatePlasmaGlucose(float mq5PPM) {
- // Calculate plasma glucose using the correlation equation
- float predictedGlucose = correlationCoefficient * mq5PPM + correlationIntercept;
- return predictedGlucose;
- }
- int MQ5detectDiabetes(float plasmaGlucose) {
- // Diabetes detection logic using plasma glucose value
- // If Plasma Glucose is less than 140: Healthy
- // If Plasma Glucose is greater than or equal to 140: Diabetes Type 1
- if (plasmaGlucose < 140.0) {
- return 0; // Healthy
- } else {
- return 1; // Diabetes Type 1
- }
- }
- //----------------------------------------------------------------------------------------------------
- void setup() {
- Serial.begin(9600);
- 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.print("Welcome!");
- //lcd.setCursor(0, 1);
- //lcd.print("Press the button");
- }
- void loop() {
- coLevel = analogRead(mq7_a0);
- mq135_Level = analogRead(mq135_a1);
- mq2_Level = analogRead(mq2_a2);
- mq5_Level = analogRead(mq5_a3);
- Serial.print(coLevel);
- Serial.print("\t");
- Serial.print(mq135_Level);
- Serial.print("\t");
- Serial.print(mq2_Level);
- Serial.print("\t");
- Serial.print(mq5_Level);
- Serial.print("\n");
- calculatePPM_MQ7();
- // Calculate acetone PPM using MQ-135 sensor's equation
- mq135PPM = MQ135calculatePPM(mq135_Level, mq135Ro, mq135CoefficientA, mq135CoefficientB);
- // Calculate ethylene PPM using MQ-2 sensor's equation
- mq2PPM = MQ2calculatePPM(mq2_Level, mq2Ro, mq2CoefficientA, mq2CoefficientB);
- // Calculate PPM using MQ-5 sensor's equation
- mq5PPM = MQ5calculatePPM(mq5_Level, mq5Ro, mq5CoefficientA, mq5CoefficientB);
- mq5PPM = mq5PPM * calibratingGlucoseLevel;
- // Calculate plasma glucose using the correlation equation
- plasmaGlucose = calculatePlasmaGlucose(mq5PPM);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("S1:");
- lcd.print(PPM, 1);//MQ7
- lcd.setCursor(8, 0);
- lcd.print("S2:");
- lcd.print(mq135PPM, 2);//MQ135
- lcd.setCursor(0, 1);
- lcd.print("S3:");
- lcd.print(mq2PPM, 1);//MQ2
- lcd.setCursor(8, 1);
- lcd.print("S4:");
- lcd.print(plasmaGlucose, 1); //mq5PPM plasmaGlucose, 1);//MQ5
- delay(100);
- int buttonState = digitalRead(pushButtonPin);
- if (buttonState == LOW) {
- buttonPressReading();
- }
- }//end of LOOP
- void buttonPressReading() {
- lcd.clear();
- lcd.setCursor(0, 1);
- calculatePPM_MQ7();
- mq135PPM = MQ135calculatePPM(mq135_Level, mq135Ro, mq135CoefficientA, mq135CoefficientB);
- // Determine health condition based on acetone PPM
- healthResult = determineHealth(mq135PPM);
- // Calculate ethylene PPM using MQ-2 sensor's equation
- mq2PPM = MQ2calculatePPM(mq2_Level, mq2Ro, mq2CoefficientA, mq2CoefficientB);
- diabetesResult = detectDiabetes(mq2PPM);
- // Calculate PPM using MQ-5 sensor's equation
- mq5PPM = MQ5calculatePPM(mq5_Level, mq5Ro, mq5CoefficientA, mq5CoefficientB);
- mq5PPM = mq5PPM * calibratingGlucoseLevel;
- // Calculate plasma glucose using the correlation equation
- plasmaGlucose = calculatePlasmaGlucose(mq5PPM);
- // Detect diabetes type 1 or healthy
- MQ5diabetesResult = MQ5detectDiabetes(plasmaGlucose);
- displayResult(PPM);
- delay(1000);
- lcd.clear();
- lcd.print("Welcome!");
- lcd.setCursor(0, 1);
- lcd.print("Press the button");
- while (digitalRead(pushButtonPin) == LOW) {
- }
- }
- void displayResult(float ppm) {
- int result = -1;
- if (ppm >= 3.3 && ppm < 4.7) {
- result = 1;
- } else if (ppm <= 5.4 && ppm >= 4.8) {
- result = 2;
- } else {
- result = 0;
- }
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("S1:");
- lcd.print(PPM, 1);//MQ7
- lcd.print(" ppm");
- lcd.setCursor(0, 1);
- lcd.print(co_results[result]);
- delay(5000);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("S2:");
- lcd.print(mq135PPM, 2);//MQ135
- lcd.print(" ppm");
- lcd.setCursor(0, 1);
- lcd.print(acetone_results[healthResult]);
- delay(5000);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("S3:");
- lcd.print(mq2PPM, 1);//MQ2
- lcd.print(" ppm");
- lcd.setCursor(0, 1);
- lcd.print(diabetes_results[diabetesResult]);
- delay(5000);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("S4:");
- lcd.print(plasmaGlucose, 1);//MQ5
- lcd.print(" mg/dL");
- lcd.setCursor(0, 1);
- lcd.print(MQ5diabetes_results[MQ5diabetesResult]);
- delay(5000);
- if (co_results[result] == "No Diabetes" && acetone_results[healthResult] == "Healthy" && diabetes_results[diabetesResult] == "Healthy" && MQ5diabetes_results[MQ5diabetesResult] == "Healthy") {
- //Write OK
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("You are Healthy");
- delay(10000);
- }
- else {
- //Write See Doctor
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Please See your");
- lcd.setCursor(0, 1);
- lcd.print(" Doctor");
- delay(10000);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement