#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#define TFT_DC 9
#define TFT_CS 10
Adafruit_ILI9341 myScreen = Adafruit_ILI9341(TFT_CS, TFT_DC);
constexpr int pbRight = 5;
constexpr int pbUp = 6;
constexpr int pbDown = 7;
void setup() {
Serial.begin(9600);
Serial.println("Test");
pinMode(pbRight, INPUT_PULLUP);
pinMode(pbUp, INPUT_PULLUP);
pinMode(pbDown, INPUT_PULLUP);
myScreen.begin();
myScreen.setTextColor(ILI9341_RED);
myScreen.setTextSize(2);
myScreen.setRotation(3);
setRain1Timer();
}
void loop(void) {
}
void setRain1Timer() {
int rainHr = 0;
int rainMin = 0;
int rainTime = 0;
myScreen.fillScreen(ILI9341_BLACK);
myScreen.setCursor(0, 0);
myScreen.print("Rain Time 1:");
myScreen.setCursor(0, 20);
myScreen.print("ENTER ON TIME");
int inputGiven = 0;
while (inputGiven < 4) {
switch (inputGiven) {
case 0 : if (getInput("Hr ", 200, 20, rainHr, 24) == true) {
inputGiven = 1;
};
break;
case 1 : if (getInput("Min ", 200, 40, rainMin, 60) == true) {
inputGiven = 2;
};
break;
case 2 : if (getInput("Sec ", 200, 60, rainTime, 60) == true) {
inputGiven = 3;
};
break;
case 3 : // Write all data to EEPROM here
Serial.println("Write data to EEPROM");
inputGiven = 4;
break;
}
}
// Read and print data from EEPROM here
Serial.println("Read and print data from EEPROM");
printData(rainHr,rainMin,rainTime);
}
boolean getInput(String txt, int x, int y, int &data, int maxValue) {
static unsigned long lastPrint = 0;
static unsigned long lastChange = 0;
static int oldData = 0;
static String oldText = "";
// Assuming that only one button is pressed at a time!!!
boolean Up = joyButtonRead(pbUp);
boolean Down = joyButtonRead(pbDown);
boolean Right = joyButtonRead(pbRight);
if (millis() - lastPrint > 200) {
lastPrint = millis();
if (oldData != data || oldText != txt ) {
myScreen.setTextColor(ILI9341_BLACK);
myScreen.setCursor(x, y);
myScreen.print(oldText);
myScreen.print(oldData);
oldData = data;
oldText = txt;
}
myScreen.setTextColor(ILI9341_RED);
myScreen.setCursor(x, y);
myScreen.print(txt);
myScreen.print(data);
}
if (millis() - lastChange > 200) {
if (Up) {
lastChange = millis();
data++;
if (data > maxValue) {
data = 0;
}
}
if (Down) {
lastChange = millis();
data--;
if (data < 0) {
data = maxValue-1;
}
}
if (Right) {
lastChange = millis();
return true;
}
}
return false;
}
boolean joyButtonRead(int pin) {
// To make it easy for me I removed the "bounce" option of the buttons in Wokwi ;-)
return !digitalRead(pin);
}
void printData(int hr, int mi, int se){
myScreen.fillScreen(ILI9341_BLACK);
myScreen.setCursor(10, 30);
myScreen.print(" START TIME SET ");
myScreen.setCursor(10, 50);
myScreen.print("AT: ");
myScreen.print(hr);
myScreen.print(":");
myScreen.print(mi);
myScreen.print(":00");
myScreen.print(" ");
myScreen.setCursor(10, 70);
myScreen.print("DURATION: ");
myScreen.print(se);
myScreen.print(" SECONDS");
myScreen.setCursor(10, 90);
myScreen.print("SAVED");
}