Display on LCD the results of two flow meter sensors

Hello everyone

Sorry about my English..
I have to finish my final project so please try to help me :slight_smile:

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 it displays only some rectangles

#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

Did it thank :slight_smile:

Right.

Now, the question is - have you ever managed to operate this display at all? No point writing a ton of complex code first and only then trying it out.

A line of blocks means that the LCD is not responding at all (though you may also need to tweak the contrast).

You have included pin 12 in the descriptor for the LCD but appear to be using it for something else. What's the deal?

You do not need lcd.clear(); after lcd.begin(16,2); thought that probably will not upset matters.

Start with making a test version by stripping the code back to only include the references to the LCD and see if you can just get it to print a line - then another line - in setup() with nothing in loop().

Also - You have seven pins listed in the descriptor. That is OK if you really have the LCD R/W pin connected to Arduino pin 3 but most of us just connect R/W to GND.

If you do connect R/W to GND you can remove pin 3 from the constructor and use that pin to implement the extra function that seems to be associated with pin 12.

Don

Thank you.

at first, I was trying to display only somting like "Hello" and then I got that blocks too.
Second, it is the first time that i'm using the LCD screen, so I connected it like it displayed in this video:

Notice I just want to display the flow rate that I've measured from the sensor

Should I buy another screen? or can it be something else?

hfrie14:
at first, I was trying to display only something like "Hello" and then I got that blocks too.

So when it did not work, you figured it would work if you just added more confusing code?

Wow! That is really classy technique! :grinning:

hfrie14:
Second, it is the first time that I'm using the LCD screen, so I connected it like it displayed in this video

Did you actually? You haven't shown us, so we can't be sure.

hfrie14:
Should I buy another screen? or can it be something else?

No, you need to read up on how to connect the display and use the correct descriptor. Which is what floresta has been explaining. You need to find a proper tutorial. I suppose this one is not too bad.

Oh my goodness!

I just looked at that YouTube video! Once seen, cannot be un-seen! It starts with the most dreadful ham-fisted job of soldering imaginable. :astonished:

Nevertheless, the code referenced and the connections are correct. Well, that is, I have no idea what he is doing with those two resistors. A single 470 ohm from VO pin 3 to ground will generally work fine.

So - how have you managed to get the numbers in the descriptor exactly backwards? You have

LiquidCrystal lcd(2,3,4,5,10,11,12);

instead of

LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);

That is truly remarkable!

And you have not explained to what your other references in the code to pin 12 correspond. If you must use pin 12 for something else, you need the descriptor

LiquidCrystal lcd(11, 10, 5, 4, 3, 2);

and connect pin 11 of the Arduino to LCD RS pin 4, pin 10 of the Arduino to LCD Enable pin 6 and LCD RS pin 5 to ground.