Creation of flexible INI File

Hii All,

I am doing my first project in Arduino, Since I am not an expert in Arduino, I am facing lot of difficulties but learn a lot.

Right now I am working with INI file and it contains Sound Profiles. It can be 1 or more than 1 Sound Profiles.

My idea is I want to read the first section "PROFILES" i.e How many Sound Profile it contains and in which order?

So everytime, when user press a button, it should get new Sound Profile and get new parameters from section. I am reffering, Stevemarple library Using his library I am able to read the ini files but now my task to choose specific Sound profile based on button pressed

For that I have sampler class contains 3 objects each object has one wave file.

My problem is, I want to read the first section "PROFILES" i.e How many Sound Profile it contains and in which order?
I was planning to use do while loop to get how many number of Sound Profile I have.

Please find the code snippet and ini file and how to link the button pressed with sound Profile?

In the code snippet I am giving manually the number 1,2 which is not flexible and we cannot say it as stand alone device. if suppose user want 10 profile he has to manually change the code. So I want read beginning 1 from section PROFILES until you have no value.

if (ini.getValue("PROFILES", "1", buffer1, bufferLen)) {
    Serial.print("section 'PROFILES' has an entry '1' with value ");
    Serial.println(buffer1);
  }
if (ini.getValue("PROFILES", "2", buffer1, bufferLen)) {
    Serial.print("section 'PROFILES' has an entry '2' with value ");
    Serial.println(buffer1);
  }

INI

[PROFILES]
1=PROFILE1
2=PROFILE5
3=PROFILE4
4=PROFILE2
5=PROFILE3

[PROFILE1]
sound_file_name1 = "/SoundP~1/bell.wav"

#Volume Parameters#
A1 = 10
B1 = 0

#Speed Parameters#
C1 = 0
D1 = 1

#Silence Parameters#
E = 0
F = 1000

[PROFILE2]
sound_file_name2 = /SoundP~1/steam.wav

#Volume Parameters#
A2 = 1
B2 = 1

#Speed Parameters#
C2 = 20
D2 = 0

#Silence Parameters#
E = 1
F = 0

[PROFILE3]
sound_file_name3 = /SoundP~1/ferrari.wav

#Volume Parameters#
A3 = 1
B3 = 1

#Speed Parameters#
C3 = 0
D3 = 1

#Silence Parameters#
E = 0
F = 1000

[PROFILE4]
#PROFILE2
sound_file_name2 = /SoundP~1/steam.wav

#Volume Parameters#
A2 = 1
B2 = 1

#Speed Parameters#
C2 = 20
D2 = 0

#Silence Parameters#
E = 0
F = 1000

#PROFILE3
sound_file_name3 = /SoundP~1/ferrari.wav

#Volume Parameters#
A3 = 1
B3 = 1

#Speed Parameters#
C3 = 20
D3 = 0

#Silence Parameters#
E = 0
F = 1000

#Mixing factor #
H1 = 1
H2 = 1
H11 = 0
H22 = 0

[PROFILE5]
#PROFILE1
sound_file_name1 = /SoundP~1/bell.wav

#Volume Parameters#
A1 = 1
B1 = 1

#Speed Parameters#
C1 = 0
D1 = 1

#Silence Parameters#
E = 0
F = 1000

#PROFILE2
sound_file_name2 = /SoundP~1/steam.wav

#Volume Parameters#
A2 = 1
B2 = 1

#Speed Parameters#
C2 = 0
D2 = 1

#Silence Parameters#
E = 0
F = 1000

#PROFILE3
sound_file_name3 = /SoundP~1/ferrari.wav

#Volume Parameters#
A3 = 1
B3 = 1

#Speed Parameters#
C3 = 0
D3 = 1

#Silence Parameters#
E = 0
F = 1000

H0 = 0
H1 = 0
H2 = 0
H00 = 0.3
H11 = 0.3
H22 = 0.3

I am reffering, Steve

You assume that we are all intimately familiar with this library. That is an invalid assumption. You need to post a link to the library.

In the code snippet

We do NOT do snippets here. The fine folks at snippets-r-us.com can help you with the snippets.

You could use a for loop to see if there was a value for n in the PROFILES section. If not, you'd know how many PROFILEn sections there are, assuming that the entries in the PROFILES section are in linearly increasing order starting from 1.

Hii PaulS,

Sorry, This is the following library I am using

Three comments do not warrant one response.

Hii PaulS,

Sorry for late reply, As per your suggestion, If I use for loop, it presumes that I know the number of key names then it is a good way i.e n =10, 100 etc. If suppose I don"t know the value of n? What can be done ? Sorry, if I said something silly.

I haven't yet tried, my approach is in this way

const size_t bufferLen = 80;
  char buffer[bufferLen];

// Open SD file
File myFile = SD.open("/config3.ini");

do{
i = i+1;


if (ini.getValue("PROFILES", "key" &i, buffer1, bufferLen)) {
    Serial.print("section "PROFILES" has an entry with value ");
    Serial.println(buffer1);
  }else{

buffer = '\0';


}


}while(buffer != '\0');

If I use for loop, it presumes that I know the number of key names then it is a good way i.e n =10, 100 etc.

So, don't use a for loop, which expects to iterate a fixed number of times. Use a while loop, which expects to iterate until a change happens.

Ok, I will try to implement it and will let you know.

Hii PaulS,

Now I am doing work step by step, firstly, I implemented your idea using for loop and was able to read the Sound Profiles but when I am using while loop , I failed to read the Profiles?

Please find the attachment of Code and ini file

for (i = 1 ; i < 4 ; i++){

itoa(i,number,10);
if(ini.getValue("PROFILES", number , buffer1, bufferLEN))
Serial.println(buffer1);

}
do{

i = i+1;

 if(ini.getValue("PROFILES",  number , buffer1, bufferLen)){
Serial.println(buffer1);

  }while(buffer1 !='\0')
}
[PROFILES]

1=PROFILE4
2=PROFILE3
3=PROFILE5

Hii,

I got the output using while loop I just changed the condition of while(i !=EOF). I get the name of sound Profiles and the maximum number of Sound Profile.

Hello,

Now, I want to read everytime when there is a change in Sound Profile by pressing a button and get new Sound Profiles and get new parameters. How can I achieve this Goal? The newly modified code is as follow which can read the maximum number of Sound Profiles

#include <SD.h>
#include <SPI.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <arduino.h>
#include <string.h>
#include<IniFile.h>
#define EOF -1

void setup() {

  int i = 0;
  int count;
  const size_t bufferLen = 80;
  char buffer1[bufferLen];
  char number[10];
  char* name1[20];

  Serial.begin(9600);

  // setup SD-card
  Serial.println(F("Initializing SD card..."));
  if (!SD.begin(10)) {
    Serial.println(F(" failed!"));
    while (true);
  } else {
    Serial.println("Sucessfully Done");
  }

  const char *filename = "/config3.ini";

  IniFile ini(filename);
  if (!ini.open()) {
    Serial.print("Ini file ");
    Serial.print(filename);
    Serial.println(" does not exist");
    //error("Ini Setting File Does NOt Exist");
  }
  Serial.println("Ini file exists");

  // Check the file is valid. This can be used to warn if any lines
  // are longer than the buffer.
  if (!ini.validate(buffer1, bufferLen)) {
    Serial.print("ini file ");
    Serial.print(ini.getFilename());
    Serial.print(" not valid: ");
    //error("Setting File Is Not Valid");
  }


  do {

    i = i + 1;  // int 1

    itoa(i, number, 10); // number to String 1

    if (ini.getValue("PROFILES",  number , buffer1, bufferLen)) {
      Serial.println(buffer1);

      count = i;

    } else {

      i = -1;
    }

  } while (i != EOF);

Serial.print("MAXIMUM SOUND PROFILES : ");
  Serial.println(count);

  ini.close();

}

void loop() {
}

Now, I want to read everytime when there is a change in Sound Profile by pressing a button

I do not understand this. How can the data on the SD card change randomly? What does pressing a switch have to do with that?

If you put the code to read the data in a function, you could call the function anywhere you needed to.

Hii PaulS,

Actually I was wrong I thought my Sound Profiles are dependent on ini file. I will get the maximum number of Sound Profile and it's order from ini ? for eg (2 = Profile3, i.e When I pressed UP button two times, I should select Sound Profile3 and read the parameters of Sound Profile 3) and so on. I have two buttons, up and down button to change Sound Profiles.