#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#define TFT_DC 9
#define TFT_CS 10
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
void setup() {
tft.begin();
tft.setCursor(26, 120);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(3);
tft.println("Hello, TFT!");
tft.setCursor(20, 160);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.println("I can has colors?");
Serial.begin(115200);
pinMode(A0, INPUT);
}
void loop() {
int inputValue = analogRead(A0);
drawProgressBar(0, 10, 128, 20, 0, 1023, inputValue, 0, 100);
drawDial(64, 72, 30, 0, 1023, inputValue, 0, 100);
Serial.println(inputValue);
delay(100);
}
void drawDial(int x, int y, int radius, int inputMin, int inputMax, int inputValue, int guageMin, int guageMax){
// 270 sets travel angle of guage needle.
int DIAL_TRAVEL_DEGS = 270;
// -225 sets start position of guage needle.
int DIAL_START_POSITION = -225;
int progress = getGuageValue(inputMin, inputMax, inputValue, guageMin, guageMax);
progress = DIAL_TRAVEL_DEGS / guageMax * progress;
int angle = progress + DIAL_START_POSITION;
float radAngle = radians(angle);
int endX = x + (cos(radAngle) * (radius-3));
int endY = y + (sin(radAngle) * (radius-3));
tft.drawLine(x, y, endX, endY, ILI9341_RED);
tft.drawCircle(x, y, radius, 255);
}
void drawProgressBar(int x, int y, int w, int h, float inputMin, float inputMax, float inputValue, float guageMin, float guageMax){
int margin = 5;
tft.drawRect(x, y, w, h, ILI9341_RED);
int progress = getGuageValue(inputMin, inputMax, inputValue, guageMin, guageMax);
progress = map(progress, guageMin, guageMax, 0, w-(2*margin)); // Use the map function for scaling
tft.fillRect(x+margin, y+margin, progress, h-(margin*2), ILI9341_BLUE);
}
int getGuageValue(float inputMin, float inputMax, float inputValue, float guageMin, float guageMax) {
float value = inputValue;
value = value / (inputMax - inputMin);
return int(value * (guageMax - guageMin));
}