I am trying to calculate volume. Basically I want to pump x amount of gallons out of my aquarium every x minutes.
This is my first time using a flow meter and I am a little lost. I have found some code that I pieced together and made work but the flow rate never resets to zero even when water is not flowing.
I’m probably way off with this but what I was trying to do to get volume is calculate the flow rate and add the number every loop to get a total. Problem is that it will not count anything if flow rate is less than 1 and when the flow stops the flowrate number does not reset to zero.
// This #include statement was automatically added by the Particle IDE.
#include <SparkIntervalTimer.h>
/*
Water flow sensor test sketch
*/
#define FLOWSENSORPIN D6
IntervalTimer msTimer;
volatile uint32_t usLastTime = 0;
volatile uint32_t usDeltaTime = 0;
volatile uint32_t msCount = 0;
volatile double revPerSec = 0;
volatile double revPerMS = 0;
float flowRate =0.0;
float litersPerMinute=0.0;
void msISR() {
revPerMS = msCount;
msCount = 0;
}
void senseISR() {
uint32_t us = micros();
msCount++;
usDeltaTime = us - usLastTime;
usLastTime = us;
revPerSec = 1000000.0 / usDeltaTime;
}
// This #include statement was automatically added by the Particle IDE.
#include "Adafruit_SSD1306/Adafruit_SSD1306.h"
/*****************************************************************************
Particle Maker Kit Tutorial #2: Next Bus Alert
This tutorial uses a Particle Photon and the OLED screen from the Particle
Maker Kit. It uses a webhook to retrieve bus prediction times from the
NextBus Public XML feed, which must be set up first along with the webhook.
See http://docs.particle.io/tutorials/topics/maker-kit to learn how!
NOTE: This code example requires the Adafruit_SSD1306 library to be included,
so make sure to add it via the Libraries tab in the left sidebar.
******************************************************************************/
// use hardware SPI
#define OLED_DC D3
#define OLED_CS D4
#define OLED_RESET D5
Adafruit_SSD1306 display(OLED_DC, OLED_RESET, OLED_CS);
int i = 0;
int totalFlow = 0;
void setup() {
msTimer.begin(msISR, 1000, uSec); // trigger every 1000µs
pinMode(FLOWSENSORPIN, INPUT_PULLUP);
attachInterrupt(FLOWSENSORPIN, senseISR, FALLING);
// by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
display.begin(SSD1306_SWITCHCAPVCC);
display.setTextSize(2); // text size
display.setTextColor(WHITE); // text color
display.clearDisplay();
}
void loop() {
display.clearDisplay();
flowRate = (revPerSec * 2.25); //Take counted pulses in the last second and multiply by 2.25mL
flowRate = flowRate * 60; //Convert seconds to minutes, giving you mL / Minute
litersPerMinute = flowRate / 1000; //Convert mL to Liters, giving you Liters / Minute
totalFlow = totalFlow + litersPerMinute;
display.setCursor(0,0);
display.print(litersPerMinute);
display.setCursor(0,30);
display.print(totalFlow/60);
display.display();
}