#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
// Define pins for the Arduino Mega and joystick
#define TFT_CS 53
#define TFT_RST 8
#define TFT_DC 9
#define JOYSTICK_X A0
#define JOYSTICK_Y A1
#define JOYSTICK_SW 7
// Create an Adafruit ILI9341 object
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
// Initial cursor position
int cursorX = 10;
int cursorY = 10;
void setup() {
Serial.begin(9600); // Initialize serial communication for debugging
tft.begin(); // Initialize the display
tft.setRotation(0); // Set display rotation to vertical (0 or 2)
tft.fillScreen(ILI9341_BLACK); // Fill the screen with black
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK); // Set text color and background
tft.setTextSize(2); // Set text size
pinMode(JOYSTICK_SW, INPUT_PULLUP); // Set joystick button pin as input with pull-up
drawInitialText(); // Draw initial text
}
void loop() {
int xValue = analogRead(JOYSTICK_X); // Read X-axis value
int yValue = analogRead(JOYSTICK_Y); // Read Y-axis value
bool buttonPressed = digitalRead(JOYSTICK_SW) == LOW; // Check if button is pressed
// Map joystick values to screen coordinates
cursorX = map(xValue, 0, 1023, 0, tft.width());
cursorY = map(yValue, 0, 1023, 0, tft.height());
// Clear the screen and update text position
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(cursorX, cursorY);
tft.println("Vehicle");
tft.setCursor(cursorX, cursorY + 30); // Adjust vertical spacing for the next line
tft.println("Monitoring");
delay(100); // Small delay to avoid flickering
}
void drawInitialText() {
tft.setCursor(cursorX, cursorY);
tft.println("Vehicle");
tft.setCursor(cursorX, cursorY + 30); // Adjust vertical spacing for the next line
tft.println("Monitoring");
}