// YFS201 Flow Sensor Pulse Count Example
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int sensorPin = 2; // digital input pin connected to the YFS201 sensor
unsigned int pulseCount = 0; // variable to hold the pulse count
const int buttonPin1 = 12;
int buttonState1 = 0;
const int SolenoidPumpPin=11;
bool PumpRunning = false;
void setup() {
Serial.begin(9600); // initialize serial communication at 9600 baud rate
pinMode(buttonPin1, INPUT); // set the buttonPin1 pin as input
pinMode(sensorPin, INPUT); // set the sensor pin as input
pinMode(SolenoidPumpPin, OUTPUT);
digitalWrite(SolenoidPumpPin, LOW);
lcd.init();
lcd.clear();
lcd.backlight();
}
void loop()
{
if(pulseCount<=10)
{
buttonState1 = digitalRead(buttonPin1);
Serial.print("buttonState1 = "); Serial.println(buttonState1);
if (buttonState1 == HIGH)
{
PumpRunning = true;
digitalWrite(SolenoidPumpPin, HIGH);
}
if (PumpRunning)
{
if (digitalRead(sensorPin) == HIGH)
{ // check if the sensor output is high
pulseCount++; // increment the pulse count
Serial.print("Pulse Count: ");
Serial.println(pulseCount); // print the pulse count to the serial monitor
lcd.setCursor(0, 0);
lcd.print(pulseCount);
}
delay(500); // delay to allow for accurate pulse counting
}
}
else
{
digitalWrite(SolenoidPumpPin, LOW);
PumpRunning = false;
pulseCount = 0;
}
}