/*
Dual "Hello World" for ILI9341 LCD
https://wokwi.com/arduino/projects/308024602434470466
*/
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#define TFT_DC_1 9
#define TFT_CS_1 10
#define TFT_DC_2 6
#define TFT_CS_2 7
Adafruit_ILI9341 tft1 = Adafruit_ILI9341(TFT_CS_1, TFT_DC_1);
Adafruit_ILI9341 tft2 = Adafruit_ILI9341(TFT_CS_2, TFT_DC_2);
int progress1 = 0;
int progress2 = 0;
void setup() {
tft1.begin();
tft1.setRotation(3); // Set rotation for TFT1
tft1.setCursor(26, 120);
tft1.setTextColor(ILI9341_RED);
tft1.setTextSize(3);
tft1.println("Hello, TFT1!");
tft1.setCursor(20, 160);
tft1.setTextColor(ILI9341_GREEN);
tft1.setTextSize(2);
tft1.println("I can has colors?");
tft2.begin();
tft2.setRotation(3); // Set rotation for TFT2
tft2.setCursor(26, 120);
tft2.setTextColor(ILI9341_RED);
tft2.setTextSize(3);
tft2.println("Hello, TFT2!");
tft2.setCursor(20, 160);
tft2.setTextColor(ILI9341_GREEN);
tft2.setTextSize(2);
tft2.println("I can has colors?");
Serial.begin(115200);
pinMode(A0, INPUT);
pinMode(A1, INPUT);
// Meme reference: https://english.stackexchange.com/questions/20356/origin-of-i-can-haz
}
void loop() {
progress1 = analogRead(A0);
progress2 = analogRead(A1);
drawDial(tft1, 128, 128, 100, 0, 1023, progress1, 0, 100);
drawProgressBar(tft2, 192, 72, 100, 20, 0, 1023, progress2, 0, 100);
Serial.print("Progress1: ");
Serial.print(progress1);
Serial.print(" | Progress2: ");
Serial.println(progress2);
delay(100);
}
int lastLineX;
int lastLineY;
void drawDial(Adafruit_ILI9341 &tft, int x, int y, int radius, float inputMin, float inputMax, float inputValue, float guageMin, float guageMax) {
// 270 sets travel angle of gauge needle.
float DIAL_TRAVEL_DEGS = 270.00;
// -225 sets start position of gauge needle.
float DIAL_START_POSITION = -225.00;
int progress = getGaugeValue(inputMin, inputMax, inputValue, guageMin, guageMax);
progress = DIAL_TRAVEL_DEGS / guageMax * progress;
float angle = float(progress) + DIAL_START_POSITION;
float radAngle = radians(angle);
int endX = x + (cos(radAngle) * (radius - 3));
int endY = y + (sin(radAngle) * (radius - 3));
// Draw the dial background
//tft.fillCircle(x, y, radius, ILI9341_BLACK);
//tft.fillCircle(x, y, radius - 4, ILI9341_WHITE);
tft.drawCircle(x, y, radius+2, ILI9341_WHITE);
// Draw the dial needle
tft.drawLine(x, y, lastLineX, lastLineY, ILI9341_BLACK);
tft.drawLine(x, y, endX, endY, ILI9341_RED);
lastLineX = endX;
lastLineY = endY;
tft.fillCircle(x, y, 4, ILI9341_RED);
}
int getGaugeValue(float inputMin, float inputMax, float inputValue, float guageMin, float guageMax) {
return int((inputValue - inputMin) * (guageMax - guageMin) / (inputMax - inputMin) + guageMin);
}
void drawProgressBar(Adafruit_ILI9341 &tft, int x, int y, int width, int height, float inputMin, float inputMax, float inputValue, float guageMin, float guageMax) {
int progress = getGaugeValue(inputMin, inputMax, inputValue, guageMin, guageMax);
int barWidth = map(progress, 0, guageMax, 0, width);
// Draw progress bar background ILI9341_RED
tft.drawRect(x, y, width, height, ILI9341_RED);
tft.fillRect(x, y, width, height, ILI9341_BLACK);
tft.fillRect(x, y, barWidth, height, ILI9341_WHITE);
// Draw progress percentage text
tft.setTextSize(2);
tft.setTextColor(ILI9341_RED);
tft.setCursor(x + width/2 - 20, y + height/2 - 10);
tft.print(progress);
tft.print("%");
}