Hey guys,
I have the sparkfun obdii uart board (https://www.sparkfun.com/products/9555) that i'm using to get data for a Digital instrument cluster that I'm designing in processing. It's working just fine, but it's extremely slow. Could you guys take a look at my code and tell me if it's a problem with my code or if the sparkfun board is just really that slow?
//Set up ring buffer
char rxData[20];
char rxIndex = 0;
void setup()
{
Serial.begin(9600); // prints to serial monitor
Serial1.begin(9600); //Hardware serial connection to the obdii uart
OBD_init(); //initiates obd link
}
void loop()
{
//Delete any data that may be left over in the serial port.
Serial1.flush();
int mph = (int)((getSPEED() * 10000L + 5) / 16090); //convert kmh to mph
Serial.print (mph);
Serial.print (",");
Serial.print (getRPM());
Serial.print (",");
Serial.print (getWATERTEMP());
Serial.print (",");
Serial.print (getOILTEMP());
Serial.print (",");
Serial.print (getFUEL());
Serial.print (",");
Serial.println (getVOLT());
// delay(10);//wait .5 seconds and grab another reading
}
void OBD_init(void)
{
//Wait for a little while before sending the reset command to the OBD-II-UART
delay(2000);
//Reset the OBD-II-UART
Serial1.print("ATZ\r");
//Wait for a bit before starting to send commands after the reset.
delay(2);
OBD_read();
Serial1.print("ATE0\r");
OBD_read();
Serial1.flush();
}
int getRPM(void)
{
//Query the OBD-II-UART for the Vehicle rpm
Serial1.flush();
Serial1.print("010C\r");
OBD_read();
return ((strtol(&rxData[6], 0, 16) * 256) + strtol(&rxData[9], 0, 16)) / 4;
}
int getSPEED(void)
{
//Query the OBD-II-UART for the vehicle speed
Serial1.flush();
Serial1.print("010D\r");
OBD_read();
return strtol(&rxData[6], 0, 16);
}
int getOILTEMP(void)
{
//Query the OBD-II-UART for the vehicle speed
Serial1.flush();
Serial1.print("015C\r");
OBD_read();
return strtol(&rxData[6], 0, 16);
}
int getFUEL(void)
{
//Query the OBD-II-UART for the vehicle speed
Serial1.flush();
Serial1.print("012F\r");
OBD_read();
return strtol(&rxData[6], 0, 16);
}
int getVOLT(void)
{
//Query the OBD-II-UART for the vehicle speed
Serial1.flush();
Serial1.print("0142\r");
OBD_read();
return strtol(&rxData[6], 0, 16);
}
int getWATERTEMP(void)
{
//Query the OBD-II-UART for the Engine Coolant Temp
Serial1.flush();
Serial1.print("0105\r");
OBD_read();
return strtol(&rxData[6], 0, 16) - 40;
}
void OBD_read(void)
{
char c;
do {
if (Serial1.available() > 0)
{
c = Serial1.read();
if ((c != '>') && (c != '\r') && (c != '\n')) //Keep these out of our buffer
{
rxData[rxIndex++] = c; //Add whatever we receive to the buffer
}
}
} while (c != '>'); //The ELM327 ends its response with this char so when we get it we exit out.
rxData[rxIndex++] = '\0';//Converts the array into a string
rxIndex = 0; //Set this to 0 so next time we call the read we get a "clean buffer"
}
any help would be greatly appreciated
thanks