How to carry a value through loop untill changed

Good evening from Alberta, Canada everyone.
Hardware:
Arduino Uno
Flow Meter
LM34 Temp Sensor (Not an issue)
Pressure Transducer (Not an issue)
LCD 4 Row 20 Column Screen (Not an issue)

Purpose, I am trying to monitor the water flow rate/volume used for an experiment. I want to carry the difference in the current volume used from the previous volume used until the volume has changed again.

This way I can see the difference in volume usage between cycles of water flowing.

Where I am stuck, is I can get it to display the difference in volume from the previous cycle, but it only last once in the loop and then is written over to zero. I am drawing a blank on how to hold that value till it changes again.

See attached screenshot of values printed out, it does calculate the cycle volume, but then once goes through the loop resets to zero on me again.

I'm not asking for anyone to code for me, I am looking for guidance/suggestions on how to tackle this problem so I can learn/grow from this lesson.

Update, I exceed my 9000 characters, so I have just snipped the code that I am using, I hope this is enough to help me

newVolTotal = (totalFlowVol / 350);          // Takeing totalFlowVol value from ISR and storing it in newVolTotal
cycleVolT = (newVolTotal - previousVol);     // Takeing newVoltTotal and subtracting previousVol that was carried through the loop
previousVol = newVolTotal;                   // Variable to capture total volume flowed and pass through cycle

lastWaterCycle = cycleVolT;                  // Carrying forward last cycle volume used

// lastWaterCylce carries through loop for one cycle, then goes to zero

Yes, the posted code is not very helpful.

Where are you setting the thingy back to zero?

Is the variable that holds the previousvalue created in the loop() or???

Why are you printing multiple “Total” lines without any intervening “L/M” lines ? In some cases the “Total” Lines appear identical.
You can add the entire .ino sketch file to a post using the attach function.

Sorry everyone, I never thought about adding the actual .ino file
Between line 98-104 is the code I'm having trouble with,

the multiple "L/M" lines are because the flow meter "hall effect sensor" starts with an ISR on the falling edge and counts the pulses to trigger the liters per minute lines your seeing.

"Total" Lines appear identical" near the end of that picture the flow meter stopped moving, and the loop continues to print the values until they are increased

Line 123 pulscount = 0 is where I'm resetting the ISR value

Flow_Meter_Orig.ino (7.07 KB)

OP's code:

// good copy March3, 2021
#include <Wire.h>                        // Wire library for i2c
#include <LiquidCrystal_I2C.h>           // LCD I2C library 

// Pin Declartions
byte flowsensor = 2;                     // Water Meter sensor pin attached to alo Intreupt # 0
byte LM34TempSensorPin = A0 ;            // Temp Sensor Pin LM34
byte pressureSensor = A1;                // Setting pressure sensor pin

// Set the LCD address to 0x27 for a 20 chars and 4 line display and name lcdScreen
LiquidCrystal_I2C lcdScreen(0x27, 20, 4);

// Global Variables
volatile int pulseCount;                 // ISR, Measures flow sensor pulses
volatile float totalFlowVol;             // ISR, Measures flow volume
float flowRateLM;                        // Variable storage values
float previousVol;                       // Previous water volume value
float cycleVolT;                         // cycle Volume Total
float newVolTotal;                       // Storeing previousVol value
float lastWaterCycle;                    // Variable, not sure if needed yet, used for lastWaterCycleing

// Timing
unsigned long previousTime;              // Variable to store previous time value
unsigned long currentTime;               // Capturing currentTime in miliscondes
int interval = 1000;                     // Setting for 1 second delay

//void ICACHE_RAM_ATTR flow();             // Needed while useing the ESP8266, see additonal ISR notes.

// Interrupt Service Retuine (ISR)
void flow () {
  pulseCount++;     // Incremeant to count pulses to figure out volume flowed
  totalFlowVol++;   // Incremeant to keep track of total volume flowed
}

void setup() {
  // Setting Pin Modes
  pinMode(flowsensor, INPUT_PULLUP);     // Setting pinmode, using internal reister (debouce reason)
  pinMode(LM34TempSensorPin, INPUT);     // Not neccsiary to put input, just did for pratice
  pinMode(pressureSensor, INPUT_PULLUP); // Setting pinmode, using internal reister (debouce reason)

  // Starting Serial Mointor/LCD Screen
  Serial.begin(9600);                    // Used for debugging
  lcdScreen.begin();                     // initialize the LCD

  // Start up Display & Message
  lcdScreen.clear();
  lcdScreen.setCursor(0, 0);
  lcdScreen.print("Starting Up");
  lcdScreen.setCursor(0, 1);
  lcdScreen.print("Opening Valve");

  // Attaching interrupts
  attachInterrupt(digitalPinToInterrupt(flowsensor), flow, FALLING); // Setup Interrupt to catch sensor pulses on "falling" edge

} // End of Setup Loop

void loop () {
  currentTime = millis();                                      // Setting varibable with current run time

  if (currentTime >= (previousTime + interval)) {              // Making sure set time has gone by, set by "interval" variable
    previousTime = currentTime;                                // Updates previousTime for next cycle

    // Reading Temp with LM34 temp Sensor
    float lm34RawTemp = analogRead(LM34TempSensorPin);           // Setting up variable to read LM34TempSensor
    float temptoF = ((lm34RawTemp * 0.0048) / .010);             // Taking variable and converting to F based on LM34 data sheet
    float convertFtoC = ((temptoF - 32 ) * 5 / 9);               // Formula to convert F to C

    // Reading Pressure Sensor
    int pressureSensorReading = analogRead(pressureSensor);              // Reading Sensor Value
    int currentPressure = (pressureSensorReading * .0048);           // ADC & converting to Voltage based on 5v / 1024
    int adjustedPressure = (currentPressure / 5 * 100);                  // Taking Volage / 5 * 100 to get Psi

    if (pulseCount != 0) {                                     // If flow meter pulseCount oppsite zero, then do this code, water flowing
      lcdScreen.clear();                                       // Clearing LCD Screen

      // Caulating flow rate in liters/mintue, Pulses x 3.03mL x 60 sec / 1000mL
      float flowRateLM = (pulseCount * 3.5 * 60 / 1000);
      Serial.print((String)"L/M = " + flowRateLM);
      lcdScreen.setCursor(0, 0);
      lcdScreen.print((String)"L/M = " + flowRateLM);

      // Print Pulse Count for trouble shooting reasons, 350 = 1 litre
      Serial.println((String)" Pulse Count = " + pulseCount);
      lcdScreen.setCursor(0, 1);                               // Setting the Curser to Colume 0, row 2
      lcdScreen.print((String)"Pulse Count = " + pulseCount);

      // If Water flow exceeds 30 l/m close the valve and leave closed Unitll user reset.
      if (flowRateLM >= 30) {
       lcdScreen.setCursor(0, 3);                             // Setting the Curser to Colume 0, row 3
        lcdScreen.print("Valve Closed");                       // LCD Screen displaying Valve staus is closed
        // delay(5000);
        // digitalWrite(valveClose, LOW);
      } // End of if statment "flowRateLM"

    } // End of if statment "Pulsecount"

    // Displaying Information to Serial Montior or LCD Screen
    else {  // No Flow Else Statment
      newVolTotal = (totalFlowVol / 350);                            // Takeing totalFlowVol value from ISR and storing it in newVolTotal
      cycleVolT = (newVolTotal - previousVol);                       // Takeing newVoltTotal and subtracting previousVol that was carried through the loop
      previousVol = newVolTotal;                                     // Variable to capture total volume flowed and pass through cycle

      lastWaterCycle = cycleVolT;                                  // print out on screen

      // Serial Monitor Read out (debugging)
      Serial.print((String)" Total Vol " + newVolTotal + "L");       // Shows accumated litres since last reset
      Serial.print((String)" Cycle Vol " + lastWaterCycle);          // Show last cycle how many litres used
      Serial.print((String)" Temp C " + convertFtoC);                // Showing Temp in C on Serial Monitor of pipe
      Serial.println((String)" Press" + adjustedPressure + " Kpa");

      // Local LCD Screen read out.
      lcdScreen.clear();                                             // Clearing Screen
      lcdScreen.setCursor(0, 0);                                     // Setting the Cursor to Colume 0, row 0
      lcdScreen.print((String)"Total Vol " + newVolTotal + "L");     // Acumalted volume of water in litres used since last reset.
      lcdScreen.setCursor(0, 1);                                     // Setting the Curser to Colume 0, row 1
      lcdScreen.print((String)"Cycle Vol " + lastWaterCycle + "L" ); // Show last cycle how many litres used
      lcdScreen.setCursor(0, 2);                                     // Setting the Curser to Colume 0, row 2
      lcdScreen.print((String)"Temp C " + convertFtoC);
      lcdScreen.setCursor(0, 3);                                     // Setting the Curser to Colume 0, row 3
      lcdScreen.print((String)" Pres " + adjustedPressure + " Kpa");       // Showing valve postion, 0=Open 1=Closed
    } // End of Else Statment "No Flow

    pulseCount = 0;                                                  // Reseting ISR pulseCounter


  }// End of if satment "Currenttime"
} // End of Loop

And you are having issues with

 if (pulseCount != 0)
{
some code and some more code is in between these brackets
} else {  // No Flow Else Statment
      newVolTotal = (totalFlowVol / 350);                            // Takeing totalFlowVol value from ISR and storing it in newVolTotal
      cycleVolT = (newVolTotal - previousVol);                       // Takeing newVoltTotal and subtracting previousVol that was carried through the loop
      previousVol = newVolTotal;                                     // Variable to capture total volume flowed and pass through cycle

      lastWaterCycle = cycleVolT;                                  // print out on screen

      // Serial Monitor Read out (debugging)
      Serial.print((String)" Total Vol " + newVolTotal + "L");       // Shows accumated litres since last reset
      Serial.print((String)" Cycle Vol " + lastWaterCycle);          // Show last cycle how many litres used
      Serial.print((String)" Temp C " + convertFtoC);                // Showing Temp in C on Serial Monitor of pipe
      Serial.println((String)" Press" + adjustedPressure + " Kpa");

      // Local LCD Screen read out.
      lcdScreen.clear();                                             // Clearing Screen
      lcdScreen.setCursor(0, 0);                                     // Setting the Cursor to Colume 0, row 0
      lcdScreen.print((String)"Total Vol " + newVolTotal + "L");     // Acumalted volume of water in litres used since last reset.
      lcdScreen.setCursor(0, 1);                                     // Setting the Curser to Colume 0, row 1
      lcdScreen.print((String)"Cycle Vol " + lastWaterCycle + "L" ); // Show last cycle how many litres used
      lcdScreen.setCursor(0, 2);                                     // Setting the Curser to Colume 0, row 2
      lcdScreen.print((String)"Temp C " + convertFtoC);
      lcdScreen.setCursor(0, 3);                                     // Setting the Curser to Colume 0, row 3
      lcdScreen.print((String)" Pres " + adjustedPressure + " Kpa");       // Showing valve postion, 0=Open 1=Closed
    } // End of Else Statment "No Flow

    pulseCount = 0;                                                  // Reseting ISR pulseCounter


  }// End of if satment "Currenttime"

the part of the code where pulseCount is already equal to 0; correct?

And you want the part where the pulse count is not equal to 0 to retain the previous pulse count value, correct? It might be an idea to put the pulseCount into a previousPulseCount variable.

If the pulseCount, when it is zero is going to be set to zero then putting the previous pulse count in another variable might work. Except for when the previous pulseCount becomes 0.

Good evening Idahowalker, no I have no issues with resetting the pulsecount.

My issues are lastWaterCycle = cycleVolT
I'm trying to get the lastWaterCycle variable to hold the value and continue it through the loop without returning to zero, It holds the increase in volume value for one loop then goes to zero as there were no further increases.
If there are no further increases in volume, then I would like to hold that value so I can see it myself.

I thought I would use an "if" statement but not sure how to go about that.

if waterflow at this reading is equal to zero then do not put the value into the lastWaterCycle variable kind of thing?

if[mythingy !=0 )
{
lastWaterCycle = new value
}

or

if( myNewthingy > theoldthingy)
{
theOldthingy = myNewThingy;
}

Idahowalker: THANK YOU SO MUCH, I knew I was close but was just stuck on the wrong set of tracks mind-set-wise.

I appreciate you guiding me and not writing the code specifically out for my problem. This is such a greater value for me to learn this than others doing for me. Again THANK YOU

newVolTotal = (totalFlowVol / 350);             // Takeing totalFlowVol value from ISR and storing it in newVolTotal
cycleVolT = (newVolTotal - previousVol);        // Takeing newVoltTotal and subtracting previousVol that was carried through the loop
previousVol = newVolTotal;                      // Variable to capture total volume flowed and pass through cycle

if (cycleVolT > 0) {                            // If value is greater then zero, capture and update variable 
lastWaterCycle =  cycleVolT;
}

+1, you are welcome.

acode777:
I appreciate you guiding me and not writing the code specifically out for my problem. This is such a greater value for me to learn this than others doing for me.

Can we get this one framed, please?

K++

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