Advertisement
safwan092

Untitled

Sep 27th, 2023
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. #define TdsSensorPin A0
  2. #define VREF 5.0 // analog reference voltage(Volt) of the ADC
  3. #define SCOUNT 30 // sum of sample point
  4.  
  5. int analogBuffer[SCOUNT]; // store the analog value in the array, read from ADC
  6. int analogBufferTemp[SCOUNT];
  7. int analogBufferIndex = 0;
  8. int copyIndex = 0;
  9. int highValue = 200;
  10. float averageVoltage = 0;
  11. float tdsValue = 0;
  12. float temperature = 25; // current temperature for compensation
  13.  
  14. // median filtering algorithm
  15. int getMedianNum(int bArray[], int iFilterLen) {
  16. int bTab[iFilterLen];
  17. for (byte i = 0; i < iFilterLen; i++)
  18. bTab[i] = bArray[i];
  19. int i, j, bTemp;
  20. for (j = 0; j < iFilterLen - 1; j++) {
  21. for (i = 0; i < iFilterLen - j - 1; i++) {
  22. if (bTab[i] > bTab[i + 1]) {
  23. bTemp = bTab[i];
  24. bTab[i] = bTab[i + 1];
  25. bTab[i + 1] = bTemp;
  26. }
  27. }
  28. }
  29. if ((iFilterLen & 1) > 0) {
  30. bTemp = bTab[(iFilterLen - 1) / 2];
  31. }
  32. else {
  33. bTemp = (bTab[iFilterLen / 2] + bTab[iFilterLen / 2 - 1]) / 2;
  34. }
  35. return bTemp;
  36. }
  37.  
  38. void setup() {
  39. Serial.begin(115200);
  40. pinMode(8, OUTPUT);
  41. pinMode(9, OUTPUT);
  42. digitalWrite(8, LOW);
  43. digitalWrite(9, HIGH);
  44. pinMode(TdsSensorPin, INPUT);
  45. }
  46.  
  47. void loop() {
  48. static unsigned long analogSampleTimepoint = millis();
  49. if (millis() - analogSampleTimepoint > 40U) { //every 40 milliseconds,read the analog value from the ADC
  50. analogSampleTimepoint = millis();
  51. analogBuffer[analogBufferIndex] = analogRead(TdsSensorPin); //read the analog value and store into the buffer
  52. analogBufferIndex++;
  53. if (analogBufferIndex == SCOUNT) {
  54. analogBufferIndex = 0;
  55. }
  56. }
  57.  
  58. static unsigned long printTimepoint = millis();
  59. if (millis() - printTimepoint > 800U) {
  60. printTimepoint = millis();
  61. for (copyIndex = 0; copyIndex < SCOUNT; copyIndex++) {
  62. analogBufferTemp[copyIndex] = analogBuffer[copyIndex];
  63.  
  64. // read the analog value more stable by the median filtering algorithm, and convert to voltage value
  65. averageVoltage = getMedianNum(analogBufferTemp, SCOUNT) * (float)VREF / 1024.0;
  66.  
  67. //temperature compensation formula: fFinalResult(25^C) = fFinalResult(current)/(1.0+0.02*(fTP-25.0));
  68. float compensationCoefficient = 1.0 + 0.02 * (temperature - 25.0);
  69. //temperature compensation
  70. float compensationVoltage = averageVoltage / compensationCoefficient;
  71.  
  72. //convert voltage value to tds value
  73. tdsValue = (133.42 * compensationVoltage * compensationVoltage * compensationVoltage - 255.86 * compensationVoltage * compensationVoltage + 857.39 * compensationVoltage) * 0.5;
  74.  
  75. //Serial.print("voltage:");
  76. //Serial.print(averageVoltage,2);
  77. //Serial.print("V ");
  78. Serial.print("TDS Value:");
  79. Serial.print(tdsValue, 0);
  80. Serial.println("ppm");
  81. if (tdsValue > highValue) {
  82. digitalWrite(8, HIGH);
  83. digitalWrite(9, LOW);
  84. }
  85. else {
  86. digitalWrite(8, LOW);
  87. digitalWrite(9, HIGH);
  88. }
  89. }
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement