URGENT! Flowmeter stops calculating after pressing # key

This code is supposed to calculate volume based on user input to the key pad but after pressing the "#" key the flowmeter stops registering values in the serial monitor.

#include <LiquidCrystal.h> //include LCD library (standard library)
#include <Keypad.h> //include keypad library - first you must install library (library link in the video description)

const byte rows = 4; //number of the keypad's rows and columns
const byte cols = 4;

char keyMap [rows] [cols] = { //define the cymbols on the buttons of the keypad

  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins [rows] = {3, 4, 5, 6}; //pins of the keypad
byte colPins [cols] = {7, 8, 9, 10};
byte statusLed    = 13;

byte sensorInterrupt = 0;  // 0 = digital pin 2
byte sensorPin       = 2;

// The hall-effect flow sensor outputs approximately 4.5 pulses per second per
// litre/minute of flow.
float calibrationFactor = 7.5;

volatile byte pulseCount;  

float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;

unsigned long oldTime;

void pulseCounter()
{
  // Increment the pulse counter
  pulseCount++;
}

long Num1,Number;
char key,action;
boolean result = false;
float constant = 0.623;


Keypad myKeypad = Keypad( makeKeymap(keyMap), rowPins, colPins, rows, cols);

LiquidCrystal lcd(A0, A1, A2, A3, A4, A5); 

byte raindrop[8] = {
  0b00100,
  0b00100,
  0b01110,
  0b01110,
  0b11111,
  0b11111,
  0b11111,
  0b01110
};
void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.createChar(0, raindrop);
  lcd.setCursor(2, 0);
  lcd.write(byte(0));
  lcd.print("WaterRight");
  lcd.write(byte(0));
  delay(2000);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Enter Area:");
  
  Serial.begin(38400); //start of flow meter code

  // 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 2 which uses interrupt 0.
  // Configured to trigger on a FALLING state change (transition from HIGH
  // state to LOW state)
  attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}

void loop() {

key = myKeypad.getKey(); //storing pressed key value in a char

if (key!=NO_KEY)
DetectButtons();

if (result==true)
CalculateVolume();

DisplayResult();   

}

void DetectButtons() {
    lcd.clear(); //Then clean it
    if (key=='*') {
      Number=Num1=0; 
      result=false;
      }
    
    if (key == '0'){
    if (Number==0)
      Number=0;
    else
      Number = (Number*10) + 0; //Pressed twice
    }
    
    if (key == '1'){
    if (Number==0)
      Number=1;
    else
      Number = (Number*10) + 1; //Pressed twice
    }

    if (key == '2'){
    if (Number==0)
      Number=2;
    else
      Number = (Number*10) + 2; //Pressed twice
    }
    
    if (key == '3'){
    if (Number==0)
       Number=3;
    else
       Number = (Number*10) + 3; //Pressed twice
    }
    
    if (key == '4'){
    if (Number==0)
      Number=4;
    else
      Number = (Number*10) + 4; //Pressed twice
    }

    if (key == '5'){
    if (Number==0)
      Number=5;
    else
      Number = (Number*10) + 5; //Pressed twice
    }
    
    if (key == '6'){
    if (Number==0)
      Number=6;
    else
      Number = (Number*10) + 6; //Pressed twice
    }
    
    if (key == '7'){
    if (Number==0)
      Number=7;
    else
      Number = (Number*10) + 7; //Pressed twice
    } 
    
     if (key == '8'){
     if (Number==0)
    Number=8;
    else
    Number = (Number*10) + 8; //Pressed twice
    }   

    if (key == '9'){
    if (Number==0)
      Number=9;
      Number = (Number*10) + 9; //Pressed twice
    }  

  
    if(key=='#'){
      flowrate();
   while(Number*constant>totalMilliLitres ){
      lcd.clear();
      if(totalMilliLitres==0){
        lcd.print("Turn On Water");
        delay(10);
      }
      if(totalMilliLitres>0){
      lcd.print("Watering..."); 
      lcd.setCursor(0,1);
      lcd.print("Gal Out:");
      lcd.print(totalMilliLitres);
      }
      delay(1000);
     
    if(Number*constant<=totalMilliLitres ){
    lcd.clear();
    lcd.print("Done Watering");
    delay(5000);
    }
    }
}
}

   
void CalculateVolume(){
   
}
void DisplayResult()
{
  lcd.setCursor(0, 0);
  lcd.print("Enter Area:");   
  lcd.print(Number); 
  lcd.setCursor(0, 1);  
  lcd.print("Gal:");
  lcd.print(Number*constant); 
}

  
void flowrate()
{
   
   if((millis() - oldTime) > 1000)    // Only process counters once per second
  { 

    detachInterrupt(sensorInterrupt);  
    flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
    oldTime = millis();
    flowMilliLitres = (flowRate / 60) * 1000;
    totalMilliLitres += flowMilliLitres;
    unsigned int frac;
    
    // Print the flow rate for this second in litres / minute
    Serial.print("Flow rate: ");
    Serial.print(int(flowRate));  // Print the integer part of the variable
    Serial.print(".");             // Print the decimal point
    // Determine the fractional part. The 10 multiplier gives us 1 decimal place.
    frac = (flowRate - int(flowRate)) * 10;
    Serial.print(frac, DEC) ;      // Print the fractional part of the variable
    Serial.print("L/min");
    // Print the number of litres flowed in this second
    Serial.print("  Current Liquid Flowing: ");             // Output separator
    Serial.print(flowMilliLitres);
    Serial.print("mL/Sec");

    // Print the cumulative total of litres flowed since starting
    Serial.print("  Output Liquid Quantity: ");             // Output separator
    Serial.print(totalMilliLitres);
    Serial.println("mL"); 
      
  }

    // Reset the pulse counter so we can start incrementing again
    pulseCount = 0;
    
    // Enable the interrupt again now that we've finished sending output
    attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
  
}

"URGENT!" for who?

Perhaps it is because you change the baud rate from 9600 to 38400 without changing the setting on your PC program.

Paul

   while(Number*constant>totalMilliLitres ){

I can't see anything in this while loop that changes the values of any of Number, constant or totalMilliLitres so there's no way this loop will ever terminate.

Also, this statement inside the while loop:

if(Number*constant<=totalMilliLitres ) {

will never be true.

Pete

el_supremo:
while(Number*constant>totalMilliLitres)

I can't see anything in this while loop that changes the values of any of Number, constant or totalMilliLitres so there's no way this loop will ever terminate.

Also, this statement inside the while loop:
if(Number*constant<=totalMilliLitres ) {
will never be true.

1. Number changes with key pressed from Keypad; constant = 0.623; totalMilliLitres (initially 0) is a function of pulseCount which changes as the pulses arrive from sensor on interrupt basis. Therefore, the argument of while() loop may turn to false at some instant?

2. Initially, totalMilliLitres=0; pressing '' from Keypad places 0 (zero) into Number. Therefore, Numberconstatnt (0*0.623 = 0) = totalMilliLitres (0). Therefore, the argument of if() structure may become true?

Perhaps it is because you change the baud rate from 9600 to 38400 without changing the setting on your PC program.

Yes! This seems to be the problem. With two different settings of the Bd and of the pressing of '#' key bring garbage on the Serial Monitor. When the Bds are made equal and the '#' key is pressed, the Serial Monitor shows meaningful messages.

No. The only thing that changes when an interrupt occurs is pulseCount and there's nothing in that while loop which uses pulseCount directly.
That while loop will not terminate.

Pete

Please don’t use URGENT! in you titles.

    if (key == '1'){
    if (Number==0)
      Number=1;
    else
      Number = (Number*10) + 1; //Pressed twice
    }

THis code is nonsense. If Number is 0, multiplying it by 10 will make it 0. So, the test for 0 is absolutely worthless.

Testing to see if key is n, then adding n is dumb. Just add key - '0', if key is between '0' and '9', inclusive.

Two lines of code, instead of 60.

Thank you all for the replies(well the ones that are helpful at least) but is there a solution to fix the issue?

jhope:
Thank you all for the replies(well the ones that are helpful at least) but is there a solution to fix the issue?

Yes, of course.
Haven't you been following?