#include "Arduino.h"
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
// Pin definitions
#define TFT_DC 7
#define TFT_CS 6
#define TFT_MOSI 11
#define TFT_CLK 13
#define TFT_RST 10
#define TFT_MISO 12
#define TRIG_PIN 22
#define ECHO_PIN 23
int prevuis_tankValue = 0;
int current_tankValue = 0;
// Display object
Adafruit_ILI9341 screen(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);
// Constants
constexpr uint16_t WARNING_COLOR = ILI9341_RED;
constexpr uint16_t TEXT_COLOR = ILI9341_GREEN;
constexpr uint16_t BG_COLOR = ILI9341_BLACK;
constexpr uint16_t LOADING_COLOR = ILI9341_YELLOW;
// Display positions
struct DisplayPos
{
uint8_t x;
uint8_t y;
};
const DisplayPos HEADER_POS = {5, 0};
const DisplayPos BYLINE_POS = {110, 5};
const DisplayPos SALES_POS = {5, 40};
const DisplayPos LITERS_POS = {5, 70};
const DisplayPos PRICE_POS = {5, 100};
const DisplayPos TANK_POS = {5, 130};
const DisplayPos LOADING_POS = {80, 110};
// Variables
int tankValue = 50;
uint16_t tankLabelWidth;
void showLoadingScreen()
{
screen.fillScreen(BG_COLOR);
screen.setTextColor(LOADING_COLOR);
screen.setTextSize(3);
screen.setCursor(LOADING_POS.x, LOADING_POS.y);
screen.print("LOADING...");
delay(1000); // Show loading screen for 1 second
screen.fillScreen(BG_COLOR);
screen.setTextColor(TEXT_COLOR);
}
void drawHeader()
{
screen.setTextSize(4);
screen.setCursor(HEADER_POS.x, HEADER_POS.y);
screen.print("AGVM ");
screen.setTextSize(3);
screen.setCursor(BYLINE_POS.x, BYLINE_POS.y);
screen.print("By: Lim Whu");
}
void drawCreditInfo()
{
screen.setTextSize(3);
screen.setCursor(SALES_POS.x, SALES_POS.y);
screen.print("CREDIT:");
}
void drawLitersInfo()
{
screen.setTextSize(3);
screen.setCursor(LITERS_POS.x, LITERS_POS.y);
screen.print("LITERS: 50.5 L");
}
void drawPriceInfo()
{
screen.setTextSize(3);
screen.setCursor(PRICE_POS.x, PRICE_POS.y);
screen.print("PRICE: 75.00 php");
}
void drawTankLabel(uint16_t color = TEXT_COLOR)
{
screen.setTextSize(3);
screen.setTextColor(color);
screen.setCursor(TANK_POS.x, TANK_POS.y);
screen.print("TANK LEVEL: ");
// Get label width for future updates
int16_t x, y;
screen.getTextBounds("TANK LEVEL: ", TANK_POS.x, TANK_POS.y, &x, &y, &tankLabelWidth, nullptr);
}
void updateTankValue(bool clear = true)
{
uint16_t color = (tankValue < 20) ? WARNING_COLOR : TEXT_COLOR;
if (clear)
{
char buffer[8];
sprintf(buffer, "%d%%", prevuis_tankValue); // Format previous value with %
int16_t x1, y1;
uint16_t w, h;
screen.setTextSize(3);
screen.getTextBounds(buffer, TANK_POS.x + tankLabelWidth, TANK_POS.y, &x1, &y1, &w, &h);
screen.fillRect(x1, y1, w, h, BG_COLOR); // Clear exact area
}
// Redraw the label with the correct color
drawTankLabel(color);
screen.setTextSize(3);
screen.setTextColor(color);
screen.setCursor(TANK_POS.x + tankLabelWidth, TANK_POS.y);
screen.print(tankValue);
screen.print("%");
}
// Change the return type to int (or uint8_t) as you're returning a value
int ultrasonicSensorSetup()
{
// Set the trigPin to LOW
digitalWrite(TRIG_PIN, LOW);
delay(2);
// Trigger the sensor by sending a HIGH pulse for 10 microseconds
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Read the echoPin, return the sound wave travel time in microseconds
long duration = pulseIn(ECHO_PIN, HIGH);
// Calculate the distance (in cm) based on the speed of sound
int distance = (duration / 2) * 0.0343;
Serial.println(distance);
return map(distance, 2, 400, 100, 0); // Map the distance to a percentage
// Note: This is a placeholder. Actual implementation may vary based on the sensor used.
}
void setup()
{
Serial.begin(9600); // Initialize serial communication for debugging
Serial.println("Initializing...");
// Initialize display
screen.begin();
screen.setRotation(3);
// Show loading screen first
showLoadingScreen();
// Draw static elements
drawHeader();
drawCreditInfo();
drawLitersInfo();
drawPriceInfo();
// Initialize tank display
drawTankLabel();
updateTankValue(false); // Initial draw without clearing
// Set the trigPin as an output and echoPin as an input
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}
void loop()
{
tankValue = ultrasonicSensorSetup(); // Get the tank value from the sensor
if (tankValue != prevuis_tankValue)
{
updateTankValue();
prevuis_tankValue = tankValue; // Update the previous value
}
}