Advertisement
safwan092

Untitled

Oct 7th, 2023
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 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.  
  10. float averageVoltage = 0;
  11. float tdsValue = 0;
  12. float temperature = 16; // 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(TdsSensorPin,INPUT);
  41. }
  42.  
  43. void loop(){
  44. static unsigned long analogSampleTimepoint = millis();
  45. if(millis()-analogSampleTimepoint > 40U){ //every 40 milliseconds,read the analog value from the ADC
  46. analogSampleTimepoint = millis();
  47. analogBuffer[analogBufferIndex] = analogRead(TdsSensorPin); //read the analog value and store into the buffer
  48. analogBufferIndex++;
  49. if(analogBufferIndex == SCOUNT){
  50. analogBufferIndex = 0;
  51. }
  52. }
  53.  
  54. static unsigned long printTimepoint = millis();
  55. if(millis()-printTimepoint > 800U){
  56. printTimepoint = millis();
  57. for(copyIndex=0; copyIndex<SCOUNT; copyIndex++){
  58. analogBufferTemp[copyIndex] = analogBuffer[copyIndex];
  59.  
  60. // read the analog value more stable by the median filtering algorithm, and convert to voltage value
  61. averageVoltage = getMedianNum(analogBufferTemp,SCOUNT) * (float)VREF / 1024.0;
  62.  
  63. //temperature compensation formula: fFinalResult(25^C) = fFinalResult(current)/(1.0+0.02*(fTP-25.0));
  64. float compensationCoefficient = 1.0+0.02*(temperature-25.0);
  65. //temperature compensation
  66. float compensationVoltage=averageVoltage/compensationCoefficient;
  67.  
  68. //convert voltage value to tds value
  69. tdsValue=(133.42*compensationVoltage*compensationVoltage*compensationVoltage - 255.86*compensationVoltage*compensationVoltage + 857.39*compensationVoltage)*0.5;
  70.  
  71. //Serial.print("voltage:");
  72. //Serial.print(averageVoltage,2);
  73. //Serial.print("V ");
  74. Serial.print("TDS Value:");
  75. Serial.print(tdsValue,0);
  76. Serial.println("ppm");
  77. if(tdsValue>100){//turnon buzzer}
  78. }
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement