Something like this:
#include <Arduino_GigaDisplay_GFX.h>
GigaDisplay_GFX display; // Initialize the display with correct resolution for GIGA Display Shield
// Constants for gauge dimensions and centering
const int xmax = 800;
const int ymax = 480;
const int xcenter = xmax / 2;
const int ycenter = ymax / 2;
const int arc = ymax / 5;
#define BLACK 0x0000
#define GREEN 0x07E0
float potmeterPin = A1; // Pin for the potentiometer
int p;
int w;
int m;
float angle;
const char* labels[] = { "LOAD", "COOLANT", "INTAKE", "VOLT" };
int labelXpos[] = { 53, 45, 49, 53 };
float prev_x1 = 0;
float prev_y1 = 0;
void drawGauge(float angle) {
display.drawCircleHelper(xcenter, ycenter, 100, 1, GREEN);
display.drawCircleHelper(xcenter, ycenter, 99, 1, GREEN);
display.drawCircleHelper(xcenter, ycenter, 98, 1, GREEN);
display.drawCircleHelper(xcenter, ycenter, 100, 2, GREEN);
display.drawCircleHelper(xcenter, ycenter, 99, 2, GREEN);
display.drawCircleHelper(xcenter, ycenter, 98, 2, GREEN);
// Calculate the needle's position
float x1 = sin(2 * angle * 2 * 3.14159 / 360);
float y1 = cos(2 * angle * 2 * 3.14159 / 360);
display.drawLine(xcenter, ycenter, xcenter + arc * x1, ycenter - arc * y1, GREEN);
display.fillCircle(xcenter, ycenter, 5, GREEN);
prev_x1 = x1;
prev_y1 = y1;
// Set font and draw gauge labels
display.setTextSize(2);
display.setCursor(labelXpos[0], 32);
display.print(labels[0]);
// Display the numeric value
display.setCursor(54, 60);
if (w < 10) {
display.print('0');
}
display.print(w);
}
void updateGauge(float angle) {
// Calculate the needle's position
float x1 = sin(2 * angle * 2 * 3.14159 / 360);
float y1 = cos(2 * angle * 2 * 3.14159 / 360);
display.drawLine(xcenter, ycenter, xcenter + arc * prev_x1, ycenter - arc * prev_y1, BLACK);
display.drawLine(xcenter, ycenter, xcenter + arc * x1, ycenter - arc * y1, GREEN);
display.fillCircle(xcenter, ycenter, 5, GREEN);
prev_x1 = x1;
prev_y1 = y1;
// Set font and draw gauge labels
display.setTextSize(2);
display.setCursor(labelXpos[0], 32);
display.print(labels[0]);
// Display the numeric value
display.setCursor(54, 60);
if (w < 10) {
display.print('0');
}
display.print(w);
}
void setup() {
while(!Serial && millis() < 4000){}
Serial.begin(115200);
display.begin(); // Initialize the display
display.setRotation(1);
display.fillScreen(BLACK);
display.setTextColor(GREEN); // Set text color to white
pinMode(A1, INPUT);
}
void loop() {
p = analogRead(potmeterPin); // Read the analog pin
w = map(p, 0, 1023, 0, 100); // Map the read value to 0-100
m = map(p, 0, 1023, 0, 90); // Map the needle movement
angle = m; // Adjust the angle for needle position
Serial.print("angle: ");
Serial.println(angle, 2);
if (angle < 45) {
angle = angle + 135;
} else {
angle = angle - 45;
}
static bool first_call = true;
if (first_call) {
drawGauge(angle);
first_call = false;
}
else updateGauge(angle);
delay(100);
}
But you will probably notice a lot of flickering of the needle...
You can reduce this several different ways, one is simply don't update if the values do not change less than some minimum... Maybe something like:
#include <Arduino_GigaDisplay_GFX.h>
GigaDisplay_GFX display; // Initialize the display with correct resolution for GIGA Display Shield
// Constants for gauge dimensions and centering
const int xmax = 800;
const int ymax = 480;
const int xcenter = xmax / 2;
const int ycenter = ymax / 2;
const int arc = ymax / 5;
#define BLACK 0x0000
#define GREEN 0x07E0
float potmeterPin = A1; // Pin for the potentiometer
int previous_p = -100; // some value out of range of what p can be.
#define MIN_P_DELTA 2 // how far P needs to change before you update.
int p;
int w;
int m;
float angle;
const char* labels[] = { "LOAD", "COOLANT", "INTAKE", "VOLT" };
int labelXpos[] = { 53, 45, 49, 53 };
float prev_x1 = 0;
float prev_y1 = 0;
void drawGauge(float angle) {
display.drawCircleHelper(xcenter, ycenter, 100, 1, GREEN);
display.drawCircleHelper(xcenter, ycenter, 99, 1, GREEN);
display.drawCircleHelper(xcenter, ycenter, 98, 1, GREEN);
display.drawCircleHelper(xcenter, ycenter, 100, 2, GREEN);
display.drawCircleHelper(xcenter, ycenter, 99, 2, GREEN);
display.drawCircleHelper(xcenter, ycenter, 98, 2, GREEN);
// Calculate the needle's position
float x1 = sin(2 * angle * 2 * 3.14159 / 360);
float y1 = cos(2 * angle * 2 * 3.14159 / 360);
display.drawLine(xcenter, ycenter, xcenter + arc * x1, ycenter - arc * y1, GREEN);
display.fillCircle(xcenter, ycenter, 5, GREEN);
prev_x1 = x1;
prev_y1 = y1;
// Set font and draw gauge labels
display.setTextSize(2);
display.setCursor(labelXpos[0], 32);
display.print(labels[0]);
// Display the numeric value
display.setCursor(54, 60);
if (w < 10) {
display.print('0');
}
display.print(w);
}
void updateGauge(float angle) {
// Calculate the needle's position
float x1 = sin(2 * angle * 2 * 3.14159 / 360);
float y1 = cos(2 * angle * 2 * 3.14159 / 360);
display.drawLine(xcenter, ycenter, xcenter + arc * prev_x1, ycenter - arc * prev_y1, BLACK);
display.drawLine(xcenter, ycenter, xcenter + arc * x1, ycenter - arc * y1, GREEN);
display.fillCircle(xcenter, ycenter, 5, GREEN);
prev_x1 = x1;
prev_y1 = y1;
// Set font and draw gauge labels
display.setTextSize(2);
display.setCursor(labelXpos[0], 32);
display.print(labels[0]);
// Display the numeric value
display.setCursor(54, 60);
if (w < 10) {
display.print('0');
}
display.print(w);
}
void setup() {
while (!Serial && millis() < 4000) {}
Serial.begin(115200);
display.begin(); // Initialize the display
display.setRotation(1);
display.fillScreen(BLACK);
display.setTextColor(GREEN); // Set text color to white
pinMode(A1, INPUT);
}
void loop() {
p = analogRead(potmeterPin); // Read the analog pin
if (abs(p - previous_p) > MIN_P_DELTA) {
w = map(p, 0, 1023, 0, 100); // Map the read value to 0-100
m = map(p, 0, 1023, 0, 90); // Map the needle movement
angle = m; // Adjust the angle for needle position
Serial.print("angle: ");
Serial.println(angle, 2);
if (angle < 45) {
angle = angle + 135;
} else {
angle = angle - 45;
}
static bool first_call = true;
if (first_call) {
drawGauge(angle);
first_call = false;
} else updateGauge(angle);
previous_p = p;
}
delay(100);
}
Where I check to see if the p value changes more than:
#define MIN_P_DELTA 2 // how far P needs to change before you update.
Which I defined here as 2, but you could try 1 and see if that is sufficient... There are several other approaches like averaging the last n inputs and the like, but hopefully this gives you some starting point