I'm trying to use a Freematics OBD-II UART Adapter V2.1, with an Arduino UNO to display RPM and speed on to an Adafruit 1.14 TFT display. my code works for the most part, managing to connect to the adapter and display values, however there is a bug, if the integer rises above for example 10 than reduced it will display 9 as 90 or 7 as 70 and so on. the value once connection is successfully made (speed in this example) will display 0 than 1 through 9 will work, but once the value goes over 10 and than reduced the bug is present and 0 will be displayed with two zero now. any help would be appreciated.
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
#include <Adafruit_ST7789.h> // Hardware-specific library for ST7789
#include <SPI.h>
#include <OBD2UART.h>
COBD obd;
#define TFT_CS 10
#define TFT_RST 9 // Or set to -1 and connect to Arduino RESET pin
#define TFT_DC 8
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
int LEDBACK1 = 9;
int potPin = A1;
void setup() {
pinMode(LEDBACK1,OUTPUT);
Serial.begin(115200);
tft.init(135, 240);
tft.fillScreen(ST77XX_BLACK);
tft.setTextColor(ST77XX_GREEN,ST77XX_BLACK);
tft.setRotation(3);
tft.setTextSize(3);
tft.setCursor(50, 40);
tft.print("MPH-");
tft.print("XXX");
tft.setTextColor(ST77XX_GREEN,ST77XX_BLACK);
tft.setRotation(3);
tft.setTextSize(3);
tft.setCursor(50,10 );
tft.print("RPM-");
tft.print("XXXX");
obd.begin();
while (!obd.init());
//tft.initR(INITR_MINI160x80);
//tft.initR(INITR_MINI160x80_PLUGIN);
tft.fillScreen(ST77XX_BLACK);
tft.setTextColor(ST77XX_GREEN,ST77XX_BLACK);
tft.setRotation(3);
tft.setTextSize(2);
tft.setCursor(50, 40);
tft.println("OBD CONNECTED");
delay(5000);
tft.fillScreen(ST77XX_BLACK);
}
void loop() {
int potentiometerValue =
analogRead (potPin);
int brightness = potentiometerValue / 4;
analogWrite(LEDBACK1,brightness);
int value;
tft.setTextColor(ST77XX_GREEN,ST77XX_BLACK);
tft.setRotation(3);
tft.setTextSize(3);
tft.setCursor(50, 40);
tft.print("MPH-");
if (obd.readPID(PID_SPEED, value)){
tft.print(value);
}
int value2;
tft.setTextColor(ST77XX_GREEN,ST77XX_BLACK);
tft.setRotation(3);
tft.setTextSize(3);
tft.setCursor(50,10 );
tft.print("RPM-");
if (obd.readPID(PID_RPM, value2)) {
tft.print(value2);
}
}
Thank you for replying, is there a chance you could show an example on how i might implement one of these into my code because I don't believe I would know how to do these, clearing the display takes a while I agree with you, I don't want to do that.
I appreciate it, I think I get the gist I was able to successfully write in more code that converted kilometers per hour to miles per hour. What dose the buffer [21]; do thought? And is there a chance you would happen to know a command that could kill the last two digits of the RPM so they were just zeros, example 2347 it would display 2300?
It creates a string (array of char) with enough space for 20 characters plus the null-terminator, for sprintf() to write the formatted values into so they can be printed to the display.