Advertisement
safwan092

Project_10591_Code

Jan 28th, 2023
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. #define SENSOR 27
  2. #define WaterLeak 23 //(D23)
  3. #define WaterLevelLow 36 //(VP)
  4. #define WaterLevelMed 39 //(VN)
  5. #define WaterLevelHigh 34 //(D34)
  6. #define TdsSensorPin 35 //(D35)
  7. #define VREF 3.3 // analog reference voltage(Volt) of the ADC
  8. #define SCOUNT 30 // sum of sample point
  9. int analogBuffer[SCOUNT]; // store the analog value in the array, read from ADC
  10. int analogBufferTemp[SCOUNT];
  11. int analogBufferIndex = 0;
  12. int copyIndex = 0;
  13. float averageVoltage = 0;
  14. float tdsValue = 0;
  15. float temperature = 0;
  16.  
  17. int WaterLevelLowValue = 0;
  18. int WaterLevelMedValue = 0;
  19. int WaterLevelHighValue = 0;
  20.  
  21. int WaterLeakValue = 0;
  22.  
  23.  
  24. unsigned long currentMillis = 0;
  25. unsigned long previousMillis = 0;
  26. int interval = 1000;
  27. boolean ledState = LOW;
  28. float calibrationFactor = 4.5;
  29. volatile byte pulseCount;
  30. byte pulse1Sec = 0;
  31. float flowRate;
  32. unsigned int flowMilliLitres;
  33. unsigned long totalMilliLitres;
  34. unsigned long printTimepoint;
  35. unsigned long analogSampleTimepoint;
  36.  
  37. float temperature25 = 25;
  38.  
  39. void IRAM_ATTR pulseCounter()
  40. {
  41. pulseCount++;
  42. }
  43.  
  44. void setup() {
  45. Serial.begin(115200);
  46. pinMode(TdsSensorPin, INPUT);
  47. pinMode(WaterLevelLow, INPUT);
  48. pinMode(WaterLevelMed, INPUT);
  49. pinMode(WaterLevelHigh, INPUT);
  50. pinMode(WaterLeak, INPUT);
  51. pinMode(SENSOR, INPUT_PULLUP);
  52. pulseCount = 0;
  53. flowRate = 0.0;
  54. flowMilliLitres = 0;
  55. totalMilliLitres = 0;
  56. previousMillis = 0;
  57. attachInterrupt(digitalPinToInterrupt(SENSOR), pulseCounter, FALLING);
  58. }
  59.  
  60. void loop() {
  61. readSensors();
  62. ShowDataSerial();
  63. }//end of loop
  64.  
  65.  
  66. int getMedianNum(int bArray[], int iFilterLen)
  67. {
  68. int bTab[iFilterLen];
  69. for (byte i = 0; i < iFilterLen; i++)
  70. bTab[i] = bArray[i];
  71. int i, j, bTemp;
  72. for (j = 0; j < iFilterLen - 1; j++)
  73. {
  74. for (i = 0; i < iFilterLen - j - 1; i++)
  75. {
  76. if (bTab[i] > bTab[i + 1])
  77. {
  78. bTemp = bTab[i];
  79. bTab[i] = bTab[i + 1];
  80. bTab[i + 1] = bTemp;
  81. }
  82. }
  83. }
  84. if ((iFilterLen & 1) > 0)
  85. bTemp = bTab[(iFilterLen - 1) / 2];
  86. else
  87. bTemp = (bTab[iFilterLen / 2] + bTab[iFilterLen / 2 - 1]) / 2;
  88. return bTemp;
  89. }
  90.  
  91.  
  92.  
  93. void ShowDataSerial() {
  94. Serial.print("Water Leak Value:");
  95. Serial.println(WaterLeakValue, 0);
  96.  
  97. Serial.print("WL LOW Value:");
  98. Serial.print(WaterLevelLowValue, 0);
  99. Serial.print("\t");
  100. Serial.print("WL MED Value:");
  101. Serial.print(WaterLevelMedValue, 0);
  102. Serial.print("\t");
  103. Serial.print("WL High Value:");
  104. Serial.print(WaterLevelHighValue, 0);
  105. Serial.println("\t");
  106.  
  107. Serial.print("TDS Value:");
  108. Serial.print(tdsValue, 0);
  109. Serial.println("ppm");
  110.  
  111. Serial.print("Flow rate: ");
  112. Serial.print(int(flowRate));
  113. Serial.print("L/min");
  114. Serial.print("\t");
  115. Serial.print("Output Liquid Quantity: ");
  116. Serial.print(totalMilliLitres);
  117. Serial.print("mL / ");
  118. Serial.print(totalMilliLitres / 1000);
  119. Serial.println("L");
  120. }
  121.  
  122. void readFlowSensor() {
  123. if (millis() - previousMillis > interval) {
  124. pulse1Sec = pulseCount;
  125. pulseCount = 0;
  126. flowRate = ((1000.0/(millis()-previousMillis))*pulse1Sec)/calibrationFactor;
  127. previousMillis = millis();
  128. flowMilliLitres = (flowRate / 60) * 1000;
  129. totalMilliLitres += flowMilliLitres;
  130. }
  131. }
  132.  
  133. void readTDSSensor() {
  134. analogSampleTimepoint = millis();
  135. if (millis() - analogSampleTimepoint > 40U) //every 40 milliseconds,read the analog value from the ADC
  136. {
  137. analogSampleTimepoint = millis();
  138. analogBuffer[analogBufferIndex] = analogRead(TdsSensorPin); //read the analog value and store into the buffer
  139. analogBufferIndex++;
  140. if (analogBufferIndex == SCOUNT)
  141. analogBufferIndex = 0;
  142. }
  143. }
  144.  
  145. void readSensors() {
  146. readFlowSensor();
  147. readTDSSensor();
  148. printTimepoint = millis();
  149. if (millis() - printTimepoint > 800U)
  150. {
  151. printTimepoint = millis();
  152. for (copyIndex = 0; copyIndex < SCOUNT; copyIndex++)
  153. analogBufferTemp[copyIndex] = analogBuffer[copyIndex];
  154. averageVoltage = getMedianNum(analogBufferTemp, SCOUNT) * (float)VREF / 1024.0; // read the analog value more stable by the median filtering algorithm, and convert to voltage value
  155. float compensationCoefficient = 1.0 + 0.02 * (temperature25 - 25.0); //temperature compensation formula: fFinalResult(25^C) = fFinalResult(current)/(1.0+0.02*(fTP-25.0));
  156. float compensationVolatge = averageVoltage / compensationCoefficient; //temperature compensation
  157. tdsValue = (133.42 * compensationVolatge * compensationVolatge * compensationVolatge - 255.86 * compensationVolatge * compensationVolatge + 857.39 * compensationVolatge) * 0.5; //convert voltage value to tds value
  158.  
  159. WaterLevelLowValue = analogRead(WaterLevelLow);
  160. WaterLevelMedValue = analogRead(WaterLevelMed);
  161. WaterLevelHighValue = analogRead(WaterLevelHigh);
  162.  
  163. WaterLeakValue = digitalRead(WaterLeak);
  164.  
  165. }
  166. }
  167.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement