Hi ..
I'm having a bit of trouble, how to reset the total volume with a button from the code below?
can someone tell me ?
[image]
#include <SPI.h>
#include <Wire.h>
#define SENSOR 5
long TimeNowOut = 0;
long previousMillis = 0;
int interval = 1000;
float calibrationFactor = 6.6;
volatile byte pulseCount;
byte pulse1Sec = 0;
float flowRate;
unsigned long flowMilliLitres;
unsigned int totalMilliLitres;
float flowLitres;
float totalLitres;
void IRAM_ATTR pulseCounter()
{
pulseCount++;
}
void setup()
{
Serial.begin(115200);
digitalWrite(13, 0);
delay(10);
pinMode(SENSOR, INPUT_PULLUP);
pulseCount = 0;
flowRate = 0.0;
flowMilliLitres = 0;
totalMilliLitres = 0;
previousMillis = 0;
attachInterrupt(digitalPinToInterrupt(SENSOR), pulseCounter, FALLING);
}
void loop()
{
static byte PulseState = LOW, ResetPulseState = LOW;
TimeNowOut = millis();
if (TimeNowOut - previousMillis > interval)
{
pulse1Sec = pulseCount;
pulseCount = 0;
flowRate = ((1000.0 / (millis() - previousMillis)) * pulse1Sec) / calibrationFactor;
previousMillis = millis();
flowMilliLitres = (flowRate / 60) * 1000;
flowLitres = (flowRate / 60);
totalMilliLitres += flowMilliLitres;
totalLitres += flowLitres;
// Print the flow rate for this second in litres / minute
Serial.print("Flow rate: ");
Serial.print(float(flowRate)); // Print the integer part of the variable
Serial.print("L/min");
Serial.print("\t"); // Print tab space
// Print the cumulative total of litres flowed since starting
Serial.print("Output Liquid Quantity: ");
Serial.print(totalMilliLitres);
Serial.print(" mL / ");
Serial.print(totalLitres);
Serial.println(" L");
}
}```
This code in setup() is pointless - they're all already zero
Which pin is your switch attached to?
i can add later, let's say
...
const int buttonPin = 2;
or can you help me change the code by measuring the total water rate and total volume and then resetting it with a button,
Hello fachromiez
Here comes a pseudo code to be adapted to your needs.
if (buttonBecomesPresses) volume=0;
Have a nice day and enjoy coding in C++.
I have tried your code but it doesn't work
It wasn't meant to "work"; as stated, it was pseudo code.
Take a look at the State Change Detection example in the IDE to see how buttonBecomesPressed could be implemented.
Hello
Do you know the meaning of pseudo code and adaptions?
do you mean like this?
totalMilliLitres += flowMilliLitres;
totalLitres += flowLitres;
// Print the flow rate for this second in litres / minute
Serial.print("Flow rate: ");
Serial.print(float(flowRate)); // Print the integer part of the variable
Serial.print("L/min");
Serial.print("\t"); // Print tab space
// Print the cumulative total of litres flowed since starting
Serial.print("Output Liquid Quantity: ");
Serial.print(totalMilliLitres);
Serial.print(" mL / ");
Serial.print(totalLitres);
Serial.println(" L");
if (buttonState == HIGH) {
// set totalLitres
totalLitres = 0;```
No, because you'll be using INPUT_PULLUP, so the switch input will normally be HIGH.
You'll be looking for the switch going from HIGH to LOW .
Sorry please don't ask me more, if you give me a revision of my code I will try to understand. I'm just trying to learn to understand this arduino. can you give me a solution from my code so i can reset the volume of running water stream with a button ?
Hello fachromiez
Run some IDE examples to get familar with coding and organiztion of Arduino sketches.
Have a nice day and enjoy coding in C++.
JCA34F
February 18, 2022, 3:56pm
13
Did you wire the button between pin 2 and Vcc or pin 2 and GND?
system
Closed
August 17, 2022, 3:56pm
14
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.