Error with flow sensor

I am trying to program a flow sensor to measure the flow of water through a sensor using an Arduino. I've gotten help with writing the code, but it's showing this error:

/private/var/folders/0v/jtgkxs655dbg5ngbhf3jnlc40000gn/T/.arduinoIDE-unsaved2024917-79863-10xs1hm.geym/sketch_oct17a/sketch_oct17a.ino: In function 'void loop()':
/private/var/folders/0v/jtgkxs655dbg5ngbhf3jnlc40000gn/T/.arduinoIDE-unsaved2024917-79863-10xs1hm.geym/sketch_oct17a/sketch_oct17a.ino:16:18: error: 'timeWhenLastCalcDone' was not declared in this scope
if ((timeNow - timeWhenLastCalcDone) >= calcDelay) {
^~~~~~~~~~~~~~~~~~~~
/private/var/folders/0v/jtgkxs655dbg5ngbhf3jnlc40000gn/T/.arduinoIDE-unsaved2024917-79863-10xs1hm.geym/sketch_oct17a/sketch_oct17a.ino:16:43: error: 'calcDelay' was not declared in this scope
if ((timeNow - timeWhenLastCalcDone) >= calcDelay) {
^~~~~~~~~
/private/var/folders/0v/jtgkxs655dbg5ngbhf3jnlc40000gn/T/.arduinoIDE-unsaved2024917-79863-10xs1hm.geym/sketch_oct17a/sketch_oct17a.ino:17:5: error: 'timeWhenLastCalcDOne' was not declared in this scope
timeWhenLastCalcDOne = timeNow;
^~~~~~~~~~~~~~~~~~~~
/private/var/folders/0v/jtgkxs655dbg5ngbhf3jnlc40000gn/T/.arduinoIDE-unsaved2024917-79863-10xs1hm.geym/sketch_oct17a/sketch_oct17a.ino: At global scope:
/private/var/folders/0v/jtgkxs655dbg5ngbhf3jnlc40000gn/T/.arduinoIDE-unsaved2024917-79863-10xs1hm.geym/sketch_oct17a/sketch_oct17a.ino:33:1: error: expected declaration before '}' token
}
^

exit status 1

Compilation error: 'timeWhenLastCalcDone' was not declared in this scope

Here is my code below:

const byte flowPinA = 2; //This is the input pin on the Arduino
double flowRateA; //Value to be calculated
volatile int countA; //Integer needs to be set as volative to ensure it updates correctly during interrupt process

void setup() {
    //setup code here, runs once
  Serial.begin(115200);
  pinMode(flowPinA, INPUT);      //Sets the pin as input
  attachInterrupt(digitalPinToInterrupt(flowPinA), FlowA, RISING);  //Set interrupt for pin 2 (Interrupt pin 0)
}

void loop() {
    //main code to run repeatedly
  countA = 0;    //makes counter start at 0
  unsigned long timeNow = millis();
  if ((timeNow - timeWhenLastCalcDone) >= calcDelay) {
    timeWhenLastCalcDOne = timeNow;
  interrupts();  //enables interrupts
  noInterrupts(); //Disable the interrupts on the Arduino
      //math for useful readings
    
  flowRateA = (countA * 2.25);    //Count pulses in last second and multiply by 2.25mL
  flowRateA = flowRateA * 60;     //Convert seconds to minutes, giving mL / minute
  flowRateA = flowRateA / 1000;   //Convert mL to Liters, giving L / minute
  Serial.println(flowRateA);     //Print the variable flowRate to Serial
  }
}

void FlowA(){
  countA++;    //every time function runs, increase count by 1
}

}

You can add line:
unsigned long timeWhenLastCalcDOne = 0;
at the beginning of the sketch.

But I expect that you need additional help with your code...

Hello emmynm

Check and test these mods.

const byte flowPinA = 2; //This is the input pin on the Arduino
double flowRateA; //Value to be calculated
volatile int countA; //Integer needs to be set as volative to ensure it updates correctly during interrupt process
uint32_t timeWhenLastCalcDOne;

void setup() {
    //setup code here, runs once
  Serial.begin(115200);
  pinMode(flowPinA, INPUT);      //Sets the pin as input
  attachInterrupt(digitalPinToInterrupt(flowPinA), FlowA, RISING);  //Set interrupt for pin 2 (Interrupt pin 0)
}

void loop() {
    //main code to run repeatedly

  unsigned long timeNow = millis();
  if ((timeNow - timeWhenLastCalcDone) >= calcDelay) {
    timeWhenLastCalcDOne = timeNow;
  
  noInterrupts(); //Disable the interrupts on the Arduino
  //math for useful readings
  flowRateA = (countA * 2.25);    //Count pulses in last second and multiply by 2.25mL
  //makes counter start at 0
  countA = 0; 
  interrupts();  //enables interrupts
  
  flowRateA = flowRateA * 60;     //Convert seconds to minutes, giving mL / minute
  flowRateA = flowRateA / 1000;   //Convert mL to Liters, giving L / minute
  Serial.println(flowRateA);     //Print the variable flowRate to Serial
  }
}

void FlowA(){
  countA++;    //every time function runs, increase count by 1
}

hth

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