Issue with extra zeros with OBD-II Adapter

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);
  }

}

Welcome to the forum

The problem occurs because you are not clearing the previously printed second digit when subsequently printing a single digit value

Possible solutions

  • clear the display before printing the new value. This is a horrible solution. Don't do it
  • Always print a space after the value whether or not it has 1 or 2 digits. Not a bad solution if there is always room to do it
  • Print the previous value in the background colour to erase it before printing the new one. Can seem complicated to do
  • Only print a space after single digit values. A good solution and easy to do
#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;
char buffer[21];

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)){
    sprintf(buffer, "%3i", value);
    tft.print(buffer);
  }


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)) {
    sprintf(buffer, "%4i", value2);
    tft.print(buffer);
  }

}

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 just saw this let me try it!

I works perfectly now thank you very much for the help!

Any questions? I only did half a job if you don't understand how the code I added works, so you can use it in other situations.

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.

  if (obd.readPID(PID_RPM, value2)) {
    value2 -= value2 % 100;
    sprintf(buffer, "%4i", value2);
    tft.print(buffer);
  }

I think that about answers my questions, I inputted that other code you gave me a worked perfectly, thank you very much for the help.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.