Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define SENSOR 27
- #define WaterLeak 23 //(D23)
- #define WaterLevelLow 36 //(VP)
- #define WaterLevelMed 39 //(VN)
- #define WaterLevelHigh 34 //(D34)
- #define TdsSensorPin 35 //(D35)
- #define VREF 3.3 // analog reference voltage(Volt) of the ADC
- #define SCOUNT 30 // sum of sample point
- int analogBuffer[SCOUNT]; // store the analog value in the array, read from ADC
- int analogBufferTemp[SCOUNT];
- int analogBufferIndex = 0;
- int copyIndex = 0;
- float averageVoltage = 0;
- float tdsValue = 0;
- float temperature = 0;
- int WaterLevelLowValue = 0;
- int WaterLevelMedValue = 0;
- int WaterLevelHighValue = 0;
- int WaterLeakValue = 0;
- unsigned long currentMillis = 0;
- unsigned long previousMillis = 0;
- int interval = 1000;
- boolean ledState = LOW;
- float calibrationFactor = 4.5;
- volatile byte pulseCount;
- byte pulse1Sec = 0;
- float flowRate;
- unsigned int flowMilliLitres;
- unsigned long totalMilliLitres;
- unsigned long printTimepoint;
- unsigned long analogSampleTimepoint;
- float temperature25 = 25;
- void IRAM_ATTR pulseCounter()
- {
- pulseCount++;
- }
- void setup() {
- Serial.begin(115200);
- pinMode(TdsSensorPin, INPUT);
- pinMode(WaterLevelLow, INPUT);
- pinMode(WaterLevelMed, INPUT);
- pinMode(WaterLevelHigh, INPUT);
- pinMode(WaterLeak, INPUT);
- pinMode(SENSOR, INPUT_PULLUP);
- pulseCount = 0;
- flowRate = 0.0;
- flowMilliLitres = 0;
- totalMilliLitres = 0;
- previousMillis = 0;
- attachInterrupt(digitalPinToInterrupt(SENSOR), pulseCounter, FALLING);
- }
- void loop() {
- readSensors();
- ShowDataSerial();
- }//end of loop
- int getMedianNum(int bArray[], int iFilterLen)
- {
- int bTab[iFilterLen];
- for (byte i = 0; i < iFilterLen; i++)
- bTab[i] = bArray[i];
- int i, j, bTemp;
- for (j = 0; j < iFilterLen - 1; j++)
- {
- for (i = 0; i < iFilterLen - j - 1; i++)
- {
- if (bTab[i] > bTab[i + 1])
- {
- bTemp = bTab[i];
- bTab[i] = bTab[i + 1];
- bTab[i + 1] = bTemp;
- }
- }
- }
- if ((iFilterLen & 1) > 0)
- bTemp = bTab[(iFilterLen - 1) / 2];
- else
- bTemp = (bTab[iFilterLen / 2] + bTab[iFilterLen / 2 - 1]) / 2;
- return bTemp;
- }
- void ShowDataSerial() {
- Serial.print("Water Leak Value:");
- Serial.println(WaterLeakValue, 0);
- Serial.print("WL LOW Value:");
- Serial.print(WaterLevelLowValue, 0);
- Serial.print("\t");
- Serial.print("WL MED Value:");
- Serial.print(WaterLevelMedValue, 0);
- Serial.print("\t");
- Serial.print("WL High Value:");
- Serial.print(WaterLevelHighValue, 0);
- Serial.println("\t");
- Serial.print("TDS Value:");
- Serial.print(tdsValue, 0);
- Serial.println("ppm");
- Serial.print("Flow rate: ");
- Serial.print(int(flowRate));
- Serial.print("L/min");
- Serial.print("\t");
- Serial.print("Output Liquid Quantity: ");
- Serial.print(totalMilliLitres);
- Serial.print("mL / ");
- Serial.print(totalMilliLitres / 1000);
- Serial.println("L");
- }
- void readFlowSensor() {
- if (millis() - previousMillis > interval) {
- pulse1Sec = pulseCount;
- pulseCount = 0;
- flowRate = ((1000.0/(millis()-previousMillis))*pulse1Sec)/calibrationFactor;
- previousMillis = millis();
- flowMilliLitres = (flowRate / 60) * 1000;
- totalMilliLitres += flowMilliLitres;
- }
- }
- void readTDSSensor() {
- analogSampleTimepoint = millis();
- if (millis() - analogSampleTimepoint > 40U) //every 40 milliseconds,read the analog value from the ADC
- {
- analogSampleTimepoint = millis();
- analogBuffer[analogBufferIndex] = analogRead(TdsSensorPin); //read the analog value and store into the buffer
- analogBufferIndex++;
- if (analogBufferIndex == SCOUNT)
- analogBufferIndex = 0;
- }
- }
- void readSensors() {
- readFlowSensor();
- readTDSSensor();
- printTimepoint = millis();
- if (millis() - printTimepoint > 800U)
- {
- printTimepoint = millis();
- for (copyIndex = 0; copyIndex < SCOUNT; copyIndex++)
- analogBufferTemp[copyIndex] = analogBuffer[copyIndex];
- averageVoltage = getMedianNum(analogBufferTemp, SCOUNT) * (float)VREF / 1024.0; // read the analog value more stable by the median filtering algorithm, and convert to voltage value
- float compensationCoefficient = 1.0 + 0.02 * (temperature25 - 25.0); //temperature compensation formula: fFinalResult(25^C) = fFinalResult(current)/(1.0+0.02*(fTP-25.0));
- float compensationVolatge = averageVoltage / compensationCoefficient; //temperature compensation
- tdsValue = (133.42 * compensationVolatge * compensationVolatge * compensationVolatge - 255.86 * compensationVolatge * compensationVolatge + 857.39 * compensationVolatge) * 0.5; //convert voltage value to tds value
- WaterLevelLowValue = analogRead(WaterLevelLow);
- WaterLevelMedValue = analogRead(WaterLevelMed);
- WaterLevelHighValue = analogRead(WaterLevelHigh);
- WaterLeakValue = digitalRead(WaterLeak);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement