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
}
}