#include <Adafruit_ILI9341.h>
#include <Adafruit_FT6206.h>
#include <SPI.h>
#include <Wire.h>
#include "HX711.h"
#define TFT_CS 10
#define TFT_DC 8
#define TFT_RST 9
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
Adafruit_FT6206 touchScreen = Adafruit_FT6206();
const int LOADCELL_DOUT_PIN = 3;
const int LOADCELL_SCK_PIN = 2;
HX711 scale;
int currentScreen = 0; // Track the current screen
void setup() {
Serial.begin(9600);
tft.begin();
tft.setRotation(4); // Set rotation to landscape
tft.fillScreen(0xB104);
tft.setTextColor(ILI9341_WHITE); // White text
tft.setTextSize(2);
tft.setCursor(15, 10);
tft.println("TRI DUC HIGHSCHOOL");
tft.setCursor(55, 160);
tft.println("Scale Ready");
delay(2000);
if (!touchScreen.begin(40)) {
Serial.println("Unable to start touchscreen.");
while (1);
}
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale(420);
scale.tare();
showCurrentScreen();
}
void loop() {
if (touchScreen.touched()) {
TS_Point p = touchScreen.getPoint();
p.x = map(p.x, 0, 240, 0, tft.width());
p.y = map(p.y, 0, 320, 0, tft.height());
// Determine which side of the screen was touched
if (p.x < tft.width() / 2) {
// Left side - Previous screen
//currentScreen = (currentScreen - 1 + 4) % 4; // Cycle to the previous screen
currentScreen = (currentScreen + 1) % 4;
} else {
// Right side - Next screen
//currentScreen = (currentScreen + 1) % 4; // Cycle to the next screen
currentScreen = (currentScreen - 1 + 4) % 4;
}
showCurrentScreen();
delay(500); // Delay to avoid rapid switching
}
}
void showCurrentScreen() {
tft.fillScreen(ILI9341_BLACK); // Clear the screen before showing the new one
switch (currentScreen) {
case 0:
showFirstScreen();
break;
case 1:
showSecondScreen();
break;
case 2:
showThirdScreen();
break;
case 3:
showFourthScreen();
break;
}
}
void showFirstScreen() {
float weight = scale.get_units(5);
float M = (50 * 10 * weight / 1056) * 0.7;
float FM = (50 * 10 * weight / 1056) * 0.6;
tft.setCursor(10, 10);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.print("Weight: ");
tft.print(weight, 2);
tft.print(" kg");
tft.setCursor(10, 50);
tft.print("Maximum AlcUnits: ");
tft.setCursor(10, 90);
tft.print("Male: ");
tft.print(M, 2);
tft.print(" DVC");
tft.setCursor(10, 130);
tft.print("Female: ");
tft.print(FM, 2);
tft.print(" DVC");
}
void showSecondScreen() {
float weight = scale.get_units(5);
float nubeer = ((50 * 10 * weight / 1056) * 0.7) / 0.79;
int hein = nubeer / 0.05;
int dquat = nubeer / 0.04;
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.setCursor(8, 10);
tft.print(" 1st stage Penalty ");
tft.setCursor(8, 22);
tft.print(" - - - - - - - - - ");
tft.setCursor(8, 24);
tft.print("- - - - - - - - - -");
tft.setCursor(8, 26);
tft.print(" - - - - - - - - - ");
tft.setCursor(10, 70);
tft.print("Heineken: ");
tft.print(hein);
tft.print(" ml");
tft.setCursor(10, 130);
tft.print("Dung Quat: ");
tft.print(dquat);
tft.print(" ml");
}
void showThirdScreen() {
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(10, 10);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.println("Third Screen");
tft.setCursor(10, 50);
tft.println("Additional Info:");
tft.setCursor(10, 90);
tft.println("More features");
}
void showFourthScreen() {
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(10, 10);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.println("Fourth Screen");
tft.setCursor(10, 50);
tft.println("System Status:");
tft.setCursor(10, 90);
tft.println("All systems go!");
}
Loading
ili9341-cap-touch
ili9341-cap-touch