Advertisement
safwan092

Untitled

Aug 9th, 2023
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.52 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. int calibratingGlucoseLevel = 700000;
  6. #define mq7_a0 A0
  7. #define mq135_a1 A1
  8. #define mq2_a2 A2
  9. #define mq5_a3 A3
  10.  
  11. int coLevel = 0;
  12. int mq135_Level = 0;
  13. int mq2_Level = 0;
  14. int mq5_Level = 0;
  15.  
  16. const int pushButtonPin = 8;
  17.  
  18. //----------------------------------------------------------------------------------------------------
  19. // MQ7
  20. // Calibration resistance value (Ro)
  21. const float Ro = 10.0;
  22.  
  23. // Coefficients for the non-linear relation
  24. const float coefficientA = 84.771349776;
  25. const float coefficientB = -0.6505447222;
  26.  
  27. const char* co_results[] = {"No Diabetes", "Diabetes Type 1", "Diabetes Type 2"};
  28.  
  29. float baselineRsRo = 1.0; // Initialize baseline Rs/Ro to a known value
  30.  
  31. // Calculate Rs/ro ratio and PPM using baselineRsRo
  32. float voltage;
  33. float Rs_ro_ratio;
  34. float PPM;
  35.  
  36. void calculatePPM_MQ7() {
  37. // Calculate Rs/ro ratio and PPM using baselineRsRo
  38. voltage = (coLevel / 1023.0) * 5.0;
  39. Rs_ro_ratio = (5.0 - voltage) / voltage * (Ro / baselineRsRo);
  40. PPM = coefficientA * pow(Rs_ro_ratio, coefficientB);
  41. }
  42. //----------------------------------------------------------------------------------------------------
  43. //----------------------------------------------------------------------------------------------------
  44. // MQ135
  45. // Calibration resistance value (Ro) for MQ-135 sensor
  46. const float mq135Ro = 10.0;
  47.  
  48. // Coefficients for the non-linear relation for MQ-135 sensor (acetone)
  49. const float mq135CoefficientA = 57.8082622183;
  50. const float mq135CoefficientB = -0.2569841436;
  51.  
  52. const char* acetone_results[] = {"Healthy", "T2DM"};
  53.  
  54. float mq135BaselineRsRo = 1.0; // Initialize baseline Rs/Ro for MQ-135 sensor
  55. float mq135PPM;
  56. int healthResult;
  57.  
  58. float MQ135calculatePPM(int rawValue, float Ro, float coefficientA, float coefficientB) {
  59. float voltage = (rawValue / 1023.0) * 5.0;
  60. float Rs_ro_ratio = (5.0 - voltage) / voltage * (Ro / mq135BaselineRsRo);
  61. return coefficientA * pow(Rs_ro_ratio, coefficientB);
  62. }
  63.  
  64. int determineHealth(float mq135PPM) {
  65. if (mq135PPM >= 10.73 && mq135PPM <= 57.13) {
  66. return 0; // Healthy
  67. } else if (mq135PPM >= 78.0 && mq135PPM <= 444.0) {
  68. return 1; // T2DM
  69. }
  70. return -1; // Invalid result
  71. }
  72. //----------------------------------------------------------------------------------------------------
  73. //----------------------------------------------------------------------------------------------------
  74. // MQ2
  75. // Calibration resistance value (Ro) for MQ-2 sensor
  76. const float mq2Ro = 10.0;
  77.  
  78. // Coefficients for the non-linear relation for MQ-2 sensor (ethylene)
  79. const float mq2CoefficientA = 2233.153446891;
  80. const float mq2CoefficientB = -2.757374515;
  81.  
  82. const char* diabetes_results[] = {"Healthy", "Diabetes Type 2"};
  83.  
  84. float mq2BaselineRsRo = 1.0; // Initialize baseline Rs/Ro for MQ-2 sensor
  85.  
  86. float mq2PPM;
  87. int diabetesResult;
  88.  
  89. float MQ2calculatePPM(int rawValue, float Ro, float coefficientA, float coefficientB) {
  90. float voltage = (rawValue / 1023.0) * 5.0;
  91. float Rs_ro_ratio = (5.0 - voltage) / voltage * (Ro / mq2BaselineRsRo);
  92. return coefficientA * pow(Rs_ro_ratio, coefficientB);
  93. }
  94.  
  95. int detectDiabetes(float mq2PPM) {
  96. if (mq2PPM < 10.73) {
  97. return 0; // Healthy
  98. } else if (mq2PPM >= 10.73 && mq2PPM <= 57.13) {
  99. return 0; // Healthy
  100. } else if (mq2PPM >= 78 && mq2PPM <= 444) {
  101. return 1; // Diabetes Type 2
  102. } else {
  103. return 1; // Diabetes Type 2
  104. }
  105. }
  106. //----------------------------------------------------------------------------------------------------
  107. //----------------------------------------------------------------------------------------------------
  108. // MQ5
  109. // Calibration resistance value (Ro) for MQ-5 sensor
  110. const float mq5Ro = 10.0;
  111.  
  112. // Coefficients for the non-linear relation for MQ-5 sensor
  113. const float mq5CoefficientA = 4122.204007;
  114. const float mq5CoefficientB = -3.539510656;
  115.  
  116. // Correlation equation parameters
  117. const float correlationCoefficient = 0.9098882772;
  118. const float correlationIntercept = 12.6462318032;
  119.  
  120. const char* MQ5diabetes_results[] = {"Healthy", "Diabetes Type 1"};
  121.  
  122. float mq5BaselineRsRo = 1.0; // Initialize baseline Rs/Ro for MQ-5 sensor
  123. float mq5PPM;
  124. float plasmaGlucose;
  125. int MQ5diabetesResult;
  126.  
  127. float MQ5calculatePPM(int rawValue, float Ro, float coefficientA, float coefficientB) {
  128. float voltage = (rawValue / 1023.0) * 5.0;
  129. Serial.println(voltage);
  130. float Rs_ro_ratio = (5.0 - voltage) / voltage * (Ro / mq5BaselineRsRo);
  131. Serial.println(Rs_ro_ratio);
  132. return coefficientA * pow(Rs_ro_ratio, coefficientB);
  133. }
  134.  
  135. float calculatePlasmaGlucose(float mq5PPM) {
  136. // Calculate plasma glucose using the correlation equation
  137. float predictedGlucose = correlationCoefficient * mq5PPM + correlationIntercept;
  138. return predictedGlucose;
  139. }
  140.  
  141. int MQ5detectDiabetes(float plasmaGlucose) {
  142. // Diabetes detection logic using plasma glucose value
  143. // If Plasma Glucose is less than 140: Healthy
  144. // If Plasma Glucose is greater than or equal to 140: Diabetes Type 1
  145. if (plasmaGlucose < 140.0) {
  146. return 0; // Healthy
  147. } else {
  148. return 1; // Diabetes Type 1
  149. }
  150. }
  151. //----------------------------------------------------------------------------------------------------
  152. void setup() {
  153. Serial.begin(9600);
  154. pinMode(mq7_a0, INPUT);
  155. pinMode(pushButtonPin, INPUT_PULLUP); // Set the push button pin as input with pull-up
  156. lcd.init();
  157. lcd.init();
  158. lcd.backlight();
  159. //lcd.print("Welcome!");
  160. //lcd.setCursor(0, 1);
  161. //lcd.print("Press the button");
  162. }
  163.  
  164. void loop() {
  165. coLevel = analogRead(mq7_a0);
  166. mq135_Level = analogRead(mq135_a1);
  167. mq2_Level = analogRead(mq2_a2);
  168. mq5_Level = analogRead(mq5_a3);
  169. Serial.print(coLevel);
  170. Serial.print("\t");
  171. Serial.print(mq135_Level);
  172. Serial.print("\t");
  173. Serial.print(mq2_Level);
  174. Serial.print("\t");
  175. Serial.print(mq5_Level);
  176. Serial.print("\n");
  177. calculatePPM_MQ7();
  178. // Calculate acetone PPM using MQ-135 sensor's equation
  179. mq135PPM = MQ135calculatePPM(mq135_Level, mq135Ro, mq135CoefficientA, mq135CoefficientB);
  180. // Calculate ethylene PPM using MQ-2 sensor's equation
  181. mq2PPM = MQ2calculatePPM(mq2_Level, mq2Ro, mq2CoefficientA, mq2CoefficientB);
  182. // Calculate PPM using MQ-5 sensor's equation
  183. mq5PPM = MQ5calculatePPM(mq5_Level, mq5Ro, mq5CoefficientA, mq5CoefficientB);
  184. mq5PPM = mq5PPM * calibratingGlucoseLevel;
  185. // Calculate plasma glucose using the correlation equation
  186. plasmaGlucose = calculatePlasmaGlucose(mq5PPM);
  187. lcd.clear();
  188. lcd.setCursor(0, 0);
  189. lcd.print("S1:");
  190. lcd.print(PPM, 1);//MQ7
  191. lcd.setCursor(8, 0);
  192. lcd.print("S2:");
  193. lcd.print(mq135PPM, 2);//MQ135
  194. lcd.setCursor(0, 1);
  195. lcd.print("S3:");
  196. lcd.print(mq2PPM, 1);//MQ2
  197. lcd.setCursor(8, 1);
  198. lcd.print("S4:");
  199. lcd.print(plasmaGlucose, 1); //mq5PPM plasmaGlucose, 1);//MQ5
  200. delay(100);
  201. int buttonState = digitalRead(pushButtonPin);
  202. if (buttonState == LOW) {
  203. buttonPressReading();
  204. }
  205. }//end of LOOP
  206.  
  207.  
  208. void buttonPressReading() {
  209. lcd.clear();
  210. lcd.setCursor(0, 1);
  211. calculatePPM_MQ7();
  212. mq135PPM = MQ135calculatePPM(mq135_Level, mq135Ro, mq135CoefficientA, mq135CoefficientB);
  213. // Determine health condition based on acetone PPM
  214. healthResult = determineHealth(mq135PPM);
  215. // Calculate ethylene PPM using MQ-2 sensor's equation
  216. mq2PPM = MQ2calculatePPM(mq2_Level, mq2Ro, mq2CoefficientA, mq2CoefficientB);
  217. diabetesResult = detectDiabetes(mq2PPM);
  218. // Calculate PPM using MQ-5 sensor's equation
  219. mq5PPM = MQ5calculatePPM(mq5_Level, mq5Ro, mq5CoefficientA, mq5CoefficientB);
  220. mq5PPM = mq5PPM * calibratingGlucoseLevel;
  221. // Calculate plasma glucose using the correlation equation
  222. plasmaGlucose = calculatePlasmaGlucose(mq5PPM);
  223. // Detect diabetes type 1 or healthy
  224. MQ5diabetesResult = MQ5detectDiabetes(plasmaGlucose);
  225.  
  226. displayResult(PPM);
  227.  
  228.  
  229. delay(1000);
  230.  
  231. lcd.clear();
  232. lcd.print("Welcome!");
  233. lcd.setCursor(0, 1);
  234. lcd.print("Press the button");
  235. while (digitalRead(pushButtonPin) == LOW) {
  236. }
  237. }
  238.  
  239. void displayResult(float ppm) {
  240. int result = -1;
  241.  
  242. if (ppm >= 3.3 && ppm < 4.7) {
  243. result = 1;
  244. } else if (ppm <= 5.4 && ppm >= 4.8) {
  245. result = 2;
  246. } else {
  247. result = 0;
  248. }
  249.  
  250. lcd.clear();
  251. lcd.setCursor(0, 0);
  252. lcd.print("S1:");
  253. lcd.print(PPM, 1);//MQ7
  254. lcd.print(" ppm");
  255. lcd.setCursor(0, 1);
  256. lcd.print(co_results[result]);
  257. delay(5000);
  258. lcd.clear();
  259. lcd.setCursor(0, 0);
  260. lcd.print("S2:");
  261. lcd.print(mq135PPM, 2);//MQ135
  262. lcd.print(" ppm");
  263. lcd.setCursor(0, 1);
  264. lcd.print(acetone_results[healthResult]);
  265. delay(5000);
  266. lcd.clear();
  267. lcd.setCursor(0, 0);
  268. lcd.print("S3:");
  269. lcd.print(mq2PPM, 1);//MQ2
  270. lcd.print(" ppm");
  271. lcd.setCursor(0, 1);
  272. lcd.print(diabetes_results[diabetesResult]);
  273. delay(5000);
  274. lcd.clear();
  275. lcd.setCursor(0, 0);
  276. lcd.print("S4:");
  277. lcd.print(plasmaGlucose, 1);//MQ5
  278. lcd.print(" mg/dL");
  279. lcd.setCursor(0, 1);
  280. lcd.print(MQ5diabetes_results[MQ5diabetesResult]);
  281. delay(5000);
  282. if (co_results[result] == "No Diabetes" && acetone_results[healthResult] == "Healthy" && diabetes_results[diabetesResult] == "Healthy" && MQ5diabetes_results[MQ5diabetesResult] == "Healthy") {
  283. //Write OK
  284. lcd.clear();
  285. lcd.setCursor(0, 0);
  286. lcd.print("You are Healthy");
  287. delay(10000);
  288. }
  289. else {
  290. //Write See Doctor
  291. lcd.clear();
  292. lcd.setCursor(0, 0);
  293. lcd.print("Please See your");
  294. lcd.setCursor(0, 1);
  295. lcd.print(" Doctor");
  296. delay(10000);
  297. }
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement