/*
Simple "Hello World" for ILI9341 LCD
https://wokwi.com/arduino/projects/308024602434470466
*/
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
//Define Screen Settings
// #define TFT_DC 9
// #define TFT_CS 10
// Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
// [ "lcd1:GND", "uno:GND.1", "black", [ "v22", "h-46" ] ],
// [ "lcd1:VCC", "uno:5V", "red", [ "h-75.2", "v12", "h-241.4", "v407.23", "h179.62" ] ],
// [ "lcd1:SCK", "uno:13", "green", [ "v16", "h-75" ] ],
// [ "lcd1:MISO", "uno:12", "limegreen", [ "v20", "h-78" ] ],
// [ "lcd1:MOSI", "uno:11", "green", [ "v24", "h-75" ] ],
// [ "lcd1:CS", "uno:10", "orange", [ "v32", "h-64" ] ],
// [ "lcd1:D/C", "uno:9", "purple", [ "v36", "h-84" ] ],
#define TFT_CS 10
#define TFT_RST 8
#define TFT_DC 9
#define TFT_SDI 11 //SDI(MOSI)
#define TFT_SCK 13
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC,TFT_SDI,TFT_SCK);
// Adafruit_ILI9341(int8_t _CS, int8_t _DC, int8_t _MOSI, int8_t _SCLK,int8_t _RST = -1, int8_t _MISO = -1);
//Input & Button Logic
const int numOfInputs = 4;
const int inputPins[numOfInputs] = {7,6,5,4};
int inputState[numOfInputs];
int lastInputState[numOfInputs] = {LOW,LOW,LOW,LOW};
bool inputFlags[numOfInputs] = {LOW,LOW,LOW,LOW};
long lastDebounceTime[numOfInputs] = {0,0,0,0};
long debounceDelay = 5;
//LCD Menu Logic
const int numOfScreens = 10;
int currentScreen = 0;
String screens[numOfScreens][2] = {{"Motor Voltage","Volts"}, {"Motor Current", "Amps"},
{"Motor Rated HP","HP"},{"Overload Temp.","degC"}, {"Accel Time", "Secs"}, {"Restart Time","Mins"},
{"Analog Out. Curr.","mA"},{"Input Temp.","degC"}, {"Run Time", "Hours"}, {"Start Times","times"}};
int parameters[numOfScreens];
void sSet ()
{
tft.begin();
tft.setRotation(1);
// 240x320 2.8" or 320x240 sideways
// tft.setCursor(0, 80);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
// tft.println("Welcome to the Serial Menu");
// tft.setCursor(60, 120);
// tft.setTextSize(2);
// tft.println("Boot Screen");
Serial.begin(9600);
Serial.println("TFT LCD Menu");
Serial.println("Welcome to the Serial Menu");
// delay(600);
// tft.fillScreen(ILI9341_BLACK );
tft.setCursor(0, 10);
tft.println("-----Test Main Menu-----");
tft.setTextColor(ILI9341_WHITE);
///////////sceen Settings
///Testing where to put text
// tft.fillRect(0,45,170,25,ILI9341_RED);
// tft.setCursor(1, 50);
// tft.print("Motor Voltage ");
// delay(900);
// tft.fillRect(0,45,170,25,ILI9341_BLACK);
// tft.setCursor(1, 50);
// tft.print("FUK ");
Serial.begin(9600);
Serial.println("TFT LCD Menu");
Serial.println("Hello, Welcome to the Serial Menu");
}
void iSet ()
{
for(int i = 0; i < numOfInputs; i++) {
pinMode(inputPins[i], INPUT);
digitalWrite(inputPins[i], HIGH); // pull-up 20k
}
}
void setup()
{
sSet ();
iSet ();
}
void loop()
{
setInputFlags();
resolveInputFlags();
}
void setInputFlags() {
for(int i = 0; i < numOfInputs; i++) {
int reading = digitalRead(inputPins[i]);
if (reading != lastInputState[i]) {
lastDebounceTime[i] = millis();
}
if ((millis() - lastDebounceTime[i]) > debounceDelay) {
if (reading != inputState[i]) {
inputState[i] = reading;
if (inputState[i] == HIGH) {
// Serial.println("Set");
// Serial.println(i);
// Serial.println(currentScreen);
inputFlags[i] = HIGH;
}
}
}
lastInputState[i] = reading;
}
}
void resolveInputFlags() {
for(int i = 0; i < numOfInputs; i++) {
if(inputFlags[i] == HIGH) {
Serial.print("\nButton ");
Serial.print(i);
Serial.print(" Current Scren ");
Serial.println(currentScreen);
inputAction(i);
inputFlags[i] = LOW;
printScreen();
}
}
}
void inputAction(int input) {
if(input == 0) {
if (currentScreen == 0) {
currentScreen = numOfScreens-1;
}else{
currentScreen--;
}
}else if(input == 1) {
if (currentScreen == numOfScreens-1) {
currentScreen = 0;
}else{
currentScreen++;
}
}else if(input == 2) {
parameterChange(0);
}else if(input == 3) {
parameterChange(1);
}
}
void parameterChange(int key) {
if(key == 0) {
parameters[currentScreen]++;
}else if(key == 1) {
parameters[currentScreen]--;
}
}
void printScreen() {
// tft.fillRect(0,45,170,25,ILI9341_RED);
// tft.setCursor(1, 50);
// tft.print("Motor Voltage ");
// delay(900);
// tft.fillRect(0,45,170,25,ILI9341_BLACK);
// tft.setCursor(1, 50);
// tft.print("FUK ");
// //FIRST ITEM ON MENU
tft.setCursor(1, 50);
tft.fillRect(0,45,170,25,ILI9341_BLACK);
tft.print(screens[currentScreen][0]); ///IT FUYCKING WORKS
//SECOND ITEM ON MENU
// tft.setCursor(0, 90);
// tft.println(parameters[currentScreen]);
// tft.println("Boot Volts ");
// lcd.print(parameters[currentScreen]);
// // lcd.clear();
// tft.setTextColor(ILI9341_WHITE);
// Serial.println("\nPrint Scren");
// tft.setCursor(0,1);
// tft.print(screens[currentScreen][0]); ///IT FUYCKING WORKS
///ORIGINAL CODE
// lcd.print(screens[currentScreen][0]);
// lcd.setCursor(0,1);
// lcd.print(parameters[currentScreen]);
// lcd.print(" ");
// lcd.print(screens[currentScreen][1]);
}