Advertisement
androidgeek18

Volume Calculation Function

Jun 2nd, 2024
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. // Interrupt service routine to count pulses
  2. void IRAM_ATTR pulseCounter()
  3. {
  4. pulseCount++;
  5. }
  6.  
  7. void pulseCounterCalculation()
  8. {
  9. if ((millis() - oldTime) > 1000) // Only process counters once per second
  10. {
  11. // Disable the interrupt while calculating flow rate and sending the value to the host
  12. detachInterrupt(flowSensor);
  13.  
  14. // Because this loop may not complete in exactly 1 second intervals we calculate the number of
  15. // milliseconds that have passed since the last execution and use that to scale the output.
  16. // We also apply the calibrationFactor to scale the output based on the number of
  17. // pulses per second per units of measure (litres/minute in this case) coming from the sensor.
  18. flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
  19.  
  20. // Note the time this processing pass was executed. Note that because we've
  21. // disabled interrupts the millis() function won't actually be incrementing right
  22. // at this point, but it will still return the value it was set to just before
  23. // interrupts went away.
  24. oldTime = millis();
  25.  
  26. // Divide the flow rate in litres/minute by 60 to determine how many litres have
  27. // passed through the sensor in this 1 second interval,
  28. flowLitres = (flowRate / 60.0);
  29.  
  30. // Add the millilitres passed in this second to the cumulative total
  31. totalLitres += flowLitres;
  32.  
  33. //Print the flow rate for this second in litres / minute
  34. Serial.print("Flow rate: ");
  35. Serial.print(flowLitres, DEC); // Print the integer part of the variable
  36. Serial.print("mL/Second");
  37. Serial.print("\t");
  38.  
  39. // Print the cumulative total of litres flowed since starting
  40. Serial.print("Output Liquid Quantity: ");
  41. Serial.print(totalLitres, DEC);
  42. Serial.println("mL");
  43. Serial.print("\t");
  44.  
  45. // Update Blynk
  46. // Blynk.virtualWrite(V1, pulseCount);
  47. // Blynk.virtualWrite(V2, totalLitres);
  48.  
  49. // Reset the pulse counter so we can start incrementing again
  50. pulseCount = 0;
  51.  
  52. // Enable the interrupt again now that we've finished sending output
  53. attachInterrupt(flowSensor, pulseCounter, FALLING);
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement