Hello everyone
Sorry about my English..
I have to finish my final project so please try to help me ![]()
I'm using Arduino Mega
I have two flow sensor (hall sensor) and i'm trying to write the data about the flow on LCD (16X2) screen, but I get some blankts rectangles
Here is my code
#include <LiquidCrystal.h>
#include <Wire.h>
/*
Pulse Period Measurement of 2 Flow Sensors
-------------------------------------------------------------------------------------
• accurate measurements by using Arduino micros() function
• determines leading edge of pulses without use of iterrupts
• calculates pulse periods, frequencies, flow rates, totalized volumes
• includes pulse generator for testing, simulation and troubleshooting
• easily adapted for testing other sensors and pulse quantities (energy, optic, etc.)
- dlloyd ----------------------------------------------------------------------------*/
LiquidCrystal lcd(2,3,4,5,10,11,12);
// constants
const byte pgenPin = 13; // PGEN pin number
const byte CLEAR = 12; // CLEAR pin number
const byte inputs[] = {6, 8}; // input pins 6 is inlet 8 is outlet
const double pulseConstant[] = {7.5, 7.5}; // pulse constants
const byte qty = sizeof(inputs);
// variables
double pgenFrequency = 7.5; // pulse generator frequency (Hz)
long previousMillis = 0;
long pgenInterval = 1000000 / (pgenFrequency * 2);
byte pgenState = LOW;
long pgenPreviousMicros = 0;
long startTime[qty];
byte inputsState[qty];
byte inputsPrevious[qty];
double pulsePeriod[qty];
double pulseFrequency[qty];
double pulseMultiplier[qty];
double flowRate[qty];
double pulseVolume[qty];
double totalVolume[qty];
byte h0[8] = {
B11111,
B01010,
B10101,
B01010,
B10101,
B01010,
B11111,
};
void setup() {
Serial.begin(9600);
Serial.print("");
lcd.begin(16,2);
lcd.clear();
pinMode(pgenPin, OUTPUT);
pinMode(CLEAR, INPUT_PULLUP);
for (int i = 0; i < qty; i++) {
pinMode(inputs[i], INPUT_PULLUP);
pulseVolume[i] = 1.0 / (pulseConstant[i] * 60);
pulseMultiplier[i] = 1.0 / (pulseConstant[i]);
//lcd.print(flowRate[i], 6); lcd.print(" m^3/sec, ");
//lcd.setCursor(0,1);
//lcd.print(flowRate[i+1], 6); lcd.print(" m^3/sec, ");
//delay(1000);
//lcd.clear();
}
clr(); // initialize arrays
}
void loop()
{
pgen(); // run pulse generator
if (digitalRead (CLEAR) == LOW) { // check CLEAR push button
clr();
}
pulseMeasure(); // run flow tests, calculations and print results
}
// functions -----------------------------------------------------
void pulseMeasure() {
for (int i = 0; i < qty; i++) {
inputsState[i] = digitalRead(inputs[i]); // read the inputs
if ((inputsState[i] == 1) && (inputsPrevious[i] == 0)) { // if rising
pulsePeriod[i] = (micros() - startTime[i]) * 0.000001; // test duration (sec)
pulseFrequency[i] = 1 / pulsePeriod[i]; // input frequency (Hz)
flowRate[i] = (pulseFrequency[i] * pulseMultiplier[i])*16.6666667; // 1.0 / pulseConstant[i] (L/min) (ml/sec)
totalVolume[i] = totalVolume[i] + pulseVolume[i]; // totalized volume (L)
if (millis() - previousMillis > 250) { // update interval (milliseconds)
Serial.print(i); Serial.print(": ");
Serial.print(pulsePeriod[i], 3); Serial.print(" sec, ");
Serial.print(pulseFrequency[i], 3); Serial.print(" Hz, ");
Serial.print(flowRate[i], 6); Serial.print(" ml/sec, ");
lcd.print(flowRate[i], 6); lcd.print(" ml/sec, ");
Serial.print(totalVolume[i], 6); Serial.print(" L");
Serial.println();
previousMillis = millis();
}
startTime[i] = micros();
}
inputsPrevious[i] = inputsState[i];
}
}
void pgen() {
// check to see if it's time to pulse the PGEN; that is, if the
// difference between the current time and last time you pulsed
// the PGEN is bigger than the interval to changed its output state.
unsigned long pgenCurrentMicros = micros();
if (pgenCurrentMicros - pgenPreviousMicros > pgenInterval) {
// save the last time you pulsed the PGEN
pgenPreviousMicros = pgenCurrentMicros;
// if the PGEN output is off turn it on and vice-versa:
if (pgenState == LOW)
pgenState = HIGH;
else
pgenState = LOW;
digitalWrite(pgenPin, pgenState);
}
}
void clr() {
for (int i = 0; i < qty; i++) {
startTime[i] = micros();
inputsState[i] = 0;
inputsPrevious[i] = 1;
pulsePeriod[i] = 0;
pulseFrequency[i] = 0;
flowRate[i] = 0;
totalVolume[i] = 0;
}
}
Someone have an idea?
(notice: I want to display on the screen only the data of flow rate)
Thank you