New!!OBD2 Com with Xsim3 and Arduino-New!!

Dashboard, gauge projects etc.
Please use the image gallery for your pictures, a short tutorial can be found here.
The first image in the first post will be shown in the project gallery.

Re: New!!OBD2 Com with Xsim3 and Arduino-New!!

Postby bahzitchiu » Mon 30. Jun 2014, 03:15

New code.



PS: the gear is blinking when rpm=95% max, but not appearing on video, sorry.

Cheers

[ ]´s
bahzitchiu
 
Posts: 9
Joined: Wed 18. Jun 2014, 05:33
Has thanked: 0 time
Been thanked: 8 times

Re: New!!OBD2 Com with Xsim3 and Arduino-New!!

Postby kbssa » Mon 30. Jun 2014, 04:11

hI, bahzitchiu

Please, share the code with us !
kbssa
 
Posts: 120
Images: 16
Joined: Thu 19. Apr 2012, 21:03
Has thanked: 5 times
Been thanked: 3 times

Re: New!!OBD2 Com with Xsim3 and Arduino-New!!

Postby bahzitchiu » Tue 1. Jul 2014, 02:15

kbssa wrote:hI, bahzitchiu

Please, share the code with us !


Foi mal kbessa :P
"My bad :P"

Its a compilation of several codes and some ideas, pls share for free.

Code: Select all
//  X-Sim Sample code for receiving OBD2 values from the extractor without using the converter
//  OBD2 is industry standard, X-Sim will additionally expand the OBD2 PID with gear and other values
//
//  Usage:
//  Arduino has to be connected to a free USB port on the computer where the extractor is running
//  Open the extractor and open the settings menu, there you have to select the OBD2 menu
//  Add now your arduino comport to the OBD2 list.
//  After closing the dialog you will see the arduino LED will be enabled which represents receiving data.
//  After closing the extractor application the LED will switch off.
//  Use this code to insert your own display code at the fitting positions
//  Copyright 2013 Martin Wiedenbauer, X-Sim.de
//  New procedures ideias by bahzitchiu
//  Share for free
//  References:
//  http://en.wikipedia.org/wiki/OBD-II_PIDs
//  http://elmelectronics.com/DSheets/ELM327DS.pdf
//  http://www.x-sim.de

#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
char gas[6] = "F";
char lugar[8] = "P";
char lap[7] = "L";
char rapport[5] = "Gear";
char trmin[5] = "RPM";
char speed1[8] = "SPEED";
char speedunit[5] = "KM/H";
char marcha[16] = "";
char message[16] = "";
unsigned int rpmmax = 100;
unsigned int rpmlast;
int setrpm = 0;
int mode = 1;
int rpm, speed, gear, position, currentlap, lapp, c,d;
long bestlaptime,lastlaptime,laptime,delta,time,alfa;
bool extractordetected=false;   
int receivebuffer[100]={
  0};      
byte bar1[8] =
{
  B11100,
  B11110,
  B11110,
  B11110,
  B11110,
  B11110,
  B11110,
  B11100
};
byte bar2[8] =
{
  B00111,
  B01111,
  B01111,
  B01111,
  B01111,
  B01111,
  B01111,
  B00111
};
byte bar3[8] =
{
  B11111,
  B11111,
  B00000,
  B00000,
  B00000,
  B00000,
  B11111,
  B11111
};
byte bar4[8] =
{
  B11110,
  B11100,
  B00000,
  B00000,
  B00000,
  B00000,
  B11000,
  B11100
};
byte bar5[8] =
{
  B01111,
  B00111,
  B00000,
  B00000,
  B00000,
  B00000,
  B00111,
  B01111
};
byte bar6[8] =
{
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B11111,
  B11111
};
byte bar7[8] =
{
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00111,
  B01111
};
byte bar8[8] =
{
  B11111,
  B11111,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000
};

void setup()

{
  lcd.createChar(1,bar1);
  lcd.createChar(2,bar2);
  lcd.createChar(3,bar3);
  lcd.createChar(4,bar4);
  lcd.createChar(5,bar5);
  lcd.createChar(6,bar6);
  lcd.createChar(7,bar7);
  lcd.createChar(8,bar8);

  lcd.begin(16, 2);
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);
  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.print("   X-SIM DASH");
  delay(3000);
  lcd.clear();
}

int HexToInt(int c)
{
  if (c >= '0' && c <= '9')
  {
    return c - '0';
  }
  else if (c >= 'a' && c <= 'f')
  {
    return c - 'a' + 10;
  }
  else if (c >= 'A' && c <= 'F')
  {
    return c - 'A' + 10;
  }
  else
  {
    return -1;
  }
}


int ReceiveValueWithTimeout(int receivetrigger, int commandhighbyte, int commandlowbyte, int receivelength)
{
  int arduinoserialbuffer = 0;
  int buffercount = -1;
  for (int z = 0; z < 1500; z++)
  {
    while (Serial.available())
    {
      if (buffercount == -1)
      {
        arduinoserialbuffer = Serial.read();
        if (arduinoserialbuffer != receivetrigger)
        {
          buffercount = -1;
        }
        else
        {
          buffercount = 0;
        }
      }
      else
      {
        arduinoserialbuffer = Serial.read();
        receivebuffer[buffercount] = arduinoserialbuffer;
        buffercount++;
        if (buffercount > receivelength)
        {
          return ParseReceiveBufferOffset(commandhighbyte, commandlowbyte, receivelength, 0);
          buffercount = -1;
        }
      }
    }
    delay(1);
  }
  return -1;
}

long ParseReceiveBufferOffset(int commandhighbyte, int commandlowbyte, int receivelength, int offset)
{

  if (receivebuffer[0] == '1' && receivebuffer[2] == commandhighbyte && receivebuffer[3] == commandlowbyte)
  {

    if (receivelength == 7)
    {
      int highresult = HexToInt(receivebuffer[offset + 5]);
      int lowresult = HexToInt(receivebuffer[offset + 6]);
      if (highresult == -1 || lowresult == -1){
        return -1;
      }
      return ((16 * highresult) + lowresult);
    }

    if (receivelength == 10)
    {
      int tophighresult = HexToInt(receivebuffer[offset + 5]);
      int toplowresult = HexToInt(receivebuffer[offset + 6]);
      int highresult = HexToInt(receivebuffer[offset + 8]);
      int lowresult = HexToInt(receivebuffer[offset + 9]);
      if (tophighresult == -1 || toplowresult == -1 || highresult == -1 || lowresult == -1){
        return -1;
      }
      return ((4096 * tophighresult) + (256 * toplowresult) + (16 * highresult) + lowresult);
    }

    if (receivelength == 16)
    {
      int tophighresult = HexToInt(receivebuffer[offset + 5]);
      int toplowresult = HexToInt(receivebuffer[offset + 6]);
      int highresult = HexToInt(receivebuffer[offset + 8]);
      int lowresult = HexToInt(receivebuffer[offset + 9]);
      int tophighresult2 = HexToInt(receivebuffer[offset + 11]);
      int toplowresult2 = HexToInt(receivebuffer[offset + 12]);
      int highresult2 = HexToInt(receivebuffer[offset + 14]);
      int lowresult2 = HexToInt(receivebuffer[offset + 15]);

      if (tophighresult2 == -1 || toplowresult2 == -1 || highresult2 == -1 || lowresult2 == -1 || tophighresult == -1 || toplowresult == -1 || highresult == -1 || lowresult == -1){
        return -1;
      }
      return ((268435456 * tophighresult) + (16777216 * toplowresult) + (1048576 * highresult) + (65536 * lowresult) + (4096 * tophighresult2) + (256 * toplowresult2) + (16 * highresult2) + lowresult2);
    }

  }
  return -1;
}

bool ReceiveAllValuesWithTimeout(int receivetrigger, int commandhighbyte, int commandlowbyte, int receivelength)
{
  int arduinoserialbuffer = 0;
  int buffercount = -1;
  for (int z = 0; z < 1500; z++)
  {
    while (Serial.available())
    {
      if (buffercount == -1)
      {
        arduinoserialbuffer = Serial.read();
        if (arduinoserialbuffer != receivetrigger)
        {
          buffercount = -1;
        }
        else
        {
          buffercount = 0;
        }
      }
      else
      {
        arduinoserialbuffer = Serial.read();
        receivebuffer[buffercount] = arduinoserialbuffer;
        buffercount++;
        if (buffercount > receivelength)
        {
          return true;
          buffercount = -1;
        }
      }
    }
    delay(1);
  }
  return false;
}

void getAllData()
{
  ClearReceiveBuffer();
  Serial.write('0');
  Serial.write('1');
  Serial.write('E');
  Serial.write('F');
  Serial.write('\r');
  if (ReceiveAllValuesWithTimeout('4', 'E', 'F', 42))
  {
    rpm = ParseReceiveBufferOffset('E', 'F', 10, 0);
    speed = ParseReceiveBufferOffset('E', 'F', 10, 6);
    gear = ParseReceiveBufferOffset('E', 'F', 7, 15);
    position = ParseReceiveBufferOffset('E', 'F', 7, 18);
    currentlap = ParseReceiveBufferOffset('E', 'F', 7, 21);
    bestlaptime = ParseReceiveBufferOffset('E', 'F', 16, 24);
  }
}

void ClearReceiveBuffer()
{
  while (Serial.available())
  {
    Serial.read();
  }
}

void SendEchoDisabled()
{

  Serial.write('A');
  Serial.write('T');
  Serial.write('E');
  Serial.write('0');
  Serial.write('\r');
}


int GetOBD2RpmValue()
{

  ClearReceiveBuffer();
  Serial.write('0');
  Serial.write('1');
  Serial.write('0');
  Serial.write('c');
  Serial.write('\r');
  return ReceiveValueWithTimeout('4', '0', 'C', 10);
}


int GetOBD2SpeedValue()
{

  ClearReceiveBuffer();
  Serial.write('0');
  Serial.write('1');
  Serial.write('0');
  Serial.write('d');
  Serial.write('\r');
  return ReceiveValueWithTimeout('4', '0', 'D', 7);
}


int GetOBD2GearValue()
{

  ClearReceiveBuffer();
  Serial.write('0');
  Serial.write('1');
  Serial.write('e');
  Serial.write('0');
  Serial.write('\r');
  return ReceiveValueWithTimeout('4', 'E', '0', 7);
}


int GetOBD2PositionValue()
{

  ClearReceiveBuffer();
  Serial.write('0');
  Serial.write('1');
  Serial.write('e');
  Serial.write('1');
  Serial.write('\r');
  return ReceiveValueWithTimeout('4', 'E', '1', 7);
}

int GetOBD2SpeedValue_new()
{

  ClearReceiveBuffer();
  Serial.write('0');
  Serial.write('1');
  Serial.write('e');
  Serial.write('1');
  Serial.write('\r');
  return ReceiveValueWithTimeout('4', 'E', '1', 7);
}

void loop()
{

  int          i;
  char*          neutral = "n";             
  char*          reverse = "r";               
  unsigned int    rpmleds;
  char          message[8];     
  String          output;
  int            hours;
  int            mins;
  int            secs;
  int            millisec;
  long         inttime;

  gear = GetOBD2GearValue();
  c=0;
  d=0;
  while (gear >= 0)
  {

    if (extractordetected == false)
    {
      SendEchoDisabled();   
      extractordetected = true;
    }

    getAllData();
    lapp=currentlap;








    if (rpmmax < rpmlast)
      rpmmax = rpmlast + 100;



    if (gear==0){

      sprintf(message,"%s:%s",rapport, reverse);
      lcd.setCursor(4, 1);
      lcd.write(2); 
      lcd.write(8);
      rpmleds = map(rpm, 0, rpmmax, 0, 20);
      if (rpmleds >= 19) {
        delay(100);
        lcd.clear();
      }

    }



    if (gear==1){
      sprintf(marcha,neutral);
      lcd.setCursor(4, 1);
      lcd.write(2); 
      lcd.write(8);
      lcd.write(1);
      rpmleds = map(rpm, 0, rpmmax, 0, 20);
      if (rpmleds >= 19) {
        delay(100);
        lcd.clear();
      }
    }



    if (gear==2){
      sprintf(message,"%s:%d",rapport, gear-1);
      lcd.setCursor(4,0);
      lcd.write(32);
      lcd.write(32);
      lcd.write(1);
      lcd.setCursor(4,1);
      lcd.write(32);
      lcd.write(32);
      lcd.write(1);
      rpmleds = map(rpm, 0, rpmmax, 0, 20);
      if (rpmleds >= 19) {
        delay(100);
        lcd.clear();
      }
    }


    if (gear==3){
      sprintf(message,"%s:%d",rapport, gear-1);
      lcd.setCursor(4,0);
      lcd.write(5);
      lcd.write(3);
      lcd.write(1);
      lcd.setCursor(4, 1);
      lcd.write(2);
      lcd.write(6);
      lcd.write(6);
      rpmleds = map(rpm, 0, rpmmax, 0, 20);
      if (rpmleds >= 19) {
        delay(100);
        lcd.clear();
      }
    }



    if (gear==4){
      sprintf(message,"%s:%d",rapport, gear-1);
      lcd.setCursor(4,0);
      lcd.write(5);
      lcd.write(3);
      lcd.write(1);
      lcd.setCursor(4, 1);
      lcd.write(7);
      lcd.write(6);
      lcd.write(1);
      rpmleds = map(rpm, 0, rpmmax, 0, 20);
      if (rpmleds >= 19) {
        delay(100);
        lcd.clear();
      }
    }



    if (gear==5){
      sprintf(message,"%s:%d",rapport, gear-1);
      lcd.setCursor(4,0);
      lcd.write(2);
      lcd.write(6);
      lcd.write(1);
      lcd.setCursor(4, 1);
      lcd.write(32);
      lcd.write(32);
      lcd.write(1);
      rpmleds = map(rpm, 0, rpmmax, 0, 20);
      if (rpmleds >= 19) {
        delay(100);
        lcd.clear();
      }
    }



    if (gear==6){
      sprintf(message,"%s:%d",rapport, gear-1);
      lcd.setCursor(4,0);
      lcd.write(2);
      lcd.write(3);
      lcd.write(4);
      lcd.setCursor(4, 1);
      lcd.write(7);
      lcd.write(6);
      lcd.write(1);
      rpmleds = map(rpm, 0, rpmmax, 0, 20);
      if (rpmleds >= 19) {
        delay(100);
        lcd.clear();
      }

    }



    if (gear==7){
      sprintf(message,"%s:%d",rapport, gear-1);
      lcd.setCursor(4,0);
      lcd.write(2);
      lcd.write(3);
      lcd.write(4);
      lcd.setCursor(4, 1);
      lcd.write(2);
      lcd.write(6);
      lcd.write(1);
      rpmleds = map(rpm, 0, rpmmax, 0, 20);
      if (rpmleds >= 19) {
        delay(100);
        lcd.clear();
      }

    }



    if (gear==8){
      sprintf(message,"%s:%d",rapport, gear-1);
      lcd.setCursor(4,0);
      lcd.write(2);
      lcd.write(8);
      lcd.write(1);
      lcd.setCursor(4, 1);
      lcd.write(32);
      lcd.write(32);
      lcd.write(1);
      rpmleds = map(rpm, 0, rpmmax, 0, 20);
      if (rpmleds >= 19) {
        delay(100);
        lcd.clear();
      }

    }



    if (gear==9){
      sprintf(message,"%s:%d",rapport, gear-1);
      lcd.setCursor(4, 0);
      lcd.write(2); 
      lcd.write(3);
      lcd.write(1);
      lcd.setCursor(4, 1);
      lcd.write(2); 
      lcd.write(6); 
      lcd.write(1);
      rpmleds = map(rpm, 0, rpmmax, 0, 20);
      if (rpmleds >= 19) {
        delay(100);
        lcd.clear();
      }

    }



    if (gear==10){
      sprintf(message,"%s:%d",rapport, gear-1);
      lcd.setCursor(4, 0);
      lcd.write(2); 
      lcd.write(3);
      lcd.write(1);
      lcd.setCursor(4, 1);
      lcd.write(7); 
      lcd.write(6); 
      lcd.write(1);
      rpmleds = map(rpm, 0, rpmmax, 0, 20);
      if (rpmleds >= 19) {
        delay(100);
        lcd.clear();
      }
    }

    {
      sprintf(message,"P%02d",position);
      lcd.setCursor(13,0);
      lcd.write(message);
      sprintf(message,"%3d",speed);
      lcd.setCursor(0,2);
      lcd.write(message);
      sprintf(message,"L%02d", currentlap);
      lcd.setCursor(0,0);
      lcd.write(message);

      if (bestlaptime < lastlaptime){
        sprintf(message, "BEST");
        lcd.setCursor(8,0);
        lcd.write(message);
      }
      else{
        sprintf(message, "    ");
        lcd.setCursor(8,0);
        lcd.write(message);
      }




      if (bestlaptime == -1)
      {
        sprintf(message, "000000");
      }
      else
      {
        inttime = bestlaptime / 1000;
        millisec = bestlaptime % 1000;
        hours = inttime / 3600;
        inttime = inttime % 3600;
        mins = inttime / 60;
        inttime = inttime % 60;
        secs = inttime;
        sprintf(message, "%1d:%02d.%003d", mins, secs, millisec);
        lcd.setCursor(8,1);
        lcd.write(message);
        lastlaptime=bestlaptime;
      }
      rpmlast = rpm;
    }
  }
  extractordetected = false;
  digitalWrite(13, HIGH);
  lcd.setCursor(0,0);
  lcd.write("[<-OPEN X-SIM->]");
  lcd.setCursor(0,2);
  lcd.write(" GAME STOCK CAR");
  delay(2000);
  lcd.clear(); 
}



PS: This code is working on game stock car extreme, rfactor 2, assetto corsa.

PS2: to gear fix on assetto corsa change "gear == x" to "gear == x+1";

ps3: sorry for my bad coding :/


This code is for euro truck 2 with 12 gears :P

Code: Select all
//  X-Sim Sample code for receiving OBD2 values from the extractor without using the converter
//  OBD2 is industry standard, X-Sim will additionally expand the OBD2 PID with gear and other values
//
//  Usage:
//  Arduino has to be connected to a free USB port on the computer where the extractor is running
//  Open the extractor and open the settings menu, there you have to select the OBD2 menu
//  Add now your arduino comport to the OBD2 list.
//  After closing the dialog you will see the arduino LED will be enabled which represents receiving data.
//  After closing the extractor application the LED will switch off.
//  Use this code to insert your own display code at the fitting positions
//  Copyright 2013 Martin Wiedenbauer, X-Sim.de
//
//  References:
//  http://en.wikipedia.org/wiki/OBD-II_PIDs
//  http://elmelectronics.com/DSheets/ELM327DS.pdf
//  http://www.x-sim.de

#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);

char rapport[5] = "Gear";
char trmin[5] = "RPM";
char speed1[8] = "SPEED";
char speedunit[5] = "KM/H";
char marcha[16] = "";
char message[16] = "";
unsigned int rpmmax = 100;
unsigned int rpmlast;
int setrpm = 0;
int mode = 1;
int rpm, speed, coolant, gear, position, currentlap, fuel,c;
long bestlaptime;
bool extractordetected=false;   
int receivebuffer[100]={
  0};      


byte bar1[8] =
{
  B11100,
  B11110,
  B11110,
  B11110,
  B11110,
  B11110,
  B11110,
  B11100
};
byte bar2[8] =
{
  B00111,
  B01111,
  B01111,
  B01111,
  B01111,
  B01111,
  B01111,
  B00111
};
byte bar3[8] =
{
  B11111,
  B11111,
  B00000,
  B00000,
  B00000,
  B00000,
  B11111,
  B11111
};
byte bar4[8] =
{
  B11110,
  B11100,
  B00000,
  B00000,
  B00000,
  B00000,
  B11000,
  B11100
};
byte bar5[8] =
{
  B01111,
  B00111,
  B00000,
  B00000,
  B00000,
  B00000,
  B00111,
  B01111
};
byte bar6[8] =
{
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B11111,
  B11111
};
byte bar7[8] =
{
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00111,
  B01111
};
byte bar8[8] =
{
  B11111,
  B11111,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000
};




void setup()

{
  // assignes each segment a write number
  lcd.createChar(1,bar1);
  lcd.createChar(2,bar2);
  lcd.createChar(3,bar3);
  lcd.createChar(4,bar4);
  lcd.createChar(5,bar5);
  lcd.createChar(6,bar6);
  lcd.createChar(7,bar7);
  lcd.createChar(8,bar8);

  // sets the LCD's rows and colums:
  lcd.begin(16, 2);
  pinMode(13, OUTPUT); //Arduino UNO LED off
  digitalWrite(13, HIGH);
  Serial.begin(9600);//9600
  //Do here stuff to init display and zero to default
  lcd.begin(16, 2);
  lcd.print("   X-SIM DASH");
  delay(3000);
  lcd.clear();
}

int HexToInt(int c)
{
  if (c >= '0' && c <= '9')
  {
    return c - '0';
  }
  else if (c >= 'a' && c <= 'f')
  {
    return c - 'a' + 10;
  }
  else if (c >= 'A' && c <= 'F')
  {
    return c - 'A' + 10;
  }
  else
  {
    return -1; // getting here is bad: it means the character was invalid
  }
}

//This function will wait for the first character in receivetrigger and will parse the result
int ReceiveValueWithTimeout(int receivetrigger, int commandhighbyte, int commandlowbyte, int receivelength)
{
  int arduinoserialbuffer = 0;
  int buffercount = -1;
  for (int z = 0; z < 1500; z++) //500ms timeout should be enough
  {
    while (Serial.available())
    {
      if (buffercount == -1)
      {
        arduinoserialbuffer = Serial.read();
        if (arduinoserialbuffer != receivetrigger) //Wait until the trigger is reached, ignore echo, first character is not in the buffer
        {
          buffercount = -1;
        }
        else
        {
          buffercount = 0;
        }
      }
      else
      {
        arduinoserialbuffer = Serial.read();
        receivebuffer[buffercount] = arduinoserialbuffer;
        buffercount++;
        if (buffercount > receivelength) //buffer has now waitlen character length
        {
          return ParseReceiveBufferOffset(commandhighbyte, commandlowbyte, receivelength, 0);
          buffercount = -1;
        }
      }
    }
    delay(1);
  }
  return -1;
}

long ParseReceiveBufferOffset(int commandhighbyte, int commandlowbyte, int receivelength, int offset)
{
  //First character is removed of the receivebuffer string and must not be verified again
  if (receivebuffer[0] == '1' && receivebuffer[2] == commandhighbyte && receivebuffer[3] == commandlowbyte)
  {
    //Parse 2 byte values on position 5 and 6 in the buffer string
    if (receivelength == 7)
    {
      int highresult = HexToInt(receivebuffer[offset + 5]);
      int lowresult = HexToInt(receivebuffer[offset + 6]);
      if (highresult == -1 || lowresult == -1){
        return -1;
      }
      return ((16 * highresult) + lowresult);
    }
    //Parse 4 byte values on position 5,6,8 and 9 in the buffer string
    if (receivelength == 10)
    {
      int tophighresult = HexToInt(receivebuffer[offset + 5]);
      int toplowresult = HexToInt(receivebuffer[offset + 6]);
      int highresult = HexToInt(receivebuffer[offset + 8]);
      int lowresult = HexToInt(receivebuffer[offset + 9]);
      if (tophighresult == -1 || toplowresult == -1 || highresult == -1 || lowresult == -1){
        return -1;
      }
      return ((4096 * tophighresult) + (256 * toplowresult) + (16 * highresult) + lowresult);
    }

    if (receivelength == 16)
    {
      int tophighresult = HexToInt(receivebuffer[offset + 5]);
      int toplowresult = HexToInt(receivebuffer[offset + 6]);
      int highresult = HexToInt(receivebuffer[offset + 8]);
      int lowresult = HexToInt(receivebuffer[offset + 9]);
      int tophighresult2 = HexToInt(receivebuffer[offset + 11]);
      int toplowresult2 = HexToInt(receivebuffer[offset + 12]);
      int highresult2 = HexToInt(receivebuffer[offset + 14]);
      int lowresult2 = HexToInt(receivebuffer[offset + 15]);

      if (tophighresult2 == -1 || toplowresult2 == -1 || highresult2 == -1 || lowresult2 == -1 || tophighresult == -1 || toplowresult == -1 || highresult == -1 || lowresult == -1){
        return -1;
      }
      return ((268435456 * tophighresult) + (16777216 * toplowresult) + (1048576 * highresult) + (65536 * lowresult) + (4096 * tophighresult2) + (256 * toplowresult2) + (16 * highresult2) + lowresult2);
    }

  }
  return -1; //Something is wrong with the returned OBD2 echo command byte
}

bool ReceiveAllValuesWithTimeout(int receivetrigger, int commandhighbyte, int commandlowbyte, int receivelength)
{
  int arduinoserialbuffer = 0;
  int buffercount = -1;
  for (int z = 0; z < 1500; z++) //500ms timeout should be enough
  {
    while (Serial.available())
    {
      if (buffercount == -1)
      {
        arduinoserialbuffer = Serial.read();
        if (arduinoserialbuffer != receivetrigger) //Wait until the trigger is reached, ignore echo, first character is not in the buffer
        {
          buffercount = -1;
        }
        else
        {
          buffercount = 0;
        }
      }
      else
      {
        arduinoserialbuffer = Serial.read();
        receivebuffer[buffercount] = arduinoserialbuffer;
        buffercount++;
        if (buffercount > receivelength) //buffer has now waitlen character length
        {
          return true;
          buffercount = -1;
        }
      }
    }
    delay(1);
  }
  return false;
}

void getAllData()
{
  ClearReceiveBuffer();
  Serial.write('0');
  Serial.write('1');
  Serial.write('E');
  Serial.write('F');
  Serial.write('\r');
  if (ReceiveAllValuesWithTimeout('4', 'E', 'F', 42))
  {
    rpm = ParseReceiveBufferOffset('E', 'F', 10, 0);
    speed = ParseReceiveBufferOffset('E', 'F', 10, 6);
    coolant = ParseReceiveBufferOffset('E', 'F', 7, 12);
    gear = ParseReceiveBufferOffset('E', 'F', 7, 15);
    position = ParseReceiveBufferOffset('E', 'F', 7, 18);
    currentlap = ParseReceiveBufferOffset('E', 'F', 7, 21);
    bestlaptime = ParseReceiveBufferOffset('E', 'F', 16, 24);
    fuel = ParseReceiveBufferOffset('E', 'F', 7, 36);
  }
}

void ClearReceiveBuffer()
{
  while (Serial.available())
  {
    Serial.read();
  }
}

void SendEchoDisabled() //not used here and a part of the OBD2 ELM327 specifications, as INFO
{
  //ATE0 Echo disabled
  Serial.write('A');
  Serial.write('T');
  Serial.write('E');
  Serial.write('0');
  Serial.write('\r');
}

//010c Request RPM, remember:  OBD2 RPM is ((A*256)+B)/4
int GetOBD2RpmValue()
{
  //010c
  ClearReceiveBuffer();
  Serial.write('0');
  Serial.write('1');
  Serial.write('0');
  Serial.write('c');
  Serial.write('\r');
  return ReceiveValueWithTimeout('4', '0', 'C', 10);
}

//010d Request speed in kmh
int GetOBD2SpeedValue()
{
  //010d
  ClearReceiveBuffer();
  Serial.write('0');
  Serial.write('1');
  Serial.write('0');
  Serial.write('d');
  Serial.write('\r');
  return ReceiveValueWithTimeout('4', '0', 'D', 7);
}

//01e0 Request gear number
int GetOBD2GearValue()
{
  //01e0
  ClearReceiveBuffer();
  Serial.write('0');
  Serial.write('1');
  Serial.write('e');
  Serial.write('0');
  Serial.write('\r');
  return ReceiveValueWithTimeout('4', 'E', '0', 7);
}

//01e1 Request current position
int GetOBD2PositionValue()
{
  //01e0
  ClearReceiveBuffer();
  Serial.write('0');
  Serial.write('1');
  Serial.write('e');
  Serial.write('1');
  Serial.write('\r');
  return ReceiveValueWithTimeout('4', 'E', '1', 7);
}

int GetOBD2SpeedValue_new()
{
  //01e0
  ClearReceiveBuffer();
  Serial.write('0');
  Serial.write('1');
  Serial.write('e');
  Serial.write('1');
  Serial.write('\r');
  return ReceiveValueWithTimeout('4', 'E', '1', 7);
}

int GetOBD2FuelValue()
{
  //01e0
  ClearReceiveBuffer();
  Serial.write('0');
  Serial.write('1');
  Serial.write('e');
  Serial.write('4');
  Serial.write('\r');
  return ReceiveValueWithTimeout('4', 'E', '4', 7);
}

//01e5 Request current joystick button status 0 to 7
int GetOBD2JoyStatus0to7()
{
  //01e5
  ClearReceiveBuffer();
  Serial.write('0');
  Serial.write('1');
  Serial.write('e');
  Serial.write('5');
  Serial.write('\r');
  return ReceiveValueWithTimeout('4', 'E', '5', 7);
}

void loop()
{
  //unsigned long currentMillis = millis();
  int          i;
  char*          neutral = "n";                // sets the character for neutral
  char*          reverse = "r";                // sets the character for reverse
  unsigned int    rpmleds; //holds the 8 leds values
  char          message[8];     // variable to display the message on the display
  int            fuel;
  int            joybutton;
  byte          button;
  String          output;
  int            dots = 40;
  int            hours;
  int            mins;
  int            secs;
  int            millisec;
  long         inttime;

  gear = GetOBD2GearValue();

  while (gear >= 0)
  {
    //unsigned long currentMillis = millis();   
    if (extractordetected == false)
    {
      SendEchoDisabled();   //Will cause a OK\r as answer from the extractor, ignored in this code
      extractordetected = true;
    }

    getAllData();

    speed == speed;

    if (rpmmax < rpmlast)
      rpmmax = rpmlast + 100;




    if (gear==0){
      sprintf(message,"%s:%s",rapport, reverse);
      lcd.setCursor(06, 1);
      lcd.write(2); 
      lcd.write(8);
      rpmleds = map(rpm, 0, rpmmax, 0, 20);
      if (rpmleds >= 19) {
        delay(100);
        lcd.clear();
      }
    }


    if (gear==1){
      sprintf(marcha,neutral);//escreve a marcha ponto morto
      lcd.setCursor(06, 1);
      lcd.write(2); 
      lcd.write(8);
      lcd.write(1);

      delay(100);
      lcd.clear();



    }
    if (gear==2){
      sprintf(message,"%s:%d",rapport, gear-1);
      lcd.setCursor(5,0);
      lcd.write(32);
      lcd.write(32);
      lcd.write(1);
      lcd.setCursor(5,1);
      lcd.write(32);
      lcd.write(32);
      lcd.write(1);
      rpmleds = map(rpm, 0, rpmmax, 0, 20);
      if (rpmleds >= 19) {
        delay(100);
        lcd.clear();
      }


    }
    if (gear==3){
      sprintf(message,"%s:%d",rapport, gear-1);
      lcd.setCursor(6,0);
      lcd.write(5);
      lcd.write(3);
      lcd.write(1);
      lcd.setCursor(6, 1);
      lcd.write(2);
      lcd.write(6);
      lcd.write(6);
      rpmleds = map(rpm, 0, rpmmax, 0, 20);
      if (rpmleds >= 19) {
        delay(100);
        lcd.clear();
      }
    }


    if (gear==4){
      sprintf(message,"%s:%d",rapport, gear-1);
      lcd.setCursor(6,0);
      lcd.write(5);
      lcd.write(3);
      lcd.write(1);
      lcd.setCursor(6, 1);
      lcd.write(7);
      lcd.write(6);
      lcd.write(1);
      rpmleds = map(rpm, 0, rpmmax, 0, 20);
      if (rpmleds >= 19) {
        delay(100);
        lcd.clear();
      }
    }


    if (gear==5){
      sprintf(message,"%s:%d",rapport, gear-1);
      lcd.setCursor(6,0);
      lcd.write(2);
      lcd.write(6);
      lcd.write(1);
      lcd.setCursor(6, 1);
      lcd.write(32);
      lcd.write(32);
      lcd.write(1);
      rpmleds = map(rpm, 0, rpmmax, 0, 20);
      if (rpmleds >= 19) {
        delay(100);
        lcd.clear();
      }


    }
    if (gear==6){
      sprintf(message,"%s:%d",rapport, gear-1);
      lcd.setCursor(6,0);
      lcd.write(2);
      lcd.write(3);
      lcd.write(4);
      lcd.setCursor(6, 1);
      lcd.write(7);
      lcd.write(6);
      lcd.write(1);
      rpmleds = map(rpm, 0, rpmmax, 0, 20);
      if (rpmleds >= 19) {
        delay(100);
        lcd.clear();
      }


    }
    if (gear==7){
      sprintf(message,"%s:%d",rapport, gear-1);
      lcd.setCursor(6,0);
      lcd.write(2);
      lcd.write(3);
      lcd.write(4);
      lcd.setCursor(6, 1);
      lcd.write(2);
      lcd.write(6);
      lcd.write(1);
      rpmleds = map(rpm, 0, rpmmax, 0, 20);
      if (rpmleds >= 19) {
        delay(100);
        lcd.clear();
      }


    }
    if (gear==8){
      sprintf(message,"%s:%d",rapport, gear-1);
      lcd.setCursor(6,0);
      lcd.write(2);
      lcd.write(8);
      lcd.write(1);
      lcd.setCursor(6, 1);
      lcd.write(32);
      lcd.write(32);
      lcd.write(1);
      rpmleds = map(rpm, 0, rpmmax, 0, 20);
      if (rpmleds >= 19) {
        delay(100);
        lcd.clear();
      }


    }     
    if (gear==9){
      sprintf(message,"%s:%d",rapport, gear-1);
      lcd.setCursor(6, 0);
      lcd.write(2); 
      lcd.write(3);
      lcd.write(1);
      lcd.setCursor(6, 1);
      lcd.write(2); 
      lcd.write(6); 
      lcd.write(1);
      rpmleds = map(rpm, 0, rpmmax, 0, 20);
      if (rpmleds >= 19) {
        delay(100);
        lcd.clear();
      }


    }     
    if (gear==10){
      sprintf(message,"%s:%d",rapport, gear-1);
      lcd.setCursor(6, 0);
      lcd.write(2); 
      lcd.write(3);
      lcd.write(1);
      lcd.setCursor(6, 1);
      lcd.write(7); 
      lcd.write(6); 
      lcd.write(1);
      rpmleds = map(rpm, 0, rpmmax, 0, 20);
      if (rpmleds >= 19) {
        delay(100);
        lcd.clear();
      }


    }     
    if (gear==11){
      sprintf(message,"%s:%d",rapport, gear-1);
      lcd.setCursor(4,0);
      lcd.write(32);
      lcd.write(32);
      lcd.write(1);
      lcd.setCursor(4,1);
      lcd.write(32);
      lcd.write(32);
      lcd.write(1);
      lcd.setCursor(7,0);
      lcd.write(2); 
      lcd.write(8);
      lcd.write(1);
      lcd.setCursor(7,1);
      lcd.write(2); 
      lcd.write(6); 
      lcd.write(1);
      rpmleds = map(rpm, 0, rpmmax, 0, 20);
      if (rpmleds >= 19) {
        delay(100);
        lcd.clear();
      }


    }     
    if (gear==12){
      sprintf(message,"%s:%d",rapport, gear-1);
      lcd.setCursor(4,0);
      lcd.write(32);
      lcd.write(32);
      lcd.write(1);
      lcd.setCursor(4,1);
      lcd.write(32);
      lcd.write(32);
      lcd.write(1);
      lcd.setCursor(7,0);
      lcd.write(32);
      lcd.write(32);
      lcd.write(1);
      lcd.setCursor(7,1);
      lcd.write(32);
      lcd.write(32);
      lcd.write(1);
      rpmleds = map(rpm, 0, rpmmax, 0, 20);
      if (rpmleds >= 19) {
        delay(100);
        lcd.clear();
      }


    }     
    if (gear==13){
      sprintf(message,"%s:%d",rapport, gear-1);
      lcd.setCursor(4,0);
      lcd.write(32);
      lcd.write(32);
      lcd.write(1);
      lcd.setCursor(4,1);
      lcd.write(32);
      lcd.write(32);
      lcd.write(1);
      lcd.setCursor(7,0);
      lcd.write(5);
      lcd.write(3);
      lcd.write(1);
      lcd.setCursor(7,1);
      lcd.write(2);
      lcd.write(6);
      lcd.write(6);
      rpmleds = map(rpm, 0, rpmmax, 0, 20);
      if (rpmleds >= 19) {
        delay(100);
        lcd.clear();
      }

    }
    sprintf(message,"%s",trmin);
    lcd.setCursor(11,0);
    lcd.write(message);
    sprintf(message,"%5d",rpm);
    lcd.setCursor(11,1);
    lcd.write(message);
    sprintf(message,"%s",speed1);
    lcd.setCursor(0,0);
    lcd.write(message);

    if (speed > 0){
      c = speed*3.595;
      sprintf(message,"%3d",c);
      lcd.setCursor(1,1);
      lcd.write(message);
    }
    delay(1);



    rpmlast = rpm;


  }
  extractordetected = false;
  digitalWrite(13, HIGH); //No connection, turn off the LED
  lcd.setCursor(0,0);
  lcd.write("[<-OPEN X-SIM->]");
  lcd.setCursor(0,1);
  lcd.write("  EURO TRUCK 2");
  delay(2000); //No serial connection, poll serial port all two seconds + readtimeout until a running extractor is detected
  lcd.clear();
}











I left some variables couse im testing here.

Cheers.

[ ]´s
bahzitchiu
 
Posts: 9
Joined: Wed 18. Jun 2014, 05:33
Has thanked: 0 time
Been thanked: 8 times

Re: New!!OBD2 Com with Xsim3 and Arduino-New!!

Postby kbssa » Tue 1. Jul 2014, 04:40

E ai cara blz ?

Não sabia que você era Brasuca...mas valeu pelo código, agora meu painel vai ficar muito louco !! kkkkkkkkkk

English

What's up man ??

I didn't know that you are Brasilian too...thank you for the code, now my dash will be craxy !!

Thank you again !!!
kbssa
 
Posts: 120
Images: 16
Joined: Thu 19. Apr 2012, 21:03
Has thanked: 5 times
Been thanked: 3 times

Re: New!!OBD2 Com with Xsim3 and Arduino-New!!

Postby bahzitchiu » Tue 1. Jul 2014, 07:18

kbssa wrote:E ai cara blz ?

Não sabia que você era Brasuca...mas valeu pelo código, agora meu painel vai ficar muito louco !! kkkkkkkkkk

English

What's up man ??

I didn't know that you are Brasilian too...thank you for the code, now my dash will be craxy !!

Thank you again !!!


Send a video to us.

Cheers.

[ ]´s
bahzitchiu
 
Posts: 9
Joined: Wed 18. Jun 2014, 05:33
Has thanked: 0 time
Been thanked: 8 times

Re: New!!OBD2 Com with Xsim3 and Arduino-New!!

Postby kbssa » Wed 2. Jul 2014, 04:23

bahzitchiu wrote:
kbssa wrote:E ai cara blz ?

Não sabia que você era Brasuca...mas valeu pelo código, agora meu painel vai ficar muito louco !! kkkkkkkkkk

English

What's up man ??

I didn't know that you are Brasilian too...thank you for the code, now my dash will be craxy !!

Thank you again !!!


Send a video to us.

Cheers.

[ ]´s


As soon as my panel is done I post a video here.
kbssa
 
Posts: 120
Images: 16
Joined: Thu 19. Apr 2012, 21:03
Has thanked: 5 times
Been thanked: 3 times

Re: New!!OBD2 Com with Xsim3 and Arduino-New!!

Postby pmancio » Fri 28. Nov 2014, 15:32

bahzitchiu wrote:New code.

PS: the gear is blinking when rpm=95% max, but not appearing on video, sorry.

Cheers

[ ]´s

Very good! It's perfectly working. I'm a noob and want to ask: what about if I wish to add an extra seven digit display to use for gear indicator?
pmancio
 
Posts: 1
Joined: Thu 27. Nov 2014, 00:46
Has thanked: 0 time
Been thanked: 0 time

Re: New!!OBD2 Com with Xsim3 and Arduino-New!!

Postby antmerino » Wed 24. Dec 2014, 01:46

I have a problem,
with best lap time is less than 1 min, the value return by Obd2 is not correct.
Always when the best time is more than 1 min all is well.

I think that is a problem with the value return by OBd2 in extractor.

any advice?

Other thing:
I like to get the last lap time, Have you any method to get this value?

thanks
antmerino
 
Posts: 2
Joined: Fri 19. Dec 2014, 22:16
Has thanked: 0 time
Been thanked: 0 time

Re: New!!OBD2 Com with Xsim3 and Arduino-New!!

Postby xwitazx » Fri 6. Mar 2015, 18:43

Hi, i'm new in this, so sorry for stupid questions.. :| batchiuhzi do you use schematics as vicpopo shows in first post of this topic? and could you share x-sim settings? It would be gratful :oops:
dscsdc
xwitazx
 
Posts: 1
Joined: Sat 28. Feb 2015, 15:08
Location: Lithuania
Has thanked: 0 time
Been thanked: 0 time

Re: New!!OBD2 Com with Xsim3 and Arduino-New!!

Postby rdriver » Thu 16. Apr 2015, 18:52

hi. i have a problem with my 16*2 lcd dashboard. i upload your code in my arduino uno board and connect it to lcd but when i open x-sim Extractor and run my game(LFS) the lcd just show (test obd2 com !).i installed lcdstudio and configured lcd studio with x-sim converter.
pls say me whats the problem.
sorry for my bad english.
thanks
rdriver
 
Posts: 6
Joined: Sun 12. Apr 2015, 22:22
Has thanked: 0 time
Been thanked: 0 time

PreviousNext

Return to Peripheral Projects

Who is online

Users browsing this forum: No registered users and 1 guest

cron