/*
Simple "Hello World" for ILI9341 LCD
https://wokwi.com/arduino/projects/308024602434470466
*/
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include <OneWire.h>
#include <DallasTemperature.h>
#define TFT_DC 9
#define TFT_CS 10
#define ONE_WIRE_BUS 7
#define PI 3.14159265358979323846
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
#define JOY_HORIZ A0
#define JOY_VERT A1
#define JOY_SEL 2
#define JOY_CENTER 512
#define JOY_DEADZONE 64
int menuSelection = 1;
int menuSize = 4;
int menu = 0;
int tempValue[5] = {0,0,0,0,0};
void setup() {
tft.begin();
pinMode(JOY_SEL, INPUT);
sensors.begin();
printmenu();
}
int joyXbool=0,joyYbool=0,joySelbool=0;
int joyXcurr,joyYcurr,joySelcurr;
int joyXprev,joyYprev,joySelprev;
void loop() {
if (menu == 1){
sensors.requestTemperatures();
float temperature = sensors.getTempCByIndex(0);
int mapped = map(temperature, 00,50,0,60);
tft.setCursor(0,0);
tft.setTextSize(1);
tft.setTextColor(ILI9341_WHITE);
tft.print("Temperature ");
tft.print(temperature);
tft.print("C");
tft.setCursor(0,25);
tft.print("C");
tft.setCursor(8,67);
tft.print("10");
tft.setCursor(8,10);
tft.print("50");
tft.setCursor(20,74);
tft.print("-5s");
tft.setCursor(110,74);
tft.print("0s");
shiftArrayLeft(tempValue,5,mapped);
// Draw the graph
tft.fillScreen(ILI9341_BLACK);
tft.drawLine(20,10,20,70, ILI9341_WHITE);
tft.drawLine(20,70,120,70, ILI9341_WHITE);
for(int i=1;i<5;i++){
tft.drawLine(20+((i-1)*25),69-tempValue[i-1],20+(i*25),69-tempValue[i],ILI9341_RED);
}
int mapped100 = map(temperature, 00,50,0,100);
float data[2] = {mapped100,100-mapped100};
uint16_t colors[2] = {ILI9341_RED, ILI9341_GREEN};
int x = 35;
int y = 105;
int radius = 20;
float angle = 0.0;
for (int i = 0; i < 2; i++) {
float startAngle = angle;
float endAngle = angle + data[i] / 100.0 * 360.0;
fillArc(x, y, radius, startAngle, endAngle, colors[i]);
angle = endAngle;
}
tft.setTextColor(ILI9341_BLACK);
tft.setCursor(25,100);
tft.print(mapped100);
tft.print("%");
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(10,135);
tft.print("Humidity");
float data2[2] = {mapped100,100-mapped100};
uint16_t colors2[2] = {ILI9341_WHITE, ILI9341_YELLOW};
int x2 = 90;
int y2 = 105;
int radius2 = 20;
float angle2 = 0.0;
for (int i = 0; i < 2; i++) {
float startAngle = angle2;
float endAngle = angle2 + data2[i] / 100.0 * 360.0;
fillArc(x2, y2, radius2, startAngle, endAngle, colors2[i]);
angle2 = endAngle;
}
tft.setTextColor(ILI9341_BLACK);
tft.setCursor(80,100);
tft.print(mapped100);
tft.print("%");
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(75,135);
tft.print("Light");
delay(1000);
}
if (menu == 0){
int joyX = analogRead(JOY_HORIZ);
int joyY = analogRead(JOY_VERT);
int joyButton = digitalRead(JOY_SEL);
int joySelcurr = joyButton;
if (joySelcurr == 1 && joySelcurr!=joySelprev){
menu++;
}
if (joyX < JOY_CENTER - JOY_DEADZONE) {
joyXcurr = -1;
}else if (joyX > JOY_CENTER + JOY_DEADZONE) {
joyXcurr = 1;
}else{
joyXcurr = 0;
}
if (joyXcurr==-1 && joyXcurr!=joyXprev){
if (menuSelection > 1) {
menuSelection--;
printmenu();
}
}
if (joyXcurr==1 && joyXcurr!=joyXprev){
if (menuSelection < menuSize) {
menuSelection++;
printmenu();
}
}
joyXprev = joyXcurr;
joySelprev = joySelcurr;
}
}
void printmenu(){
tft.fillScreen(ILI9341_BLACK);
tft.setTextSize(3);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(00,0);
tft.print("Plant Sensor");
for (int i=1;i<=menuSize;i++){
tft.setTextSize(2);
tft.setCursor(0,(i*25)+10);
if (menuSelection == i){
menuData(i);
tft.print(" <");
}else{
menuData(i);
}
}
}
void menuData(int i){
if (i == 1){
tft.print("Check Plant");
}else if (i == 2){
tft.print("Water the Plant");
}else if (i == 3){
tft.print("Go to Deep Sleep");
}else if (i == 4){
tft.print("Select new WIFI");
}
}
void shiftArrayLeft(int arr[], int len, int newValue) {
for (int i = 0; i < len - 1; i++) {
arr[i] = arr[i + 1];
}
arr[len - 1] = newValue;
}
void fillArc(int16_t x0, int16_t y0, int16_t r, float startAngle, float endAngle, uint16_t color) {
// Convert angles from degrees to radians
float startRad = startAngle / 180.0 * PI;
float endRad = endAngle / 180.0 * PI;
// Draw the arc as a series of triangles
for (float angle = startRad; angle <= endRad; angle += 0.01) {
int16_t x1 = x0 + r * cos(angle);
int16_t y1 = y0 + r * sin(angle);
int16_t x2 = x0 + r * cos(angle + 0.01);
int16_t y2 = y0 + r * sin(angle + 0.01);
tft.fillTriangle(x0, y0, x1, y1, x2, y2, color);
}
}