Thanks again for your help so far. I finally got some free time to work on the project again (pregnant wife and getting ready for our firstborn is taking up my project time)
anyways here are the results from my tests this morning. I guess i was mistaken about the Serial.flush I commented out all the Serial.flush lines. no real speed up in response but it didn't break the script so they are gone now.
I tried a bunch of different baud rates on Serial1 (kept Serial0 at 9600) and didn't get a response from the obd-ii uart board in my serial monitor. I tried 115200, 38400, 41600, 10400, 500000, and 250000. no luck.
next i tried to play with the timeouts and it helped TREMENDOUSLY with speed but the data i retrieve isn't consistent. I will read back the correct rpm for a few cycles then it'll jump up to 21071 or down to ~53 or 0. The only way i could solve this is to add a delay:
delay(75);
but it slows it down (still faster then w/o the timeouts)
Here is a [demo](
http://youtu.be/GrkIU5mSe-4) of the processing app in action if you want to see how well it's working. Optimally I'd like to get those times down even more but I think it's satisfactory for now (unless you have any other suggestions)
issue no 2: When I add the second value to poll (speed), it goes a bit out of whack with random numbers. If i don't do the internal timeout method it works fine, just slow. If i do the internal timeout method but alternate the readings (cycle 1: read rpm, cycle 2: read speed, cycle 3: read rpm... and so on) it works fine. again just slower
here is the initial sketch that's working with just one reading:
//Set up ring buffer
char rxData[20];
char rxIndex = 0;
int rpmstored = 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()
rpmstored = getRPM();
//int mph = (int)((getSPEED() * 10000L + 5) / 16090); //convert to mph
// Serial.print (mph);
Serial.print (0);
Serial.print (",");
Serial.print (rpmstored);
Serial.print (",");
// Serial.print (getWATERTEMP());
Serial.print (0);
Serial.print (",");
// Serial.print (getOILTEMP());
Serial.print (0);
Serial.print (",");
// Serial.print (getFUEL());
// Serial.print (",");
Serial.println (0);
// Serial.println (getVOLT());
delay(75);
}
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.print("010C1\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.print("010D1\r");
OBD_read();
return strtol(&rxData[6], 0, 16);
}
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"
}