Super slow (20 seconds) data from UART OBD II connector.
I'm making a speedometer with some nixie tubes that I have connected with serial. I'm using the supplied library for the car connector and I'm using the mega 2560 so I can have multiple hardware serial connections. I originally did this with software serial and switched to see if that was the problem. I am receiving updates for speed maybe every 20 seconds. Here is my code:
[code]
#import <Arduino.h>
#include <OBD.h>
int ones = 0;
int tens = 0;
int kphValue = 0;
int mphValue = 0;
int rpmValue = 0;
COBD obd;
void setup(){
Serial2.begin(9600);
obd.begin();
//initiate OBD-II connection until success
while (!obd.init()){
Serial2.print("$0,Y,Y,255,255,000,000$0,Y,Y,255,255,000,000!");
delay(1000);
Serial2.print("$0,Y,Y,000,000,000,000$0,Y,Y,000,000,000,000!");
delay(1000);
}
}
void loop(){
obd.read(PID_RPM, rpmValue);
delay(100);
obd.read(PID_SPEED, kphValue);
delay(100);
kphToMPH();
String onesString = String(ones);
String tensString = String(tens);
String payloadString;
if(rpmValue<3000){
payloadString = "$" + onesString + ",N,N,255,000,255,000$" + tensString + ",N,N,255,000,255,000!";
Serial2.print(payloadString);
}
else if(rpmValue>=5000){
payloadString = "$" + onesString + ",N,N,255,255,000,000$" + tensString + ",N,N,255,255,000,000!";
Serial2.print(payloadString);
}
else if(rpmValue>=3000){
payloadString = "$" + onesString + ",N,N,255,255,030,000$" + tensString + ",N,N,255,255,030,000!";
Serial2.print(payloadString);
}
}
void splitDigits(int input_number)
{
ones = (input_number%10);
tens = ((input_number/10)%10);
}
void kphToMPH()
{
mphValue = kphValue * 0.621371;
splitDigits(mphValue);
}
[/code]
[code]
#import <Arduino.h>
#include <OBD.h>
int ones = 0;
int tens = 0;
int kphValue = 0;
int mphValue = 0;
int rpmValue = 0;
COBD obd;
void setup(){
Serial2.begin(9600);
obd.begin();
//initiate OBD-II connection until success
while (!obd.init()){
Serial2.print("$0,Y,Y,255,255,000,000$0,Y,Y,255,255,000,000!");
delay(1000);
Serial2.print("$0,Y,Y,000,000,000,000$0,Y,Y,000,000,000,000!");
delay(1000);
}
}
void loop(){
obd.read(PID_RPM, rpmValue);
delay(100);
obd.read(PID_SPEED, kphValue);
delay(100);
kphToMPH();
String onesString = String(ones);
String tensString = String(tens);
String payloadString;
if(rpmValue<3000){
payloadString = "$" + onesString + ",N,N,255,000,255,000$" + tensString + ",N,N,255,000,255,000!";
Serial2.print(payloadString);
}
else if(rpmValue>=5000){
payloadString = "$" + onesString + ",N,N,255,255,000,000$" + tensString + ",N,N,255,255,000,000!";
Serial2.print(payloadString);
}
else if(rpmValue>=3000){
payloadString = "$" + onesString + ",N,N,255,255,030,000$" + tensString + ",N,N,255,255,030,000!";
Serial2.print(payloadString);
}
}
void splitDigits(int input_number)
{
ones = (input_number%10);
tens = ((input_number/10)%10);
}
void kphToMPH()
{
mphValue = kphValue * 0.621371;
splitDigits(mphValue);
}
[/code]
2014-04-22 11:26:39 Would this be better?
[code]
#import <Arduino.h>
#include <OBD.h>
int ones = 0;
int tens = 0;
int kphValue = 0;
int mphValue = 0;
int rpmValue = 0;
String onesString;
String tensString;
String payloadString;
String rpmString;
COBD obd;
void setup(){
Serial2.begin(9600);
obd.begin();
//initiate OBD-II connection until success
while (!obd.init()){
Serial2.print("$0,Y,Y,255,255,000,000$0,Y,Y,255,255,000,000!");
delay(1000);
Serial2.print("$0,Y,Y,000,000,000,000$0,Y,Y,000,000,000,000!");
delay(1000);
}
}
void loop(){
onesString = String(ones);
tensString = String(tens);
payloadString;
rpmString;
if(obd.read(PID_RPM, rpmValue)){
if(rpmValue<3000){
rpmString =",N,N,255,000,255,000";
}
else if(rpmValue>=5000){
rpmString = ",N,N,255,255,000,000";
}
else if(rpmValue>=3000){
rpmString = ",N,N,255,255,030,000";
}
}
if(obd.read(PID_SPEED, kphValue)){
kphToMPH();
payloadString = "$" + onesString + rpmString + "$" + tensString + "rpmString" + "!";
if(Serial2.available()){
Serial2.print(payloadString);
}
}
}
void splitDigits(int input_number)
{
ones = (input_number%10);
tens = ((input_number/10)%10);
}
void kphToMPH()
{
mphValue = kphValue * 0.621371;
splitDigits(mphValue);
}
[/code] evan.stoddard
[code]
#import <Arduino.h>
#include <OBD.h>
int ones = 0;
int tens = 0;
int kphValue = 0;
int mphValue = 0;
int rpmValue = 0;
String onesString;
String tensString;
String payloadString;
String rpmString;
COBD obd;
void setup(){
Serial2.begin(9600);
obd.begin();
//initiate OBD-II connection until success
while (!obd.init()){
Serial2.print("$0,Y,Y,255,255,000,000$0,Y,Y,255,255,000,000!");
delay(1000);
Serial2.print("$0,Y,Y,000,000,000,000$0,Y,Y,000,000,000,000!");
delay(1000);
}
}
void loop(){
onesString = String(ones);
tensString = String(tens);
payloadString;
rpmString;
if(obd.read(PID_RPM, rpmValue)){
if(rpmValue<3000){
rpmString =",N,N,255,000,255,000";
}
else if(rpmValue>=5000){
rpmString = ",N,N,255,255,000,000";
}
else if(rpmValue>=3000){
rpmString = ",N,N,255,255,030,000";
}
}
if(obd.read(PID_SPEED, kphValue)){
kphToMPH();
payloadString = "$" + onesString + rpmString + "$" + tensString + "rpmString" + "!";
if(Serial2.available()){
Serial2.print(payloadString);
}
}
}
void splitDigits(int input_number)
{
ones = (input_number%10);
tens = ((input_number/10)%10);
}
void kphToMPH()
{
mphValue = kphValue * 0.621371;
splitDigits(mphValue);
}
[/code] evan.stoddard