#include <SPI.h> // Include the SPI library
// This library allows you to communicate with SPI (Serial Peripheral Interface) devices.
// It provides functions to set up and control SPI communication, making it easier to interface with devices like sensors, displays, and memory cards.
#include <Adafruit_GFX.h> // Include the Adafruit GFX library
// This is the core graphics library from Adafruit.
// It provides a common set of graphics functions (like drawing shapes, text, and images) that can be used with various display types (LCD, OLED, etc.).
#include <Adafruit_ILI9341.h> // Include the Adafruit ILI9341 library
// This library is specifically designed for the ILI9341 TFT display.
// It provides functions to control the ILI9341 display, including initializing the display, setting pixel colors, and drawing graphics.
// This library works together with the Adafruit GFX library to provide a complete set of tools for working with ILI9341 displays.
// These are all libraries which contain prewritten code to do common tasks
// Abstraction yaaaaaay
int delayTime = 100; // Variable declaration
// Define the pin numbers for the TFT display connections
#define TFT_MISO 26 // MISO (Master In Slave Out) pin for SPI communication
// This pin is used to receive data from the TFT display to the ESP32.
// In SPI communication, the MISO line carries data from the slave device (TFT display) back to the master device (ESP32).
//Green
#define TFT_LED 5 // LED pin to control the backlight of the TFT display
// By setting this pin high or low, you can turn the backlight on or off, respectively.
// This is useful for saving power or adjusting the display's visibility.
//Orange
#define TFT_SCK 19 // SCK (Serial Clock) pin for SPI communication
// This pin provides the clock signal generated by the master device (ESP32) to synchronize data transmission.
// Both the master and slave devices use this clock signal to ensure data is sent and received at the correct times.
//Cyan
#define TFT_MOSI 23 // MOSI (Master Out Slave In) pin for SPI communication
// This pin is used to send data from the ESP32 to the TFT display.
// In SPI communication, the MOSI line carries data from the master device (ESP32) to the slave device (TFT display).
//Dark Blue
#define TFT_DC 21 // DC (Data/Command) pin to switch between data and command mode
// When the DC pin is low, the TFT display interprets the incoming data as a command.
// When the DC pin is high, the TFT display interprets the incoming data as display data (e.g., pixel information).
//Pink
#define TFT_RESET 18 // RESET pin to reset the TFT display
// When the RESET pin is pulled low, the display is reset, and it initializes itself.
// This is useful for ensuring the display starts in a known state.
//White
#define TFT_CS 22 // CS (Chip Select) pin to select the TFT display for SPI communication
// When the CS pin is low (active), the TFT display is selected, and the ESP32 can communicate with it.
// When the CS pin is high (inactive), the TFT display is not selected, and the ESP32 will not communicate with it.
//Purple
//All of these constants are to make the object from the Adafruit library
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCK, TFT_RESET, TFT_MISO);
//This tft thing creates an object for the screen so we can actually control the screen and use functions on it
void playStartupSound() {
tone(13, 523, 200); // C5
delay(200);
tone(13, 659, 200); // E5
delay(200);
tone(13, 784, 200); // G5
delay(200);
tone(13, 1046, 400); // C6
delay(400);
noTone(13);
}
const int greenButtonPin = 27;
const int yellowButtonPin = 16;
const int redButtonPin = 32;
int oldGreenValue = HIGH; // idle value is HIGH, when button is clicked, value input is LOW so a change is
int oldYellowValue = HIGH; // detected.
int oldRedValue = HIGH;
const int debounceDelay = 50; // Debounce delay in milliseconds
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);//Sets the serial communication at a baud rate of 9600 bits per second
//Serial is an object that allows the microcontroller on the arduino board to communication with a computer or other devices
//baud rate specifies how fast the data is sent
// gpt says 9600 is common
Serial.println("Hello, ESP32!");
//Serial monitor shows text data sent from the arduino board via serial communication
//Its the arduino speaking
//Should be good for live updates of any data sent from the arduino
//I just commented it out and it works without, not sure why but ok
//LED blink example
pinMode(2, OUTPUT); // initialize digital pin 2 (GPIO2) as an output.
digitalWrite(2, LOW);
//functionName(parameter1, parameter2)
//Turn on backlight for display, to actually make something pop up on the screen
pinMode(TFT_LED, OUTPUT);
digitalWrite(TFT_LED, HIGH); //If want to turn off, put to LOW
pinMode(13, OUTPUT);
pinMode(greenButtonPin, INPUT_PULLUP);
pinMode(yellowButtonPin, INPUT_PULLUP);
pinMode(redButtonPin, INPUT_PULLUP);
//Initializing Display
tft.begin();
tft.setSPISpeed(40000000);
//tft.setRotation(1);
//by default the screen is in portrait mode
//this would make it horizontal, 2 would make it portrait upside down, 3 would be landscape upsidedown
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(2);
tft.println("Hello World!");
playStartupSound();
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
//digitalWrite(2, HIGH); // turn the LED on (HIGH is the voltage level)
//delay(delayTime); // wait for a second
//digitalWrite(2, LOW); // turn the LED off by making the voltage LOW
//delay(delayTime); // wait another second
int newGreenValue = digitalRead(greenButtonPin); // reading value from button
// Check if the value was changed,
// by comparing it with the previous value.
if(newGreenValue != oldGreenValue)
{
delay(debounceDelay); // Wait for debounce delay
if(newGreenValue == LOW)
{
Serial.println("The green button is pressed.");
digitalWrite(2, HIGH);
showScreen(3);
}
else
{
Serial.println("The green button is released.");
digitalWrite(2, LOW);
}
// Remember the value for the next time.
oldGreenValue = newGreenValue;
}
int newYellowValue = digitalRead(yellowButtonPin);
// Check if the value was changed,
// by comparing it with the previous value.
if(newYellowValue != oldYellowValue)
{
delay(debounceDelay); // Wait for debounce delay
if(newYellowValue == LOW)
{
Serial.println("The yellow button is pressed.");
digitalWrite(2, HIGH);
showScreen(2);
}
else
{
Serial.println("The yellow button is released.");
digitalWrite(2, LOW);
}
// Remember the value for the next time.
oldYellowValue = newYellowValue;
}
int newRedValue = digitalRead(redButtonPin);
// Check if the value was changed,
// by comparing it with the previous value.
if(newRedValue != oldRedValue)
{
delay(debounceDelay); // Wait for debounce delay
if(newRedValue == LOW)
{
Serial.println("The red button is pressed.");
digitalWrite(2, HIGH);
showScreen(1);
}
else
{
Serial.println("The red button is released.");
digitalWrite(2, LOW);
}
// Remember the value for the next time.
oldRedValue = newRedValue;
}
}
void showScreen(int screen) {
tft.fillScreen(ILI9341_WHITE);
tft.setCursor(0, 0);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(2);
if (screen == 1) {
tft.println("Screen red");
// Add your screen 1 content here
} else if (screen == 2) {
tft.println("Screen yellow");
// Add your screen 2 content here
}else if (screen == 3) {
tft.println("Screen green");
tft.fillRect(100, 50, 20, 30, tft.color565(255, 255, 0));
// Add your screen 2 content here
}
}