Hello all,
I'm working on a project using the SKPang/Sparkfun CAN-bus shield (http://www.skpang.co.uk/catalog/arduino-canbus-shield-with-usd-card-holder-p-706.html) on an Arduino Uno R3, working in Arduino IDE 1.03 on Windows 8 x64. I'm using this (http://skpang.googlecode.com/files/Canbus_v4.zip) group of files as a basis for my work. My project file is located here for you to download and inspect: Canbus v5 - Pastebin.com
I'm having a rather peculiar problem with the program. The goal of the program is to read data from an OBD-II port on a car (the one under your dash, if you're from the US/Canada), in particular, the data that can be used to calculate instant gas mileage. The data is then taken into a function through a call and printed out onto an serial LCD screen that is attached to the CAN-bus shield. Later in the development cycle, I plan to write it to a uSD card as well, but that's beyond the scope of this post.
In order to get the gas mileage, I use the following function (which is an excerpt from the pastebin you saw above):
float mpgCalculation()
{
//MPG = (14.7 * 6.17 * 454 * Vehicle Speed Sensor * 0.621371) / (3600 * MAF/ 100)
//MPG = 710.7 * VSS/MAF
Serial.println("mpgCalculation");
float maf = Canbus.ecu_req(MAF_SENSOR, buffer);
float vss = Canbus.ecu_req(VEHICLE_SPEED, buffer);
Serial.println(maf);
Serial.println(vss);
//execute calculation #1
float instantMpg = (14.7 * 6.17 * 454 * vss * 0.621371) / (3600 * maf/100);
return instantMpg;
}
float maf = Canbus.ecu_req(MAF_SENSOR, buffer);
float vss = Canbus.ecu_req(VEHICLE_SPEED, buffer);
These two lines access the buffer and make calls to the C++ library file that interprets the car's data output.
The place where I call this function is in the main loop:
void loop() {
Serial.println("Engine RPM.");
if(Canbus.ecu_req(ENGINE_RPM,buffer) == 1) // Request for engine RPM
{
sLCD.write(COMMAND); //Move LCD cursor to line 0
sLCD.write(LINE0);
sLCD.print(buffer); //Display data on LCD
}
digitalWrite(LED3, HIGH);
Serial.println("Vehicle Speed.");
if(Canbus.ecu_req(VEHICLE_SPEED,buffer) == 1)
{
sLCD.write(COMMAND);
sLCD.write(LINE0 + 9);
sLCD.print(buffer);
}
/*
if(Canbus.ecu_req(ENGINE_COOLANT_TEMP,buffer) == 1)
{
sLCD.write(COMMAND);
sLCD.write(LINE1); // Move LCD cursor to line 1
sLCD.print(buffer);
}
*/
Serial.println("MPG");
if(Canbus.ecu_req(MAF_SENSOR, buffer) == 1 && Canbus.ecu_req(VEHICLE_SPEED, buffer) == 1)
{
sLCD.write(COMMAND);
sLCD.write(LINE1);
float instantMpg = mpgCalculation();
sLCD.print(instantMpg);
}
Serial.println("Throttle");
if(Canbus.ecu_req(THROTTLE,buffer) == 1)
{
sLCD.write(COMMAND);
sLCD.write(LINE1 + 9);
sLCD.print(buffer);
file.print(buffer);
}
// Canbus.ecu_req(O2_VOLTAGE,buffer);
digitalWrite(LED3, LOW);
delay(100);
}
This produces the following output when executed, pretty much constantly. These statements are the result of the Serial.println() statements I put in the code.
MPG
Throttle
Engine RPM.
Vehicle Speed.
MPG
Throttle
Engine RPM.
Vehicle Speed.
MPG
Throttle
ne RPM.
Vehicle Speed.
MPG
Throttle
Engine RPM.
Vehicle Speed.
MPG
Throttle
Engine RPM.
Vehicle Speed.
MPG
Throttle
I also placed some of these statements in the function I'm using, with Serial.println("mpgCalculation");
being an example. As you can plainly see, the messages are not printed out to the Serial console on the computer. I have verified that it is the correct serial port.
To me, it appears as if the call to the function simply isn't being placed. I've gotten a little rusty on my C/C++... but not THAT rusty. What do you guys make of this?