#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#define S_BTN 5
#define C_BTN 22
#define TFT_DC 2
#define TFT_CS 15
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
int selectState = 0;
int target = 0;
int maxTarget = 100;
void setup() {
pinMode(S_BTN, INPUT_PULLUP);
pinMode(C_BTN, INPUT_PULLUP);
Serial.begin(9600);
tft.begin();
// tft.fillScreen(ILI9341_BLACK);
tft.setRotation(2);
}
void loop() {
if (digitalRead(S_BTN) == LOW) {
selectState++;
tft.fillScreen(ILI9341_BLACK);
}
if (selectState == 0) {
setPressure(target);
if (digitalRead(C_BTN) == LOW) {
target++;
// setPressure(target);
if (target == maxTarget) {
target = 0;
}
}
} else if (selectState == 1) {
for (int i = 0; i < 101; i++) {
int value = i / 5;
drawLEDBarGraph(value);
sensorText(i);
delay(10);
}
}
}
void sensorText(int value) {
tft.setRotation(1);
tft.setCursor(100, 100);
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.setTextSize(3);
tft.println(value);
if (value < 10)
{
tft.fillRect(115, 100, 18, 24, ILI9341_BLACK);
}
if (value < 100)
{
tft.fillRect(135, 100, 18, 24, ILI9341_BLACK);
}
tft.setRotation(2);
}
void drawLEDBarGraph(int value) {
int barWidth = 10;
int barHeight = 40;
int x = 15;
int y = 10;
tft.drawRect(x, y - 1, (barWidth * 20) + 1, barHeight + 2, ILI9341_WHITE);
for (int i = 0; i < value; i++) {
tft.fillRect(x + (i * barWidth + 1), y + 1, barWidth - 2, barHeight - 2, ILI9341_GREEN);
}
for (int i = value; i < 20; i++) {
tft.fillRect(x + (i * barWidth + 1), y + 1, barWidth - 2, barHeight - 2, ILI9341_BLACK);
}
}
void setPressure(int value) {
tft.setRotation(1);
tft.setCursor(0, 100);
tft.setTextSize(2);
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.println("Press button to set target");
tft.setTextSize(3);
tft.setTextColor(ILI9341_RED, ILI9341_BLACK);
tft.print("Target = ");
tft.print(value);
tft.setRotation(2);
}