#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#define TFT_DC 7
#define TFT_CS 6
#define TFT_MOSI 11
#define TFT_CLK 13
#define TFT_RST 10
#define TFT_MISO 12
Adafruit_ILI9341 screen = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);
int boxSize = 80; // Size of the box
int currentPosition = 4; // Start at position 5 (index 4 in positions array)
// Positions for the boxes on the grid
int positions[9][2] = {
{0, 2}, // 1 (top left)
{80, 2}, // 2 (top middle)
{160, 2}, // 3 (top right)
{0, 108}, // 4 (middle left)
{80, 108},// 5 (center)
{160, 108},// 6 (middle right)
{0, 214}, // 7 (bottom left)
{80, 214},// 8 (bottom middle)
{160, 214} // 9 (bottom right)
};
// Analog pins for the potentiometers
int potPins[5] = {A1, A2, A3, A4, A5};
int targetPositions[5] = {1, 3, 4, 5, 7}; // Mapped positions for each potentiometer (A1 → 2, A2 → 4, etc.)
// Function to draw a single box at any position
void drawBox(int x, int y) {
screen.fillRect(x, y, boxSize, boxSize, ILI9341_BLUE);
}
// Function to clear the box at any position
void clearBox(int x, int y) {
screen.fillRect(x, y, boxSize, boxSize, ILI9341_BLACK);
}
// Function to move smoothly with incremental steps and blinking effect
void moveBoxSmooth(int fromX, int fromY, int toX, int toY) {
int steps = 12; // Number of steps for smoother movement
int stepX = (toX - fromX) / steps; // X-axis step size
int stepY = (toY - fromY) / steps; // Y-axis step size
int blinkInterval = 2; // Number of steps between each blink
int delayTime = 10; // Delay between each step
// Previous position
int previousX = fromX;
int previousY = fromY;
for (int i = 0; i <= steps; i+=4) {
// Calculate current position
int currentX = fromX + stepX * i;
int currentY = fromY + stepY * i;
// Blinking effect: alternate between drawing and clearing
if ((i / blinkInterval) % 2 == 0) {
// Draw the box at the current position
drawBox(currentX, currentY);
} else {
// Clear the box (blink effect)
clearBox(currentX, currentY);
}
// Clear the previous area
if (i > 0) { // Avoid clearing before drawing at the first step
clearBox(previousX, previousY);
}
// Update previous position
previousX = currentX;
previousY = currentY;
// Small delay to make the movement smooth
delay(delayTime);
}
// Ensure the box is drawn at the final position
drawBox(toX, toY);
}
// Function to find the highest potentiometer value
int getHighestPot() {
int highestValue = 0;
int highestIndex = 0;
for (int i = 0; i < 5; i++) {
int potValue = analogRead(potPins[i]);
if (potValue > highestValue) {
highestValue = potValue;
highestIndex = i;
}
}
return highestIndex;
}
void setup() {
screen.begin();
screen.fillScreen(ILI9341_BLACK);
screen.setTextColor(ILI9341_WHITE);
screen.setTextSize(2);
screen.setCursor(0, 0);
// Start with box at location 5 (center)
drawBox(positions[4][0], positions[4][1]);
}
void loop() {
// Get the highest potentiometer reading
int highestPotIndex = getHighestPot();
int targetPosition = targetPositions[highestPotIndex]; // Get the mapped position from the potentiometer
// If the position has changed, move the box smoothly
if (targetPosition != currentPosition) {
moveBoxSmooth(positions[currentPosition][0], positions[currentPosition][1],
positions[targetPosition][0], positions[targetPosition][1]);
currentPosition = targetPosition; // Update the current position
delay(2000);
currentPosition=2;
moveBoxSmooth(positions[currentPosition][0], positions[currentPosition][1],
positions[targetPosition][0], positions[targetPosition][1]);
}
delay(100); // Small delay to prevent excessive reading
}