/*
Arduino | coding-help
Screen Flicker when updating values
Syn [puffy], puffy - Tuesday, May 5, 2026 5:26 PM
Trying to make a custom digital boost/vacuum gauge with a map sensor.
I finally got the values to be stable but it's causing my SPI screen to flicker.
Based on:
https://wokwi.com/projects/307567201804616256
*/
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
const int TFT_DC = 9;
const int TFT_CS = 10;
const int SENSOR_PIN = A0;
int oldValue = -1; // just so it is never the "raw value" on startup
// Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
void setup() {
Serial.begin(9600);
Serial.println("ILI9341 Test!");
tft.begin();
tft.setTextSize(3);
tft.setTextColor(ILI9341_GREEN, ILI9341_BLACK);
}
void loop(void) {
char buffer[8];
int rawValue = analogRead(SENSOR_PIN); // read the input pin
int mappedValue = map(rawValue, 0, 1023, -30, 20);
if (rawValue != oldValue) {
oldValue = rawValue;
Serial.print("Raw value: ");
Serial.println(rawValue); // print raw sensor reading
snprintf(buffer, 8, "%3d", mappedValue);
tft.setCursor(70, 50);
tft.print(buffer);
}
}