This is building on top of my last post.... So you can certainly isolate / remove some bits:
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#define TFT_CS 10
#define TFT_DC 9
#define SCREEN_HEIGHT 240
#define SCREEN_WIDTH 320
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
#define arrSize(X) sizeof(X) / sizeof(X[0])
int course = 310;
int target = 27;
int circleRadius = 50;
int bearings[] = { 0, 22.5, 45, 67.5, 90, 112.5, 135, 157.5, 180, 202.5, 225, 247.5, 270, 292.5, 315, 337.5, 360 };
const char* bearingStrings[] = { "N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW", "N" };
void turnLeft() {
Serial.println("Left");
}
void turnRight() {
Serial.println("Right");
}
// returns the array element ID for the nearest bearing to the target number
int nearestBearing(int& target) {
int idx = 0; // by default near first element
int distance = abs(bearings[idx] - target);
for (int i = 1; i < arrSize(bearings); i++) {
int d = abs(bearings[i] - target);
if (d < distance) {
idx = i;
distance = d;
}
else return idx;
}
return idx;
}
void setup() {
Serial.begin(9600);
tft.begin();
tft.setRotation(1);
tft.fillScreen(ILI9341_BLACK);
Serial.println((String)"Current course is: " + bearingStrings[nearestBearing(course)] + " (" + course + " degrees)");
Serial.println((String)"Target course is: " + bearingStrings[nearestBearing(target)] + " (" + target + " degrees)");
Serial.print("Suggested movement: ");
int dif = target - course < 0 ? (360 + target - course) : (target - course);
dif < 180 ? turnRight() : turnLeft();
tft.setCursor(10, 10);
tft.setTextSize(1);
tft.setTextColor(ILI9341_GREEN);
tft.print("Current Bearing In GREEN");
tft.setCursor(10, 25);
tft.setTextSize(1);
tft.setTextColor(ILI9341_RED);
tft.print("Target Bearing In RED");
tft.fillCircle(tft.width() / 2, tft.height() / 2, circleRadius, ILI9341_BLUE);
// Draw the current course bearing line in GREEN
tft.drawLine(
tft.width() / 2,
tft.height() / 2,
tft.width() / 2 + circleRadius * sin(course * M_PI / 180.),
tft.height() / 2 - circleRadius * cos(course * M_PI / 180.),
ILI9341_GREEN);
// Draw the target course bearing line in RED
tft.drawLine(
tft.width() / 2,
tft.height() / 2,
tft.width() / 2 + circleRadius * sin(target * M_PI / 180.),
tft.height() / 2 - circleRadius * cos(target * M_PI / 180.),
ILI9341_RED);
}
void loop() { }
See it in action: TFT-Display - Wokwi Arduino and ESP32 Simulator