Below is the libraries needed to compile the code for the Water Flow Controller
Below is the code needed for the Water Flow Controller
#include <Wire.h>
//#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
#include <TFT_eSPI.h> // Hardware-specific library
#include <SPI.h>
TFT_eSPI tft = TFT_eSPI(); // Invoke custom library
#define EEPROM_SIZE 12
int T0Sensetivity = 25,
T2Sensetivity = 25,
T3Sensetivity = 25,
T8Sensetivity = 20;
bool showTouch = false;
volatile int flowTicks; // Measures flow sensor pulses
int flowMeter = 26,
valve = 21,
pulseBtn = 25,
EEPROM_Adr = 0;
float targetLiter = 0,
totalVol,
ppl = 900, targetPulses;
bool valveActiveState = HIGH,
btnRead = true,
lastRunStat = true,
running = false,
saving = false,
saved = false,
inching;
unsigned long lastUpdate;
void flow() { // Interrupt function
flowTicks++;
}
void Stop() { // Interrupt function
detachInterrupt(flowMeter);
Serial.println("stopBtn Preessed");
digitalWrite(valve, LOW);
// delay(100);
// delay(100);
//Serial.println("sensor inactive");
running = false;
}
void setup() {
Serial.begin(115200);
EEPROM.begin(EEPROM_SIZE);
pinMode(valve, OUTPUT);
digitalWrite(valve, LOW);
pinMode(flowMeter, INPUT_PULLUP);
pinMode(pulseBtn, INPUT_PULLUP);
// Use this initializer if using a 1.8" TFT screen:
tft.init(); // initialize a ST7735S chip
tft.setRotation(2);
tft.fillScreen(TFT_BLACK);
//lcd.begin();
if (touchRead(T2) < T2Sensetivity) {
tft.setTextSize(1.5);
tft.println("Resetting");
tft.println("Release button");
while (touchRead(T2) < 30) {
}
targetLiter = 5.2;
EEPROM.writeFloat(EEPROM_Adr, targetLiter);
EEPROM.commit();
//EEPROM.put(0, targetLiter);
delay(1000);
} else {
EEPROM.get(EEPROM_Adr, targetLiter);
//targetLiter = EEPROM.readFloat(0);
}
Serial.println(targetLiter);
touchAttachInterrupt(T2, Stop, 30);
defaultScreen();
//delay(500);
}
void loop() {
if (checkButtons()) {
//delay(1000);
}
// if (millis() >= lastUpdate + 50) {
lastUpdate = millis();
//lcd.clear();
//tft.fillScreen(TFT_BLACK);
tft.setTextColor(TFT_BLUE, TFT_BLACK);
tft.setTextSize(2);
tft.setCursor(45, 36);
tft.println(targetLiter, 1);
tft.setCursor(45, 63);
totalVol = flowTicks / ppl;
tft.println(totalVol, 1);
tft.setCursor(22, 81);
if (running) {
tft.setTextColor(TFT_GREEN, TFT_BLACK);
} else {
tft.setTextColor(TFT_RED, TFT_BLACK);
}
tft.println(running ? "Running" : "Stopped");
if (showTouch) {
tft.println(touchRead(T0), 1);
tft.println(touchRead(T2), 1);
tft.println(touchRead(T3), 1);
tft.println(touchRead(T8), 1);
}
// lcd.setCursor(0, 2);
// lcd.print(running ? "running" : "stopped");
// lcd.setCursor(0, 3);
// lcd.print(flowTicks);
// }
if (running != lastRunStat) {
if (running) {
Serial.println("Started");
digitalWrite(valve, HIGH);
attachInterrupt(flowMeter, flow, FALLING); // Setup Interrupt
flowTicks = 0;
} else {
detachInterrupt(flowMeter);
Serial.println("Stopped");
digitalWrite(valve, LOW);
// delay(100);
// delay(100);
//Serial.println("sensor inactive");
running = false;
}
lastRunStat = running;
}
targetPulses = targetLiter * ppl;
if (flowTicks > targetPulses && running) {
running = false;
}
}
bool checkButtons() {
int buttonHoldCounter = 0;
while (touchRead(T2) < 30 && !saved) {
delay(1000);
if (touchRead(T2) < 30 && !saving) {
saving = true;
tft.fillScreen(TFT_BLACK);
tft.setTextColor(TFT_WHITE);
tft.setTextSize(1.5);
tft.setCursor(0, 0);
tft.println("To save value");
tft.println("Hold start and stop");
delay(1000);
}
if (touchRead(T0) < T0Sensetivity && touchRead(T2) < T2Sensetivity && btnRead) {
btnRead = false;
saved = true;
EEPROM.writeFloat(EEPROM_Adr, targetLiter);
EEPROM.commit();
tft.fillScreen(TFT_BLACK);
tft.setTextColor(TFT_WHITE);
tft.setTextSize(1.5);
tft.println("Value Saved");
delay(2000);
defaultScreen();
}
}
if (touchRead(T0) < T0Sensetivity && btnRead) {
Serial.println("startBtn Preessed");
flowTicks = 0;
running = true;
btnRead = false;
////Serial.println("sensor active");
return true;
} else if (touchRead(T3) < T3Sensetivity && btnRead) {
Serial.println("decrementBtn Preessed");
btnRead = false;
targetLiter -= .1;
return true;
} else if (touchRead(T8) < T8Sensetivity && btnRead) {
Serial.println("incrementBtn Preessed");
btnRead = false;
targetLiter += .1;
return true;
} else if (!digitalRead(pulseBtn) && btnRead) {
Serial.println("pulseBtn Preessed");
running = true;
inching = true;
btnRead = false;
return true;
} else if ((touchRead(T8) > T8Sensetivity && touchRead(T3) > T3Sensetivity && touchRead(T2) > T2Sensetivity && touchRead(T0) > T0Sensetivity && digitalRead(pulseBtn)) && (!btnRead || saving)) {
btnRead = true;
saving = false;
saved = false;
defaultScreen();
if (inching) {
running = false;
inching = false;
}
Serial.println("Normalize Buttons");
return false;
}
return false;
}
void defaultScreen() {
tft.fillScreen(TFT_BLACK);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setTextSize(1.5);
tft.setCursor(45, 0);
tft.println("Liquid");
tft.setCursor(15, 9);
tft.println("Volume Controller");
tft.setCursor(0, 27);
tft.println("Target Volume");
tft.setCursor(0, 54);
tft.println("Current Volume");
}