Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef VOLCALC_H
- #define VOLCALC_H
- #include <Arduino.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1306.h>
- #include "OTA.h"
- // Global Variables from main.cpp file
- extern Adafruit_SSD1306 display;
- float flowRate = 0.0;
- unsigned int flowMilliLitres = 0;
- unsigned long totalMilliLitres = 0;
- unsigned long oldTime = 0;
- float calibrationFactor = 45.028; // You can change according to your datasheet
- volatile byte pulseCount = 0; // pulse count
- int sensorInterrupt = 4; // GPIO pin 4 same as sensor pin
- unsigned long currentTime = millis(); // Store the current time
- // Interrupt Service Routine
- void pulseCounter()
- {
- // Increment the pulse counter
- pulseCount++;
- }
- void displayCount()
- {
- // Display flow rate, total volume, and pulse count on OLED display
- display.clearDisplay();
- display.setTextSize(1); // Normal 1:1 pixel scale
- display.setTextWrap(true); // Enable text wrapping
- display.setTextColor(SSD1306_WHITE);
- display.setCursor(1, 10);
- display.print("Rate: ");
- display.print(flowMilliLitres, DEC); // Display with 2 decimal places
- display.println(" mL/sec");
- display.setCursor(1, 23);
- display.print("Total Vol:");
- // Print only a portion of the total volume, adjust as needed
- display.print(totalMilliLitres, DEC); // Display with 2 decimal places
- display.println(" mL");
- display.display(); // Display the content on OLED
- }
- void volCalc()
- {
- if ((millis() - oldTime) > 1000) // Only process counters once per second
- {
- // Disable the interrupt while calculating flow rate and sending the value to the host
- detachInterrupt(sensorInterrupt);
- // 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.
- flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
- // Note the time this processing pass was executed. Note that because we've
- // disabled interrupts the millis() function won't actually be incrementing right
- // at this point, but it will still return the value it was set to just before
- // interrupts went away.
- oldTime = millis();
- // Divide the flow rate in litres/minute by 60 to determine how many litres have
- // passed through the sensor in this 1 second interval, then multiply by 1000 to
- // convert to millilitres.
- flowMilliLitres = (flowRate / 60) * 1000;
- // Add the millilitres passed in this second to the cumulative total
- totalMilliLitres += flowMilliLitres;
- unsigned int frac;
- // Reset the pulse counter so we can start incrementing again
- pulseCount = 0;
- // Enable the interrupt again now that we've finished sending output
- attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
- // Call displayCount() to update the OLED display
- displayCount();
- // Print the flow rate for this second in litres / minute
- Serial.print("Flow rate: ");
- Serial.print(flowMilliLitres, DEC); // Print the integer part of the variable
- Serial.print("mL/Second");
- Serial.print("\t");
- // Print the cumulative total of litres flowed since starting
- Serial.print("Output Liquid Quantity: ");
- Serial.print(totalMilliLitres,DEC);
- Serial.println("mL");
- Serial.print("\t");
- }
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement