Water Dispenser With Hall Effect Flow Sensor - Troubleshooting

Greetings all,
First: thanks for viewing my first post here ever! My expertise level is "full-on-newbie."

I'm attempting to build a water dispenser that dispenses a user-configured amount (via keypad) and then shuts off via a solenoid valve after the hall-effect sensor has determined the target volume has been dispensed.

I'm using/borrowing some code written by @geoffism00 for their 'Water Hose Dispenser'. Their original topic was closed about a year ago so I cannot ask questions within the original thread and DM's appear to not be a thing, so here I am. I'm running into some verification issues and received the following error:

Arduino: 1.8.19 (Mac OS X), Board: "Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"

/Users/Kalevra_Goodcat/Dropbox/Arduino/WaterControl_HLT/WaterControl_HLT.ino: In function 'void loop()':
WaterControl_HLT:145:7: error: 'inNum' was not declared in this scope
       inNum *= 10;
       ^~~~~
/Users/Kalevra_Goodcat/Dropbox/Arduino/WaterControl_HLT/WaterControl_HLT.ino:145:7: note: suggested alternative: 'enum'
       inNum *= 10;
       ^~~~~
       enum
exit status 1
'inNum' was not declared in this scope

I've tried putting "int inNum;" in my declarations section (line 19) and that allows the code to compile but I'm not completely sure that's the correct solution (I'm still taking some intro C++ courses to better orient myself). I get an odd behaviour on the LCD where the values start at "0-48" continue to increment and end at "0-21840" without any action on my behalf. At this point, my keypad has not arrived so I'm hoping that issue is resolved once integrated into my prototype...but I can't help but think that the two issues are potentially related.

I've provided the code below with the 'inNum' declared on line 19 but commented out so the error can be reproduced. Without it, line 127 throws the aforementioned compiling error. Special note: Geofism00's original code does not declare "inNum" and given my newbie status I'm a little lost on this one.

Please see the code below (which is what I'm currently using):

/*==========ORIGINAL ARDUINO FORUM POST CREDIT TO GEOFFISM00==========*/
//https://forum.arduino.cc/t/water-hose-dispenser/653386

/*==========LIBRARIES==========*/
#include <Keypad.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);

/*==========MISC DECLARATIONS==========*/
unsigned long currentTime = 0;
byte data_count=0;
char Data[2];
int iGallons;
byte statusLed = 13;
byte sensorInterrupt = 0;  // 0 = digital pin 2
byte sensorPin = 2;
const byte pumpPin= 3;
//int inNum;

/*==========HALL-EFFECT FLOW SENSOR LTR/MN==========*/
float calibrationFactor = 4.5;
volatile byte pulseCount;
float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;
unsigned long oldTime;

/*==========KEYPAD SETTINGS==========*/
const byte ROWS = 4; 
const byte COLS = 4; 
char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {11, 10, 9, 8}; 
byte colPins[COLS] = {7, 6, 5, 4}; 

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup()
{

  // Initialize a serial connection for reporting values to the host
  Serial.begin(9600);
  
  pinMode(pumpPin, INPUT_PULLUP);
  pinMode(pumpPin, OUTPUT);
  digitalWrite(pumpPin, HIGH);
          
  //if 15 seconds go by and Data[0] is still 0 
  //and not a # the system will auto set to 0 and be on constantly
  Data[0]='0';
  iGallons=0;

  // Set up the status LED line as an output
  pinMode(statusLed, OUTPUT);
  digitalWrite(statusLed, HIGH);  // We have an active-low LED attached

  pinMode(sensorPin, INPUT);
  digitalWrite(sensorPin, HIGH);

  pulseCount        = 0;
  flowRate          = 0.0;
  flowMilliLitres   = 0;
  totalMilliLitres  = 0;
  oldTime           = 0;

  // The Hall-effect sensor is connected to pin 22 which uses interrupt 0.
  // Configured to trigger on a FALLING state change (transition from HIGH
  // state to LOW state)
  attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
  lcd.init();
  lcd.backlight();
}

void loop()
{
  lcd.setCursor(0,0);
  lcd.print("Gallons: ");
 
  char key = customKeypad.getKey();
  detachInterrupt(sensorInterrupt);
  flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
  oldTime = millis();
  flowMilliLitres = (flowRate / 60) * 1000;
  totalMilliLitres += flowMilliLitres;
  unsigned int frac;

  if(Data[0]=='0' && currentTime>10)
  {
    lcd.clear();
    lcd.setCursor(2,0);
    // Print the cumulative total of litres flowed since starting
    lcd.print("Quart: ");
    lcd.print((totalMilliLitres * 0.00105669));
    lcd.setCursor(2,1);
    lcd.print( "Liters: ");
    lcd.print((flowRate / 60));
    lcd.setCursor(2,2);
    lcd.print("Gallons: ");
    lcd.print(totalMilliLitres / 3785.41);
  }
  else 
  {
    if(key=='#')
    {
      lcd.clear();
      
      unsigned long curGallons=totalMilliLitres / 3785.41;
      iGallons=atoi(Data);
      lcd.print(curGallons);
      lcd.print(" of ");
      lcd.print(iGallons);

      if(curGallons>=iGallons)
      {
          digitalWrite(pumpPin, LOW);
      }
    }
    else
    {   
      int c = (int)key - 48;
      inNum *= 10;
      inNum += c;
    
    Data[data_count] = inNum;
      lcd.print(atoi(Data)); 
      lcd.print(inNum);
      data_count++;
    }
  }
  
 /*==========RESET PULSE COUNTER TO INCREMENT AGAIN==========*/ 
  pulseCount = 0;

    // Enable the interrupt again now that we've finished sending output
  attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
  currentTime++;
  delay(1000); 
}

/*==========INTERRUPT SERVICE ROUTINE==========*/
void pulseCounter()
{
  // Increment the pulse counter
  pulseCount++;
}

Not sure if it's important to list out what I'm using but in case: I'm using a mega board for prototyping purposes only (until a smaller board arrives), a hall-effect flow sensor, and an electric solenoid valve controlled by a MOSFET (controlled by pin 3). At the point of writing this I'm still awaiting the arrival of the keypad so have not had a chance to test that functionality out as of yet.

In advance, I'm grateful for any help/wisdom/direction.

1 Like

I don't see the condition for zeroing inNum.
If key is not '#', even no key press, won't inNum simply keep incrementing?

Are you 100%, cross your heart and hope to die, sure you copied ALL code correctly?

At the risk of being laconic: yes. :wink:

This is the immediate cause of your problem:

//int inNum;

Uncomment that line and it will compile. Whether it will work, I doubt, based on what others have already observed.

@anon73444976 quick question since I'm not that far along with my C++ understanding, what would zeroing inNum look like and where within the code would that logically live? Many thanks.

@wildbill forgot to @ you in my original reply. I had added the //int inNum; as the original code from @geoffism00 did not include it. Unsure it's correct but to your point it allows the code to compile for better or worse =)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.