ArduinoJson in combination with SD library not working

Hello

I am trying to read a n-th json file in a directory "programs/" on a SD card using standard arduino SD library and ArduinoJson. I manage to get it work (partially - Option 2) in a very strange way: I added a call of "dummyFun" which basically just opens one json file and close it, but without calling this function the ArduinoJson fails to read the file. This suggest that this problem is related with my inexperience/understanding of SD library.
Although if I comment out Option 2 and uncomment Option 1 (Option 1 is what I want) it fails to read it either way.

I am using an Arduino Uno and RepRapDiscount unit for SD reading (see wiring in the attachment). An 2Gb SandDisk micro SD with an adapter (and yes I formatted it with SD Card Formatter).
On an Sd card dhere is only "programs/" directory with three identical json files: "foo0.set", "foo1.set" and "foo2.set".

I am complete newbie with SD library and ArduinoJson, so any help would be appreciated.

My sketch:

// https://arduinojson.org/v6/example/config/

#include <ArduinoJson.h>
#include <SD.h>
#include <SPI.h>

const uint8_t maxArrLen = 10;
struct Config {
  uint8_t pLength;
  bool dir;
  float K;
  float lengths[maxArrLen];
  float startK[maxArrLen];
  float endK[maxArrLen];
};
uint8_t prog_selection = 2;
const int chipSelect = 4;
//uint8_t option = 0;


Config config;                         // <- global configuration object
File settingsFile;
File settingsDir;

// Loads the configuration from a file
void loadSettings(File setFile, Config &config) {
  
  // Allocate a temporary JsonDocument
  // Don't forget to change the capacity to match your requirements.
  // Use arduinojson.org/v6/assistant to compute the capacity.
  StaticJsonDocument<610> doc;
  // Deserialize the JSON document
  DeserializationError error = deserializeJson(doc, setFile);//file);
  
  if (error){
    Serial.println(F("loadJson: Failed to read file, using default configuration"));
  }
  else{
    Serial.println(F("loadJson: Succeeded to read file"));
  }
  // Copy values from the JsonDocument to the Config
  config.pLength = doc["pLength"] | 0;
  config.dir = doc["dir"]|0;
  config.K = doc["K"] | 1.0;
  for(int i=0; i<maxArrLen; i++){
    config.lengths[i] = doc["lengths"][i]|0.00;
    config.startK[i] = doc["startK"][i]|0.00;
    config.endK[i] = doc["endK"][i]|0.00;
  }
}
void dummyFun() {
  settingsFile = SD.open("/programs/foo1.set");
  settingsFile.close();
}

void loadProgram(Config &config){
//  settingsDir = SD.open("programs/");           //Option 1
//  for(int i=0; i<prog_selection; i++){          //Option 1
//    settingsFile = settingsDir.openNextFile();  //Option 1
//    if(i<prog_selection-1){                     //Option 1
//      settingsFile.close();                     //Option 1
//    }                                           //Option 1
//  }                                             //Option 1
//  settingsDir.close();                          //Option 1

  settingsFile = SD.open("/programs/foo1.set");   //Option 2
  
  loadSettings(settingsFile, config);
  settingsFile.close();
  dummyFun();
  
}

void setup() {
  Serial.begin(9600);
  Serial.println( F("Loading configuration..."));
  if(!SD.begin(4)){
    Serial.println("NOK");
  }
  loadProgram(config);
  Serial.println(config.pLength);
  Serial.println(config.dir);
  Serial.println(config.K);
  for(int i=0; i<10; i++){
    Serial.print(config.lengths[i]);
    Serial.print("\t");
    Serial.print(config.startK[i]);
    Serial.print("\t");
    Serial.println(config.endK[i]);
  } 
}



void loop() {
  // not used in this example
}

content of "programs/foo0.set" (and "foo1.set" and "foo2.set") file:

{
	"pLength": 5,
	"dir": 1,
	"K": 1.45,
	"lengths":[1.5,4.6,3.8,7.6,4.3,0.0,0.0,0.0,0.0,0.0],
	"startK":[0.4,0.5,0.8,1.2,0.3,0.0,0.0,0.0,0.0,0.0],
	"endK":[0.4,0.5,0.4,1,0.0,0.3,0.0,0.0,0.0,0.0]
}

Output with dummyFun (and Option 2):

Loading configuration...
loadJson: Succeeded to read file
5
1
1.45
1.50	0.40	0.40
4.60	0.50	0.50
3.80	0.80	0.40
7.60	1.20	1.00
4.30	0.30	0.00
0.00	0.00	0.30
0.00	0.00	0.00
0.00	0.00	0.00
0.00	0.00	0.00
0.00	0.00	0.00

Output without dummyFun (and/or Option 1):

Loading configuration...
loadJson: Failed to read file, using default configuration
0
0
1.00
0.00	0.00	0.00
0.00	0.00	0.00
0.00	0.00	0.00
0.00	0.00	0.00
0.00	0.00	0.00
0.00	0.00	0.00
0.00	0.00	0.00
0.00	0.00	0.00
0.00	0.00	0.00
0.00	0.00	0.00

Thank you,

Andrej

@mr_ak47

Your topic was Moved to it’s current location / section as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.