/*
Dual "Hello World" for ILI9341 LCD
https://wokwi.com/arduino/projects/308024602434470466
*/
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
// Pin definitions for TFT1 and TFT2
#define TFT_DC_1 9
#define TFT_CS_1 10
// Pin definitions for buttons
#define BUTTON_INC_PIN 2 // Increase button
#define BUTTON_DEC_PIN 3 // Decrease button
// Pin definitions for LEDs (Too High and Too Low for each wheel)
#define LED_WHEEL_1_LOW 4
#define LED_WHEEL_1_HIGH 5
#define LED_WHEEL_2_LOW A4
#define LED_WHEEL_2_HIGH A5
#define LED_WHEEL_3_LOW 6
#define LED_WHEEL_3_HIGH 7
#define LED_WHEEL_4_LOW 1
#define LED_WHEEL_4_HIGH 8
// Initialize TFT1 and TFT2
Adafruit_ILI9341 tft1 = Adafruit_ILI9341(TFT_CS_1, TFT_DC_1);
// Progress variables
int progress1 = 0; // Wheel 1
int progress2 = 0; // Wheel 2
int progress3 = 0; // Wheel 3
int progress4 = 0; // Wheel 4
// Car height levels (0 to 3)
int carHeight = 0;
void setup() {
// Initialize TFT1
tft1.begin();
tft1.setRotation(3);
// Set up display text
tft1.setCursor(26, 120);
tft1.setTextColor(ILI9341_RED);
tft1.setTextSize(3);
tft1.println("Hello, TFT1!");
tft1.setCursor(20, 160);
tft1.setTextColor(ILI9341_GREEN);
tft1.setTextSize(2);
tft1.println("I can has colors?");
// Initialize Serial communication
Serial.begin(115200);
// Set pin modes for analog inputs (potentiometers)
pinMode(A0, INPUT); // Wheel 1
pinMode(A1, INPUT); // Wheel 2
pinMode(A2, INPUT); // Wheel 3
pinMode(A3, INPUT); // Wheel 4
// Set pin modes for buttons
pinMode(BUTTON_INC_PIN, INPUT_PULLUP); // Button for increasing height
pinMode(BUTTON_DEC_PIN, INPUT_PULLUP); // Button for decreasing height
// Set pin modes for LEDs
pinMode(LED_WHEEL_1_LOW, OUTPUT);
pinMode(LED_WHEEL_1_HIGH, OUTPUT);
pinMode(LED_WHEEL_2_LOW, OUTPUT);
pinMode(LED_WHEEL_2_HIGH, OUTPUT);
pinMode(LED_WHEEL_3_LOW, OUTPUT);
pinMode(LED_WHEEL_3_HIGH, OUTPUT);
pinMode(LED_WHEEL_4_LOW, OUTPUT);
pinMode(LED_WHEEL_4_HIGH, OUTPUT);
}
void loop() {
// Read analog inputs (potentiometers)
progress1 = analogRead(A0);
progress2 = analogRead(A1);
progress3 = analogRead(A2);
progress4 = analogRead(A3);
// Update car height based on button presses
if (digitalRead(BUTTON_INC_PIN) == LOW) {
increaseCarHeight();
delay(200); // Debounce delay
}
if (digitalRead(BUTTON_DEC_PIN) == LOW) {
decreaseCarHeight();
delay(200); // Debounce delay
}
// Determine the permitted range based on carHeight
int rangeMin = carHeight * 256;
int rangeMax = rangeMin + 255;
// Draw progress bars on TFT1
drawProgressBar(tft1, 20, 60, 100, 20, 0, 1023, progress1, 0, 100); // Wheel 1
drawProgressBar(tft1, 140, 60, 100, 20, 0, 1023, progress2, 0, 100); // Wheel 2
drawProgressBar(tft1, 20, 100, 100, 20, 0, 1023, progress3, 0, 100); // Wheel 3
drawProgressBar(tft1, 140, 100, 100, 20, 0, 1023, progress4, 0, 100);// Wheel 4
// Update LEDs based on whether the wheel height is too low or too high
updateLEDs(progress1, LED_WHEEL_1_LOW, LED_WHEEL_1_HIGH, rangeMin, rangeMax);
updateLEDs(progress2, LED_WHEEL_2_LOW, LED_WHEEL_2_HIGH, rangeMin, rangeMax);
updateLEDs(progress3, LED_WHEEL_3_LOW, LED_WHEEL_3_HIGH, rangeMin, rangeMax);
updateLEDs(progress4, LED_WHEEL_4_LOW, LED_WHEEL_4_HIGH, rangeMin, rangeMax);
// Display the current car height level
tft1.fillRect(20, 140, 200, 30, ILI9341_BLACK); // Clear the area
tft1.setCursor(20, 140);
tft1.setTextColor(ILI9341_YELLOW);
tft1.setTextSize(2);
tft1.print("Car Height: ");
tft1.print(carHeight);
// Print progress and car height values to Serial Monitor
Serial.print("Progress1: ");
Serial.print(progress1);
Serial.print(" | Progress2: ");
Serial.print(progress2);
Serial.print(" | Progress3: ");
Serial.print(progress3);
Serial.print(" | Progress4: ");
Serial.print(progress4);
Serial.print(" | Car Height: ");
Serial.println(carHeight);
delay(100);
}
// Function to map input value to gauge value
int getGaugeValue(float inputMin, float inputMax, float inputValue, float gaugeMin, float gaugeMax) {
return int((inputValue - inputMin) * (gaugeMax - gaugeMin) / (inputMax - inputMin) + gaugeMin);
}
// Function to draw a progress bar on the TFT display
void drawProgressBar(Adafruit_ILI9341 &tft, int x, int y, int width, int height, float inputMin, float inputMax, float inputValue, float gaugeMin, float gaugeMax) {
int progress = getGaugeValue(inputMin, inputMax, inputValue, gaugeMin, gaugeMax);
int barWidth = map(progress, 0, gaugeMax, 0, width);
// Draw progress bar background
tft.drawRect(x, y, width, height, ILI9341_RED);
tft.fillRect(x, y, width, height, ILI9341_BLACK);
tft.fillRect(x, y, barWidth, height, ILI9341_WHITE);
// Draw progress percentage text
tft.setTextSize(2);
tft.setTextColor(ILI9341_RED);
tft.setCursor(x + width / 2 - 20, y + height / 2 - 10);
tft.print(progress);
tft.print("%");
}
// Function to update LEDs based on whether the wheel height is too low or too high
void updateLEDs(int progress, int ledLow, int ledHigh, int rangeMin, int rangeMax) {
digitalWrite(ledLow, (progress < rangeMin) ? HIGH : LOW);
digitalWrite(ledHigh, (progress > rangeMax) ? HIGH : LOW);
}
// Function to increase car height level
void increaseCarHeight() {
if (carHeight < 3) {
carHeight++;
}
}
// Function to decrease car height level
void decreaseCarHeight() {
if (carHeight > 0) {
carHeight--;
}
}