Problem with reading sensor

Hi,
Finally I've made my OBD2 logger. It's recording data, but I don't know why only first value is acctual.

PID_RPM: 1479;PID_SPEED: 0;PID_THROTTLE: 18;PID_ENGINE_LOAD: 41;PID_ABS_ENGINE_LOAD: 0;PID_FUEL_LEVEL: 0;PID_DISTANCE: 0
PID_RPM: 1476;PID_SPEED: 0;PID_THROTTLE: 18;PID_ENGINE_LOAD: 41;PID_ABS_ENGINE_LOAD: 0;PID_FUEL_LEVEL: 0;PID_DISTANCE: 0

Rest of pid's value seems to be frozen at moment when I start logging.

Can you help me ivestigate this, why it doesn't record all pid's?
Here is my code:

#include <SdFat.h>
#include <arduino.h>
#include <OBD.h>

const uint8_t chipSelect = SS;
unsigned long lastTick = millis();

SdFat sd;
COBD obd;

  int rpm;
  int speed_;
  int throttle;
  int eng_load;
  int abs_eng_load;
  int fuel;
  int dist;

void setup() {
  Serial.begin(OBD_SERIAL_BAUDRATE);
  sd.begin(chipSelect, SPI_FULL_SPEED);
  obd.Init();
}
//------------------------------------------------------------------------------
void loop() {
  
  unsigned long curTick = millis();
  if (curTick - lastTick > 500) 
        {
  	  ofstream sdlog("LOGFILE.TXT", ios::out | ios::app);
          obd.ReadSensor(PID_RPM, rpm);
          obd.ReadSensor(PID_SPEED, predk);
          obd.ReadSensor(PID_THROTTLE, przepustnica);
          obd.ReadSensor(PID_ENGINE_LOAD, eng_load);
          obd.ReadSensor(PID_ABS_ENGINE_LOAD, abs_eng_load);
          obd.ReadSensor(PID_FUEL_LEVEL, fuel); 
          obd.ReadSensor(PID_DISTANCE, dist);
         
          sdlog << "PID_RPM: " << rpm << ";PID_SPEED: " << predk << ";PID_THROTTLE: " << przepustnica << ";PID_ENGINE_LOAD: " << eng_load << ";PID_ABS_ENGINE_LOAD: " << abs_eng_load << ";PID_FUEL_LEVEL: " << fuel << ";PID_DISTANCE: " << dist << endl;
         // if (!sdlog) sd.errorHalt("append failed");
	  lastTick = curTick;
	}
}

I would first try to read the data and display them on an LCD or so. Much easier to debug.

DO you have a link to the ODB library?

does this code actually run? as predk and przepustnica are not defined?

Hi,
I've got OBD2 library from Freematics OBD-II Adapter for Arduino | ArduinoDev.com
It's display data correctly, I've try it earlier, and it works out of the box. Yes I know that this two variables are not defined, but it doesn't change anything.

I found a diff with the rpm_led demo - it's probably the initialization

const uint8_t chipSelect = SS;
unsigned long lastTick = millis();

SdFat sd;
COBD obd;

  int rpm;
  int speed_;
  int throttle;
  int eng_load;
  int abs_eng_load;
  int fuel;
  int dist;

void setup() {
  Serial.begin(OBD_SERIAL_BAUDRATE);
  sd.begin(chipSelect, SPI_FULL_SPEED);
  while (!obd.Init());   // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< change this line this is from the example program
}
//------------------------------------------------------------------------------
void loop() {
  
  unsigned long curTick = millis();
  if (curTick - lastTick > 500) 
        {
  	  ofstream sdlog("LOGFILE.TXT", ios::out | ios::app);
          obd.ReadSensor(PID_RPM, rpm);
          obd.ReadSensor(PID_SPEED, predk);
          obd.ReadSensor(PID_THROTTLE, przepustnica);
          obd.ReadSensor(PID_ENGINE_LOAD, eng_load);
          obd.ReadSensor(PID_ABS_ENGINE_LOAD, abs_eng_load);
          obd.ReadSensor(PID_FUEL_LEVEL, fuel); 
          obd.ReadSensor(PID_DISTANCE, dist);
         
          sdlog << "PID_RPM: " << rpm << ";PID_SPEED: " << predk << ";PID_THROTTLE: " << przepustnica << ";PID_ENGINE_LOAD: " << eng_load << ";PID_ABS_ENGINE_LOAD: " << abs_eng_load << ";PID_FUEL_LEVEL: " << fuel << ";PID_DISTANCE: " << dist << endl;
         // if (!sdlog) sd.errorHalt("append failed");
	  lastTick = curTick;
	}
}

Truthly I doubt that this change is necessary. As I understand this line it's try initiate obd until it finish success, and this is executed only once on start. So when I received this one value, this means obd was initialized successfully.

marcin_i:
Yes I know that this two variables are not defined, but it doesn't change anything.

Well, it does change something because the fact that the sketch doesn't compile means it can't possibly demonstrate the problem. You need to post your actual sketch.

#include <SdFat.h>
#include <arduino.h>
#include <OBD.h>

const uint8_t chipSelect = SS;
unsigned long lastTick = millis();

SdFat sd;
COBD obd;

  int rpm;
  int speed_;
  int throttle;
  int fuel;
  int dist;
  
void setup() {
  Serial.begin(OBD_SERIAL_BAUDRATE);
  sd.begin(chipSelect, SPI_FULL_SPEED);
  obd.Init();

}
//------------------------------------------------------------------------------
void loop() {
  
  unsigned long curTick = millis();
  if (curTick - lastTick > 500) 
        {
  	  ofstream sdlog("LOGFILE.TXT", ios::out | ios::app);

          obd.ReadSensor(PID_RPM, rpm);
          obd.ReadSensor(PID_SPEED, speed_);
          obd.ReadSensor(PID_THROTTLE, throttle);
          obd.ReadSensor(PID_FUEL_LEVEL, fuel); 
          obd.ReadSensor(PID_DISTANCE, dist);
         
          sdlog << "PID_RPM: " << rpm << ";PID_SPEED: " << speed_ << ";PID_THROTTLE: " << throttle << ";PID_FUEL_LEVEL: " << fuel << ";PID_DISTANCE: " << dist << endl;
         // if (!sdlog) sd.errorHalt("append failed");
	  lastTick = curTick;
	}

}

This my actual sketch and compilation ends without any errors. The difference comparing to previous version is that I removed two pids.

Which Arduino are you running this code on? For a 328-based Arduino, the SD library takes up more than 1/4 of the memory, because it actually writes to the card in 512 byte chunks.

The streaming library also use a fair amount of memory holding the data to be streamed.

My suspicion is that you are running out of memory.

it's arduino uno r3. How can I investigate do I suffer for lack of memory?

Google the site for FreeMemory(). Start getting rid of libraries you don't really need. Start with the Streaming one. The SD card doesn't care whether the data gets there because of one function call or 12 function calls.

Ok I found some function which counts how many free memory I have. And the result was 638 bytes of free memory. I'm still a beginner in arduino and it hard to say for me is it big or small.

Which library is those streaming?

Which library is those streaming?

This is supported because of the Streaming library. You do not explicitly include the Streaming library, so either SdFat or OBD is, although that should not be possible.

          sdlog << "PID_RPM: " << rpm << ";PID_SPEED: " << speed_ << ";PID_THROTTLE: " << throttle << ";PID_FUEL_LEVEL: " << fuel << ";PID_DISTANCE: " << dist << endl;

638 bytes should be plenty, but, of course, that depends on where you measured that value.

I'd try Serial.print()ing the values, to make sure that they are indeed correct. No sense chasing a bug in the wrong place.

Hi,
I think I found root of this problem. OBD2 library which I use somehow doesn't allow me to read more than one PID. When I want to read two or more it reads only the first one. Now the tricky part. Does anyone of you have any idea how to solve this issue.

The library intro at link you provided implies that the library can read multiple sensors, although their example there doesn't show it. Look at the dashboard_1602 example though and you see that multiple PIDs are being read, corresponding to which mode the device is in.

I suggest you try writing a minimalist program that reads two pids and displays the result, on the serial port for preference, and build up from there.

edit: grammar