Hello, Im in confuse about my code.
So the goal is to open the Solenoid valve when the distance is under 20cm, While solenoid opens, the waterflow sensor read the flow until 2000mL reached, then the solenoid valve closed and ultrasonic sensor continue to read the distance.
the problem is whenever the distance is under 20cm the valve opens but ultrasonic sensor keeps reading distance so whenever the waterflow reached 2000mL, the valve wont close. Or if the object moved to the distance more than 20cm the valve immidiately closed. Even though the waterflow is not reading 2000mL yet.
Here's my code:
#include <ezButton.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2);
ezButton button (7);// Flush Button
#define echoPin 4 //Pin echo ultrasonic
#define trigPin 5 //Pin trigger ultrasonic
long duration, cm; //variabel untuk durasi ultrasonic
int distance; //variabel untuk pengukuran jarak
int inPin = 10; // pin Flush Button
int buttonVal = 0; // Value Flush Button
//int i=0;
/////////////////////PARAMETER FLOW METER///////////////////////
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 = 4.5;
volatile byte pulseCount;
float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;
unsigned long oldTime;
//////////////////////////////////////////////////////////////
void setup() {
// INIT LCD
lcd.init();
lcd.backlight();
lcd.clear();
// Initiate Ultra Sonic
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(38400);
// INIT RELAY
pinMode (8, OUTPUT);
//INIT FLUSH BUTTON
pinMode(inPin, INPUT);
/*
lcd.print("LOADING...");
delay (3000);
lcd.setCursor (0,0);
lcd.print("AUTO VALVE");
lcd.setCursor (0,1);
lcd.print("BY:DEYAPARINATA");
delay(3000);
lcd.clear();
*/
//INIT FLOW METER
pinMode (8, OUTPUT);
digitalWrite(8,LOW);
Serial.println ("Low");
// Initialize a serial connection for reporting values to the host
Serial.begin(38400);
// 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;
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}
void pulseCounter()
{
// Increment the pulse counter
pulseCount++;
}
void ultraSonic()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
cm = (duration/2) / 29.1;
Serial.println(cm);
lcd.setCursor(0,0);
}
void flowMeter()
{
///////////////////////////////////////FLOWMETER/////////////////////////////////
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;
// Add the millilitres passed in this second to the cumulative total
totalMilliLitres += flowMilliLitres;
unsigned int frac;
// Print the flow rate for this second in litres / minute
Serial.print("Flow rate: ");
Serial.print(int(flowRate));
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: ");
Serial.print(flowMilliLitres);
Serial.print("mL/Sec");
// Print the cumulative total of litres flowed since starting
Serial.print(" Output Liquid Quantity: ");
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 have finished sending output
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}
}
void loop() {
//////////////////////////////////////FLUSH/////////////////////////////////
button.loop(); //EZButton Library
//Jika Tombol ditekan, maka relay (pin 10) terbuka selama flow > 0
if (button.isReleased()){
flowMeter();
lcd.println("Button Pressed");
if (flowMilliLitres < 5){
digitalWrite(8,LOW);
}else {
digitalWrite(8,HIGH);
}
}
//////////////////////////////////////////////////////////////////////////
if (cm<20){
digitalWrite(8, HIGH);
flowMeter();
} else {
digitalWrite(8, LOW);
ultraSonic();
}
}