Your sketch:
#define VER 003
#define enableDisplay true
#define sensorConstant 6.6
const byte pinToMonitor = 2;
boolean LeadingEdgeOccured = false;
unsigned long endCycle;
unsigned long beginCycle = 0;
unsigned long beginAverageCycle;
float frequency;
float AverageFrequency;
unsigned long count = 0;
unsigned long countStart;
float LitersPerMinute;
unsigned long displayPeriodically = millis();
/*
* Measures Frequency and counts pulses on pin to monitor
*
* The manufacturer provides the formula F=6.6Q(l/min)
* and stated that Q is 60 min
* I suspect that lpm = F/6.6
* If max lpm is 60 lpm, then max F = 200hz, which would be pretty fast
* ver 003
* removed spin character
*/
#if enableDisplay
// Display
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
/*
byte s0[8] = {B00000,B00100,B00100,B00100,B00100,B00100,B00000,B00000}; // |
byte s1[8] = {B00000,B00001,B00010,B00100,B01000,B10000,B00000,B00000}; // /
byte s2[8] = {B00000,B00000,B00000,B11111,B00000,B00000,B00000,B00000}; // -
byte s3[8] = {B00000,B10000,B01000,B00100,B00010,B00001,B00000,B00000}; // \\ (backslash)
byte spinPtr = 0;
unsigned long spinTimeStart = millis();
#define spinTimeBetween 250
//-----------------------------------------------------------------------------------spin
void spin(){
if (millis()-spinTimeStart < spinTimeBetween) return;
spinTimeStart = millis();
lcd.setCursor(15,0);
lcd.write(byte(spinPtr++%4));
}
//-----------------------------------------------------------------------------------setupDisplay
*/
void setupDisplay(){
lcd.begin();
lcd.noBacklight();
lcd.clear();
/*
lcd.createChar(0,s0);
lcd.createChar(1,s1);
lcd.createChar(2,s2);
lcd.createChar(3,s3);
*/
lcd.setCursor(0,0);
lcd.print("Flow Sensor ");
// 0123456789ABCDEF
lcd.print(VER);
lcd.backlight();
}
#endif
//-----------------------------------------------------------------------------------ISR acknowledgeLeadingEdge
void acknowledgeLeadingEdge() {// want this to be quick to minimize error in timing
LeadingEdgeOccured = true;
}
//-----------------------------------------------------------------------------------displayFrequencyAndCount
void displayFrequencyAndCount(){
char outline[33],outline2[17] ;
char Hz[5],HzA[5],lpm[5];
dtostrf(frequency,4,0,Hz);
dtostrf(AverageFrequency,4,0,HzA);
dtostrf(LitersPerMinute,4,1,lpm);
sprintf(outline,"%sHz %sHz ",Hz,HzA);
sprintf(outline2,"%sl/m %9lu",lpm,count);
// 0123456789ABCDEF
// ffffHz ffffHz
// ff.fl/m
#if enableDisplay
lcd.setCursor(0,0);
lcd.print(outline);
lcd.setCursor(0,1);
lcd.print(outline2);
/*
lcd.print(" Hz LPM ");
lcd.print(" count");
lcd.setCursor(0,0);lcd.print(frequency,0);
lcd.setCursor(6,0);lcd.print(LitersPerMinute,1);
lcd.setCursor(0,1);lcd.print(count);
*/
#else
Serial.print("\r");
Serial.print(outline);Serial.print(outline2);
#endif
}
//-----------------------------------------------------------------------------------setup
void setup() {
Serial.begin(115200);
Serial.print(F("\r\nFrequency Measurement Version "));Serial.println(VER);
pinMode(pinToMonitor, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(pinToMonitor), acknowledgeLeadingEdge, RISING);
#if enableDisplay
setupDisplay();
#endif
}
//-----------------------------------------------------------------------------------loop
void loop () {
if (LeadingEdgeOccured){
LeadingEdgeOccured = false;
count++;
endCycle = millis();
if (endCycle-displayPeriodically > 1000) { // only print to console every second
if (beginCycle == 0 ){
beginCycle = millis();
beginAverageCycle = beginCycle;
count = 0;
countStart = 0;
}
else {
displayPeriodically = millis();
frequency = float(count-countStart) * 1000.0/float(endCycle-beginCycle);
AverageFrequency = float(count) * 1000.0/float(endCycle-beginAverageCycle);
LitersPerMinute = frequency / sensorConstant;
displayFrequencyAndCount();
if (endCycle == beginCycle or endCycle == beginAverageCycle) while(1);
countStart=count;
beginCycle = endCycle;
}
}
}
#if enableDisplay
// spin();
#endif
}