#include <JC_Button.h> // 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 3
//-------------------AM Button ---------------------------------------
//const byte BUTTON_PIN(A4); // first delcare a pin that button connects to
//Button myBtn(BUTTON_PIN); // defind the button instance
// Define the pin numbers
const int ledPin = A3; // Pin connected to the LED
const int buttonPin = A4; // Pin connected to the button
// Variables to keep track of the button state and LED state
int buttonState = 0; // Current state of the button
int lastButtonState = 0; // Previous state of the button
int ledState = LOW;
//-------------------------------------------------------------------
#define TFT_DC 9
#define TFT_CS 10
#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(6, INPUT_PULLUP); // this will connect to a transistor
// pinMode(A3, OUTPUT); // this pin will control the transistor
// myBtn.begin();
// Initialize the LED pin as an output
pinMode(ledPin, 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(8, INPUT_PULLUP); //Tank pump on/off pin
pinMode(6, INPUT_PULLUP); // Tank pump auto manual pin
pinMode(A0, OUTPUT); // Tank pump switching signal
//--------------------------------------------------------------------------------------
//pinMode(LED_PIN, OUTPUT); // set the LED pin as an output
//---------------------------------------------------------
// Solar pump pinout:
pinMode(4, INPUT_PULLUP); // photo resistor signal
pinMode(5, INPUT_PULLUP); // solar pump auto manual pin
pinMode(7, INPUT_PULLUP); // Solar pump on/off pin
pinMode(A1, OUTPUT); // solar pump switching signal
//--------------------------------------------------------------------------------------
}
void loop() {
button();
Tank_Pump_Control();
//Solar_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(6)))pump1 = 1;
if (percentage > 95)pump1 = 0;
digitalWrite(A0, 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(6))tft.print("Mamual ");
else tft.print("Auto ");
if (!digitalRead(8) & !digitalRead(6)) {
pump1 = !pump1;
}
}
void Solar_Pump_Control() {
if (digitalRead(7)&digitalRead(5))pump2 = 0;
if (!digitalRead(4)&digitalRead(5))pump2 = 1;
digitalWrite(A1, pump2); // by adding !pump, the value is inverted in case a relay with low trigger is used
tft.setCursor(155, 155);
tft.setTextColor(0x57FF, 0xA800);
if (pump2 == 1)tft.println("ON ");
else if (pump2 == 0) tft.println("OFF");
tft.setCursor(155, 129);
tft.setTextColor(0x57FF, 0xAAA0);
if (!digitalRead(5))tft.print("Manual ");
else tft.print("Auto ");
if (!digitalRead(7) & !digitalRead(5)) {
pump2 = !pump2;
}
}
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
}
//-----------------------------------------------------------------------------------
//void button()
//{
// static bool ledState; // a variable that keeps the current LED status
// myBtn.read(); // read the button
// if (myBtn.wasReleased()) // if the button was released, change the LED state
// {
// ledState = !ledState;
// digitalWrite(A3, ledState);
// }
//}
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) {
ledState = !ledState; // Toggle the LED state
digitalWrite(ledPin, ledState); // Update the LED state
}
// Delay to avoid bouncing (debouncing)
delay(50);
}
// Save the current button state for the next loop iteration
lastButtonState = buttonState;
}