safwan092

Untitled

Aug 6th, 2023
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 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.  
  7. const int mq7_a0 = A0;
  8. const int pushButtonPin = 8;
  9.  
  10. const byte contrastBlock[8] = {
  11. B11111,
  12. B11111,
  13. B11111,
  14. B11111,
  15. B11111,
  16. B11111,
  17. B11111,
  18. B00000
  19. };
  20.  
  21. const char* co_results[] = {"No Diabetes", "Not Determined", "Diabetes Type 1", "Diabetes Type 2"};
  22.  
  23. float calibrateSensor() {
  24. int numReadings = 10;
  25. float totalVoltage = 0.0;
  26.  
  27. for (int i = 0; i < numReadings; i++) {
  28. int raw_value = analogRead(mq7_a0);
  29. float voltage = (raw_value / 1023.0) * 5.0;
  30. totalVoltage += voltage;
  31. delay(100);
  32. }
  33.  
  34. float averageVoltage = totalVoltage / numReadings;
  35. float baselineConcentration = averageVoltage / 0.1; // Assuming 100mV per 1 ppm CO concentration
  36.  
  37. return baselineConcentration;
  38. }
  39.  
  40. void displayResult(float ppm) {
  41. lcd.clear();
  42. lcd.print("CO Level: ");
  43. lcd.print(ppm, 1); // Display CO concentration with 1 decimal place
  44.  
  45. int result = -1;
  46.  
  47. if (ppm >= 3.3 && ppm < 4.7) {
  48. result = 2; // Diabetes Type 1
  49. } else if (ppm <= 5.4 && ppm >= 4.8) {
  50. result = 3; // Diabetes Type 2
  51. } else {
  52. result = 0; // No Diabetes
  53. }
  54.  
  55. lcd.setCursor(0, 1);
  56. lcd.print(co_results[result]);
  57. delay(11000); // Display result for 11 seconds
  58. }
  59.  
  60. void setup() {
  61. pinMode(pushButtonPin, INPUT_PULLUP); // Set the push button pin as input with pull-up
  62. lcd.init();
  63. lcd.init();
  64. lcd.backlight();
  65. lcd.createChar(0, contrastBlock); // Create custom character for contrast control
  66. lcd.print("Welcome!");
  67. lcd.setCursor(0, 1);
  68. lcd.print("Press the button");
  69. }
  70.  
  71. void loop() {
  72. int buttonState = digitalRead(pushButtonPin);
  73. if (buttonState == LOW) {
  74. lcd.clear();
  75. lcd.print("Please Exhale");
  76. lcd.setCursor(0, 1);
  77. lcd.print("and Breathe Out!");
  78. delay(6000); // Wait for 6 seconds (Exhale time)
  79.  
  80. lcd.clear();
  81. lcd.print("Detecting");
  82. for (int i = 0; i < 3; i++) {
  83. lcd.print(".");
  84. delay(500); // Wait for 0.5 seconds
  85. }
  86.  
  87. // Calibrate the sensor for 1 second
  88. float baselineConcentration = calibrateSensor();
  89.  
  90. // Read CO concentration from sensor and convert to ppm
  91. int coLevel = analogRead(mq7_a0);
  92. float ppm = map(coLevel, 0, 1023, 0, 50);
  93.  
  94. displayResult(ppm);
  95.  
  96. lcd.clear();
  97. lcd.print("Calibrating..."); // Display calibration screen
  98. baselineConcentration = calibrateSensor(); // Calibrate the sensor again after displaying the result
  99. delay(1000); // Delay to show calibration screen for 1 second
  100.  
  101. lcd.clear();
  102. lcd.print("Welcome!");
  103. lcd.setCursor(0, 1);
  104. lcd.print("Press the button");
  105. while (digitalRead(pushButtonPin) == LOW) {
  106. // Wait for the button to be released before continuing
  107. }
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment