#include <LiquidCrystal_I2C.h>
#include <FlowSensor.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
#define sig_1 2
#define sig_2 3
#define Pulse_1 4
#define Pulse_2 5
#define button_1 13
#define adj_1 A0
#define adj_2 A1
// Timer_2 variables
int trigger;
int cycle;
const long duration_2 = 300000; // milliseconds
long offset_2;
unsigned long currentMillis;
// Timer_1 variables
byte a;
byte b;
const long duration_1 = 1000; // milliseconds
long offset_1;
// Timer_3 variables
byte c;
byte d;
const long duration_3 = 1000; // milliseconds
long offset_3;
// Button variables
int page;
byte watchdog_5;
// Flow sensor variables
#define type YFS201
FlowSensor Sensor_1(type, sig_1);
FlowSensor Sensor_2(type, sig_2);
int Limit_1;
int Limit_2;
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
delay(2000);
lcd.clear();
Sensor_1.begin(count_1);
Sensor_2.begin(count_2);
pinMode(Pulse_1, OUTPUT);
pinMode(Pulse_2, OUTPUT);
pinMode(button_1, INPUT_PULLUP);
digitalWrite(Pulse_1, LOW);
digitalWrite(Pulse_2, LOW);
lcd.noBacklight();
trigger = 0;
cycle = 0;
currentMillis = millis();
a = false;
b = false;
}
void loop() {
Display();
Button();
timer_1();
timer_3();
timer_2();
int g = analogRead(adj_1);
int h = analogRead(adj_2);
Limit_1 = map(g, 0, 1023, 0, 40);
Limit_2 = map(h, 0, 1023, 0, 40);
Sensor_1.read();
if(Sensor_1.getVolume() >= Limit_1) {
a = true;
Sensor_1.resetVolume();
}
Sensor_2.read();
if(Sensor_2.getVolume() >= Limit_2) {
c = true;
Sensor_2.resetVolume();
}
}
void count_1() {
Sensor_1.count();
}
void count_2() {
Sensor_2.count();
}
void Button() {
if(digitalRead(button_1) == LOW){
watchdog_5 = true;
}
else if((digitalRead(button_1) == HIGH) && (watchdog_5 == true)){
watchdog_5 = false;
trigger = 1;
}
}
void timer_1() {
currentMillis = millis();
if(a == true) {
offset_1 = currentMillis;
a = false;
}
if(currentMillis < (offset_1 + duration_1)) {
b = true;
digitalWrite(Pulse_1, HIGH);
}
if(currentMillis > (offset_1 + duration_1)) {
b = false;
digitalWrite(Pulse_1, LOW);
}
}
void timer_3() {
currentMillis = millis();
if(c == true) {
offset_3 = currentMillis;
c = false;
}
if(currentMillis < (offset_3 + duration_3)) {
d = true;
digitalWrite(Pulse_2, HIGH);
}
if(currentMillis > (offset_3 + duration_3)) {
d = false;
digitalWrite(Pulse_2, LOW);
}
}
void timer_2() {
currentMillis = millis();
if(trigger == 1) {
offset_2 = currentMillis;
trigger = 0;
}
if(currentMillis < (offset_2 + duration_2)) {
cycle = 1;
}
if(currentMillis > (offset_2 + duration_2)) {
cycle = 0;
}
}
void Display() {
if(cycle == 1) {
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Limit 1: ");
lcd.print(Limit_1);
lcd.print("L ");
lcd.setCursor(0,1);
lcd.print("Limit 2: ");
lcd.print(Limit_2);
lcd.print("L ");
lcd.setCursor(0,2);
lcd.print("Ch.1: ");
lcd.print(Sensor_1.getVolume(), 3);
lcd.print("L ");
lcd.setCursor(0,3);
lcd.print("Ch.2: ");
lcd.print(Sensor_2.getVolume(), 3);
lcd.print("L ");
}
else if(cycle == 0) {
lcd.noBacklight();
lcd.clear();
}
}