Advertisement
safwan092

Untitled

Aug 6th, 2023
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <LiquidCrystal_I2C.h>
  3.  
  4. LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
  5.  
  6. const int mq7_a0 = A0;
  7. //float ppm;
  8. //float sensorVoltage;
  9.  
  10. const int pushButtonPin = 8;
  11.  
  12. // Calibration resistance values
  13. float Ro = 10; // Calibrate by measuring sensor resistance in clean air
  14. float cleanAirFactor = 9.83;
  15.  
  16. const byte contrastBlock[8] = {
  17. B11111,
  18. B11111,
  19. B11111,
  20. B11111,
  21. B11111,
  22. B11111,
  23. B11111,
  24. B00000
  25. };
  26.  
  27. const char* co_results[] = {"No Diabetes", "Not Determined", "Diabetes Type 1", "Diabetes Type 2"};
  28.  
  29. float calibrateSensor() {
  30. int numReadings = 10;
  31. float totalVoltage = 0.0;
  32.  
  33. for (int i = 0; i < numReadings; i++) {
  34. int raw_value = analogRead(mq7_a0);
  35. float voltage = (raw_value / 1023.0) * 5.0;
  36. totalVoltage += voltage;
  37. delay(100);
  38. }
  39.  
  40. float averageVoltage = totalVoltage / numReadings;
  41. float baselineConcentration = averageVoltage / 0.1; // Assuming 100mV per 1 ppm CO concentration
  42.  
  43. return baselineConcentration;
  44. }
  45.  
  46. void displayResult(float ppm) {
  47. lcd.clear();
  48. lcd.print("CO Level: ");
  49. lcd.print(ppm, 1); // Display CO concentration with 1 decimal place
  50.  
  51. int result = -1;
  52.  
  53. if (ppm >= 3.3 && ppm < 4.7) {
  54. result = 2; // Diabetes Type 1
  55. } else if (ppm <= 5.4 && ppm >= 4.8) {
  56. result = 3; // Diabetes Type 2
  57. } else {
  58. result = 0; // No Diabetes
  59. }
  60.  
  61. lcd.setCursor(0, 1);
  62. lcd.print(co_results[result]);
  63. delay(11000); // Display result for 11 seconds
  64. }
  65.  
  66. void setup() {
  67. pinMode(mq7_a0, INPUT);
  68. pinMode(pushButtonPin, INPUT_PULLUP); // Set the push button pin as input with pull-up
  69. lcd.init();
  70. lcd.init();
  71. lcd.backlight();
  72. lcd.createChar(0, contrastBlock); // Create custom character for contrast control
  73. lcd.print("Welcome!");
  74. lcd.setCursor(0, 1);
  75. lcd.print("Press the button");
  76. }
  77.  
  78. void loop() {
  79. int coLevel = analogRead(mq7_a0);
  80. lcd.setCursor(8, 0);
  81. lcd.print(" ppm:");
  82. lcd.print(coLevel);
  83. //sensorVoltage = analogRead(mq7_a0) * (5.0 / 1024.0);
  84. int buttonState = digitalRead(pushButtonPin);
  85. if (buttonState == LOW) {
  86. lcd.clear();
  87. lcd.print("Please Exhale");
  88. lcd.setCursor(0, 1);
  89. lcd.print("and Breathe Out!");
  90. delay(6000); // Wait for 6 seconds (Exhale time)
  91.  
  92. lcd.clear();
  93. lcd.print("Detecting");
  94. for (int i = 0; i < 3; i++) {
  95. lcd.print(".");
  96. delay(500); // Wait for 0.5 seconds
  97. }
  98.  
  99. // Calibrate the sensor for 1 second
  100. float baselineConcentration = calibrateSensor();
  101.  
  102. // Read CO concentration from sensor and convert to ppm
  103. float ppm = map(coLevel, 0, 1023, 0, 50);
  104.  
  105. displayResult(ppm);
  106.  
  107. lcd.clear();
  108. lcd.print("Calibrating..."); // Display calibration screen
  109. baselineConcentration = calibrateSensor(); // Calibrate the sensor again after displaying the result
  110. delay(1000); // Delay to show calibration screen for 1 second
  111.  
  112. lcd.clear();
  113. lcd.print("Welcome!");
  114. lcd.setCursor(0, 1);
  115. lcd.print("Press the button");
  116. while (digitalRead(pushButtonPin) == LOW) {
  117. // Wait for the button to be released before continuing
  118. }
  119. }
  120. }
  121.  
  122. /*
  123. void calculateCO_PPM_Log() {
  124. float sensorResistance = (5.0 - sensorVoltage) / sensorVoltage;
  125. // Take log of resistance to linearize sensor response
  126. sensorResistance = log(sensorResistance);
  127. // Apply clean air factor
  128. float correctedResistance = sensorResistance / cleanAirFactor;
  129. // Calculate PPM
  130. ppm = pow(10, (log(Ro) - correctedResistance));
  131. }*/
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement