Code: Select all
#include <OBD2UART.h>
#include <Arduino.h>
#include <TM1637Display.h>
//set display pins here
#define CLK 2
#define DIO 3
COBD obd;
TM1637Display display(CLK, DIO);
void setup()
{
obd.begin();
// initiate OBD-II connection until success
while (!obd.init());
}
void loop()
{
int k;
uint8_t data[] = { 0xff, 0xff, 0xff, 0xff };
uint8_t blank[] = { 0x00, 0x00, 0x00, 0x00 };
display.setBrightness(0x0f);
//read fuel mass flow rate
int maf;
obd.readPID(PID_MAF_FLOW, maf);
//read vehicle speed
int vss;
obd.readPID(PID_SPEED, vss);
//fancy math to make units good
vss = vss * 710.7;
float mpg;
mpg = vss / maf;
mpg = mpg * 100;
//output mpg to display with leading zeros
display.showNumberDec(mpg, true);
delay(100);
}