// https://github.com/JChristensen/JC_Button
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
//#include <SoftwareSerial.h>
// define sensor PWM Pin
#define Trig 2
#define Echo 4
//-------------------AM Button ---------------------------------------
// Define the pin numbers
const int transistor_Pin = 27; // Pin connected to the LED
const int buttonPin = 25; // Pin connected to the button
// Variables to keep track of the button state and transistor signal state
int buttonState = 0; // Current state of the button
int lastButtonState = 0; // Previous state of the button
int transistor_state = LOW;
//-------------------------------------------------------------------
#define TFT_DC 17
#define TFT_CS 5
#define TFT_RST 12
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
long duration;
long total_tank_depth; // this is the overall hight between sensor and bottom of tank
long water_level_depth; // use to store distance between water suface and sensor
long duration_tankcheck; // used under function to check if tank is dipper than 3 meters
int percentage = 0;
bool state1, pump1;
bool state2, pump2;
int check = 0;
int init_screen = 1;
int fill1 = 0;
int fill2 = 0;
byte X = 25;
byte Y = 193;
byte W = 102;
byte H = 15;
void setup() {
//----------------------------------------------------------
// pinMode(12, INPUT_PULLUP); // this will connect to a transistor
//pinMode(27, OUTPUT); // this pin will control the transistor
//myBtn.begin();
// T_Btn.begin();
// Initialize the LED pin as an output
pinMode(transistor_Pin, OUTPUT);
// Initialize the button pin as an input
pinMode(buttonPin, INPUT);
//-------------------------------------------------------------------
tft.begin();
tft.setRotation(1);
tft.fillRect(0, 174, 320, 14, 0xFFFF);
tft.fillRect(0, 188, 320, 52, 0xA815);
//tft.fillScreen(ILI9341_PURPLE);
tft.fillRect(0, 0, 320, 25, 0x57EA);
tft.setTextColor(0x0);
tft.setTextSize(2);
tft.setCursor(155, 5);
tft.print("TANK");
tft.fillRect(0, 25, 320, 25, 0xFFEA);
tft.setCursor(2, 29);
tft.print("Water Level: ");
tft.fillRect(0, 50, 320, 25, 0xA815);
tft.setCursor(2, 54);
tft.print("Pump Mode: ");
tft.fillRect(0, 75, 320, 25, 0xFAAA);
tft.setCursor(2, 80);
tft.print("Pump State: ");
tft.fillRect(1, 100, 320, 25, 0x57EA);
tft.setCursor(155, 104);
tft.print("PANEL");
tft.fillRect(0, 125, 320, 25, 0xAAA0);
tft.setCursor(2, 130);
tft.print("Pump Mode: ");
tft.fillRect(0, 150, 320, 25, 0xA800);
tft.setCursor(2, 154);
tft.print("Pump State: ");
tft.drawRect(20, 189, 115, 23, 0xFFFF);
tft.setTextSize(2);
tft.setCursor(234, 193);
tft.print("Depth:");
// Tank Pump Pinout:-------------------------------------------------------------------
pinMode(Trig, OUTPUT); // ultrasonic trigger pin
pinMode(Echo, INPUT); // ultrasonic echo pin
pinMode(26, INPUT_PULLUP); //Tank pump on/off pin
pinMode(12, INPUT_PULLUP); // Tank pump auto manual pin
pinMode(22, OUTPUT); // Tank pump switching signal
//--------------------------------------------------------------------------------------
//pinMode(LED_PIN, OUTPUT); // set the LED pin as an output
//---------------------------------------------------------
}
void loop() {
button();
//T_AutoManual();
Tank_Pump_Control();
barProgress();
}
void Tank_Pump_Control() {
Sensor_PWM();
// Print distance from sensor to water surface.
tft.setTextColor(0xFFEA);
tft.setCursor(240, 215);
tft.setTextColor(0xFFEA, 0xA815);
tft.print(water_level_depth);
tft.println("cm ");
//Print distance as percentage.
if (percentage < 0)percentage = 0;
tft.setTextColor(0xA800, 0xFFEA);
tft.setCursor(155, 29);
tft.print(percentage);
tft.println("% ");
//check percentage of distance and turn pump on or off
if ((percentage < 30) & (digitalRead(12)))pump1 = 1;
if (percentage > 90)pump1 = 0;
digitalWrite(22, pump1); // by adding !pump, the value is inverted in case a relay with low trigger is used
// Check if pump is on or off and print text accordingly.
tft.setCursor(155, 79);
tft.setTextColor(0x57FF, 0xFAAA);
if (pump1 == 1)tft.println("ON ");
else if (pump1 == 0) tft.println("OFF ");
// Check condition to determine if button is flipped on Auto or Manual position
tft.setCursor(155, 55);
tft.setTextColor(0x57FF, 0xA815);
if (!digitalRead(12))tft.print("Mamual ");
else tft.print("Auto ");
if (!digitalRead(26) & !digitalRead(12)) {
pump1 = !pump1;
}
}
void barProgress()
{
tft.fillRect(22, 193, 110, H, 0xA815); // erase old data
tft.fillRect(X, Y, percentage + 5, H, 0xFFEA); // fill in % level every 25% increase
tft.drawLine(58, 190, 58, 209, 0xA800);// draw line seperating each 25% increase
tft.drawLine(82, 190, 82, 209, 0xA800);
tft.drawLine(106, 190, 106, 209, 0xA800);
tft.drawLine(130, 190, 130, 209, 0xA800);
// Print progress text
tft.setCursor(25, 215);
tft.setTextColor(0xFFFF, 0xA815);
tft.print("Progress = ");
//Print percentage value
tft.setCursor(158, 215);
tft.setTextColor(0xFFEA, 0xA815);
tft.print(percentage);
tft.println("% ");
}
void Sensor_PWM() {
digitalWrite(Trig, LOW);
delayMicroseconds(2);
digitalWrite(Trig, HIGH);
delayMicroseconds(20);
digitalWrite(Trig, LOW);
duration = pulseIn(Echo, HIGH);
water_level_depth = (duration / 2) * 0.343; // distance from water surface to sensor.
water_level_depth = water_level_depth / 10;
percentage = map(water_level_depth, 5, 400, 100, 0); // express distance in percentage max length
//percentage = map(water_level_depth, 5, 30, 100, 0); // express distance in percentage test
}
//Tank Auto manual soft touch button function
void button()
{
// Read the current state of the button
buttonState = digitalRead(buttonPin);
// Check if the button state has changed (from LOW to HIGH)
if (buttonState != lastButtonState) {
// If the button is pressed (HIGH), toggle the LED state
if (buttonState == HIGH) {
transistor_state = !transistor_state; // Toggle the LED state
digitalWrite(transistor_Pin, transistor_state); // Update the LED state
}
// Delay to avoid bouncing (debouncing)
delay(50);
}
// Save the current button state for the next loop iteration
lastButtonState = buttonState;
}