Advertisement
androidgeek18

Volume Calculation Header File

Mar 30th, 2024
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. #ifndef VOLCALC_H
  2. #define VOLCALC_H
  3.  
  4. #include <Arduino.h>
  5. #include <Adafruit_GFX.h>
  6. #include <Adafruit_SSD1306.h>
  7. #include "OTA.h"
  8.  
  9. // Global Variables from main.cpp file
  10. extern Adafruit_SSD1306 display;
  11.  
  12. float flowRate = 0.0;
  13. unsigned int flowMilliLitres = 0;
  14. unsigned long totalMilliLitres = 0;
  15. unsigned long oldTime = 0;
  16. float calibrationFactor = 45.028; // You can change according to your datasheet
  17. volatile byte pulseCount = 0; // pulse count
  18. int sensorInterrupt = 4; // GPIO pin 4 same as sensor pin
  19. unsigned long currentTime = millis(); // Store the current time
  20.  
  21. // Interrupt Service Routine
  22. void pulseCounter()
  23. {
  24. // Increment the pulse counter
  25. pulseCount++;
  26. }
  27.  
  28. void displayCount()
  29. {
  30. // Display flow rate, total volume, and pulse count on OLED display
  31. display.clearDisplay();
  32. display.setTextSize(1); // Normal 1:1 pixel scale
  33. display.setTextWrap(true); // Enable text wrapping
  34. display.setTextColor(SSD1306_WHITE);
  35. display.setCursor(1, 10);
  36. display.print("Rate: ");
  37. display.print(flowMilliLitres, DEC); // Display with 2 decimal places
  38. display.println(" mL/sec");
  39. display.setCursor(1, 23);
  40. display.print("Total Vol:");
  41. // Print only a portion of the total volume, adjust as needed
  42. display.print(totalMilliLitres, DEC); // Display with 2 decimal places
  43. display.println(" mL");
  44. display.display(); // Display the content on OLED
  45. }
  46.  
  47. void volCalc()
  48. {
  49. if ((millis() - oldTime) > 1000) // Only process counters once per second
  50. {
  51. // Disable the interrupt while calculating flow rate and sending the value to the host
  52. detachInterrupt(sensorInterrupt);
  53.  
  54. // Because this loop may not complete in exactly 1 second intervals we calculate the number of milliseconds that have passed since the last execution and use that to scale the output. We also apply the calibrationFactor to scale the output based on the number of pulses per second per units of measure (litres/minute in this case) coming from the sensor.
  55. flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
  56.  
  57. // Note the time this processing pass was executed. Note that because we've
  58. // disabled interrupts the millis() function won't actually be incrementing right
  59. // at this point, but it will still return the value it was set to just before
  60. // interrupts went away.
  61. oldTime = millis();
  62.  
  63. // Divide the flow rate in litres/minute by 60 to determine how many litres have
  64. // passed through the sensor in this 1 second interval, then multiply by 1000 to
  65. // convert to millilitres.
  66. flowMilliLitres = (flowRate / 60) * 1000;
  67.  
  68. // Add the millilitres passed in this second to the cumulative total
  69. totalMilliLitres += flowMilliLitres;
  70.  
  71. unsigned int frac;
  72.  
  73. // Reset the pulse counter so we can start incrementing again
  74. pulseCount = 0;
  75.  
  76. // Enable the interrupt again now that we've finished sending output
  77. attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
  78.  
  79. // Call displayCount() to update the OLED display
  80. displayCount();
  81.  
  82. // Print the flow rate for this second in litres / minute
  83. Serial.print("Flow rate: ");
  84. Serial.print(flowMilliLitres, DEC); // Print the integer part of the variable
  85. Serial.print("mL/Second");
  86. Serial.print("\t");
  87.  
  88. // Print the cumulative total of litres flowed since starting
  89. Serial.print("Output Liquid Quantity: ");
  90. Serial.print(totalMilliLitres,DEC);
  91. Serial.println("mL");
  92. Serial.print("\t");
  93. }
  94. }
  95.  
  96. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement