#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <SPI.h>
// Display pins for Arduino Mega
#define TFT_CS 53
#define TFT_RST 48
#define TFT_DC 49
#define TFT_MOSI 51
#define TFT_MISO 50
#define TFT_SCK 52
// Initialize display
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
// Military colors
#define BLACK 0x0000
#define WHITE 0xFFFF
#define GREEN 0x07E0
#define DARK_GREEN 0x03E0
#define RED 0xF800
#define YELLOW 0xFFE0
#define BLUE 0x001F
#define ORANGE 0xFD20
#define GRAY 0x8410
// Unit structure
struct Unit {
char name[10];
int tanks;
int apcs;
int artillery;
int troops;
int x, y;
};
Unit units[5];
void setup() {
Serial.begin(9600);
Serial.println("Military Forces Display");
tft.begin();
tft.setRotation(1);
initializeUnits();
drawMilitaryDisplay();
}
void initializeUnits() {
// Unit 1 - North
strcpy(units[0].name, "NORTH");
units[0].tanks = 45;
units[0].apcs = 30;
units[0].artillery = 15;
units[0].troops = 1200;
units[0].x = 80;
units[0].y = 60;
// Unit 2 - East
strcpy(units[1].name, "EAST");
units[1].tanks = 60;
units[1].apcs = 40;
units[1].artillery = 20;
units[1].troops = 1800;
units[1].x = 240;
units[1].y = 80;
// Unit 3 - South
strcpy(units[2].name, "SOUTH");
units[2].tanks = 35;
units[2].apcs = 25;
units[2].artillery = 12;
units[2].troops = 900;
units[2].x = 200;
units[2].y = 180;
// Unit 4 - West
strcpy(units[3].name, "WEST");
units[3].tanks = 50;
units[3].apcs = 35;
units[3].artillery = 18;
units[3].troops = 1500;
units[3].x = 80;
units[3].y = 160;
// Unit 5 - Reserve
strcpy(units[4].name, "RESERVE");
units[4].tanks = 25;
units[4].apcs = 15;
units[4].artillery = 8;
units[4].troops = 600;
units[4].x = 160;
units[4].y = 120;
}
void drawMilitaryDisplay() {
// Clean black background
tft.fillScreen(BLACK);
// Draw title
drawTitle();
// Draw each unit
for (int i = 0; i < 5; i++) {
drawUnitInfo(units[i]);
}
// Draw connections between units
drawUnitConnections();
// Draw summary at bottom
drawForceSummary();
// Draw legend
drawLegend();
// Draw compass
drawCompass();
}
void drawTitle() {
// Title bar
tft.fillRect(0, 0, 320, 20, DARK_GREEN);
tft.drawRect(0, 0, 320, 20, GREEN);
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.setCursor(40, 2);
tft.print("MILITARY FORCES");
tft.setTextSize(1);
tft.setCursor(270, 6);
tft.print("06:00Z");
}
void drawUnitInfo(Unit u) {
int x = u.x;
int y = u.y;
// Unit box
tft.drawRect(x - 40, y - 40, 80, 60, WHITE);
// Unit name
tft.setTextColor(YELLOW);
tft.setTextSize(1);
// Center the name
int nameWidth = strlen(u.name) * 6;
tft.setCursor(x - nameWidth/2, y - 35);
tft.print(u.name);
// Tanks
tft.setTextColor(WHITE);
tft.setCursor(x - 35, y - 25);
tft.print("Tanks:");
tft.setTextColor(GREEN);
tft.setTextSize(2);
tft.setCursor(x - 35, y - 35);
tft.print(u.tanks);
// APCs
tft.setTextColor(WHITE);
tft.setTextSize(1);
tft.setCursor(x + 10, y - 25);
tft.print("APCs:");
tft.setTextColor(GREEN);
tft.setTextSize(2);
tft.setCursor(x + 10, y - 35);
tft.print(u.apcs);
// Artillery
tft.setTextColor(WHITE);
tft.setTextSize(1);
tft.setCursor(x - 35, y - 5);
tft.print("Arty:");
tft.setTextColor(GREEN);
tft.setTextSize(2);
tft.setCursor(x - 35, y - 15);
tft.print(u.artillery);
// Troops (smaller font for large numbers)
tft.setTextColor(WHITE);
tft.setTextSize(1);
tft.setCursor(x + 10, y - 5);
tft.print("Troops:");
tft.setTextColor(GREEN);
tft.setTextSize(1);
tft.setCursor(x + 10, y - 15);
tft.print(u.troops);
// Small unit icon
tft.fillRect(x - 15, y + 5, 30, 3, GREEN);
tft.fillCircle(x - 5, y + 10, 3, DARK_GREEN);
tft.fillCircle(x + 5, y + 10, 3, DARK_GREEN);
}
void drawUnitConnections() {
// Draw lines connecting units to HQ (center)
for (int i = 0; i < 5; i++) {
// Draw dashed lines
for (int j = 0; j < 10; j++) {
int x1 = units[i].x + (160 - units[i].x) * j / 10;
int y1 = units[i].y + (120 - units[i].y) * j / 10;
int x2 = units[i].x + (160 - units[i].x) * (j + 0.5) / 10;
int y2 = units[i].y + (120 - units[i].y) * (j + 0.5) / 10;
tft.drawLine(x1, y1, x2, y2, DARK_GREEN);
}
}
// HQ marker
tft.fillCircle(160, 120, 5, RED);
tft.drawCircle(160, 120, 8, YELLOW);
tft.setTextColor(WHITE);
tft.setTextSize(1);
tft.setCursor(150, 100);
tft.print("HQ");
}
void drawForceSummary() {
// Calculate totals
int totalTanks = 0, totalAPCs = 0, totalArty = 0, totalTroops = 0;
for (int i = 0; i < 5; i++) {
totalTanks += units[i].tanks;
totalAPCs += units[i].apcs;
totalArty += units[i].artillery;
totalTroops += units[i].troops;
}
// Summary box
tft.fillRect(0, 200, 320, 40, DARK_GREEN);
tft.drawRect(0, 200, 320, 40, GREEN);
tft.setTextColor(WHITE);
tft.setTextSize(1);
// Row 1
tft.setCursor(10, 205);
tft.print("TOTAL FORCES");
tft.setCursor(120, 205);
tft.print("Tanks:");
tft.setTextColor(YELLOW);
tft.setCursor(160, 205);
tft.print(totalTanks);
tft.setTextColor(WHITE);
tft.setCursor(200, 205);
tft.print("APCs:");
tft.setTextColor(YELLOW);
tft.setCursor(240, 205);
tft.print(totalAPCs);
// Row 2
tft.setTextColor(WHITE);
tft.setCursor(10, 220);
tft.print("Artillery:");
tft.setTextColor(YELLOW);
tft.setCursor(70, 220);
tft.print(totalArty);
tft.setTextColor(WHITE);
tft.setCursor(120, 220);
tft.print("Troops:");
tft.setTextColor(YELLOW);
tft.setCursor(170, 220);
tft.print(totalTroops);
tft.setTextColor(WHITE);
tft.setCursor(240, 220);
tft.print("Units: 5");
}
void drawLegend() {
// Legend box
tft.fillRect(250, 150, 60, 40, BLACK);
tft.drawRect(250, 150, 60, 40, GRAY);
tft.setTextColor(GRAY);
tft.setTextSize(1);
tft.setCursor(260, 155);
tft.print("LEGEND");
// Tanks
tft.setTextColor(WHITE);
tft.setCursor(255, 165);
tft.print("T");
tft.fillRect(260, 163, 10, 4, GREEN);
// APCs
tft.setCursor(275, 165);
tft.print("A");
tft.fillCircle(285, 165, 3, GREEN);
// Artillery
tft.setCursor(255, 175);
tft.print("G");
tft.drawLine(265, 175, 275, 170, GREEN);
}
void drawCompass() {
// Simple compass
int cx = 290;
int cy = 35;
tft.drawCircle(cx, cy, 10, WHITE);
tft.drawLine(cx, cy - 8, cx, cy - 12, RED);
tft.drawLine(cx, cy + 8, cx, cy + 12, WHITE);
tft.drawLine(cx - 8, cy, cx - 12, cy, WHITE);
tft.drawLine(cx + 8, cy, cx + 12, cy, WHITE);
tft.setTextColor(RED);
tft.setCursor(cx - 3, cy - 20);
tft.print("N");
tft.setTextColor(WHITE);
tft.setCursor(cx - 3, cy + 10);
tft.print("S");
tft.setCursor(cx - 15, cy - 5);
tft.print("W");
tft.setCursor(cx + 10, cy - 5);
tft.print("E");
}
void loop() {
// Update numbers randomly for demonstration
static int counter = 0;
counter++;
if (counter % 100 == 0) {
// Update one unit
int unitNum = random(0, 5);
// Clear old unit info
tft.fillRect(units[unitNum].x - 41, units[unitNum].y - 41,
82, 62, BLACK);
// Update numbers slightly
units[unitNum].tanks += random(-1, 2);
units[unitNum].apcs += random(-1, 2);
// Keep numbers positive
if (units[unitNum].tanks < 20) units[unitNum].tanks = 20;
if (units[unitNum].apcs < 10) units[unitNum].apcs = 10;
// Redraw unit
drawUnitInfo(units[unitNum]);
// Update summary
tft.fillRect(0, 200, 320, 40, DARK_GREEN);
tft.drawRect(0, 200, 320, 40, GREEN);
drawForceSummary();
}
delay(50);
}