For & arrays - confirmation

Hi everyone,
I would like to know if I could simplify this part of my sketch :

char k1 = EEPROM.read (1);
char k2 = EEPROM.read (2);
char k3 = EEPROM.read (3);
char k4 = EEPROM.read (4);
char k5 = EEPROM.read (5);
char k6 = EEPROM.read (6);
char k7 = EEPROM.read (7);
char k8 = EEPROM.read (8);
char k9 = EEPROM.read (9);
char k10 = EEPROM.read (10);

by this :

for (byte i = 1; i < 10; i++)
{
char k = EEPROM.read (i);
}

Not quite.
Check the range of array indices generated by the for loop.
(Most people index C arrays from zero)
Please remember to use code tags when posting code

You could certainly do what you suggest but all that would happen at the end of the for loop is that the variable k would have the value read from EEPROM address 9

You need to read up on arrays and do something like

char k[10];  //declare an array of chars with 10 levels numbered 0 to 9
for (int byte i = 0; i < 10; i++)
{
k[i] = EEPROM.read(i);
}

If what you are reading is to be used as a C style string then the array will need to have 11 levels and you will need to add the trailing zero to level 10 to make it into a string.

Thanks. So, as I need to read the 200 first values in the EEPROM, the part of my sketch will definitely look like this :

char k[200];  //declare an array of chars with 10 levels numbered 0 to 9

for (int byte i = 1; i < 201; i++)
{
k[i] = EEPROM.read(i);
}

So here, if I understand well the sketch, the variable k124 for instance will take her value in the adress 124 of the EEPROM.

I am right ?

No, you are wrong.
There is no variable k124.

so, where is assigned the value of the adress 124 of the EEPROM ?

In k[124]

Remember that C arrays index from zero.

Ah ok, thanks. And I will be able to use this variable like another ?
For instance to print her value on a LCD, like this ?

lcd.clear();
lcd.print(k[124]);

From your earlier post. Note my added/revised comments

char k[200];  //declare an array of chars with 200 levels numbered 0 to 199

for (int byte i = 1; i < 201; i++)  //i will have a value of 200 during the final iteration of the for loop
{
k[i] = EEPROM.read(i);  //**THERE IS NO LEVEL 200 IN THE ARRAY**
}

Be VERY careful with array index variables as it is all to easy to overwrite memory that is not in the array, as above.

As previously pointed out, array levels start at 0, not 1

Ok, so the code would look more like this ?

char k[200];  /*declare an array of chars with 200 levels numbered 0 to 199*/

for (int byte i = 0; i < 200; i++)  /* i will have a value of 199 during the final iteration of the for loop*/
{
k[i] = EEPROM.read(i);  //**THERE IS NO LEVEL 200 IN THE ARRAY**
}

Yes, that's better, except they're "ELEMENTS" not "LEVELS", thus //**THERE IS NO ELEMENT 200 IN THE ARRAY**

That's better

Now look at EEPROM.put() and EEPROM.get() to see how to save or load the whole array with one line of code.

AWOL:
they're "ELEMENTS" not "LEVELS"

That's my fault because I referred to them as levels earlier in the thread

Ok, thanks a lot.

Can I put this “for” loop in the void setup, to avoid 200 lines of codes ?

So to replace this :

char l0 = EEPROM.read (0);
char l1 = EEPROM.read (1);
char l2 = EEPROM.read (2);
// [...]
char l199 = EEPROM.read (199);

by this :

char k[200];  /*declare an array of chars with 200 levels numbered 0 to 199*/

for (int byte i = 0; i < 200; i++)  /* i will have a value of 199 during the final iteration of the for loop*/
{
l[i] = EEPROM.read(i);  //**THERE IS NO ELEMENT 200 IN THE ARRAY**
}

In fact, I don’t know if the void setup can support loops.

The setup function is just that - a function.
Anything you can do in the loop function, you can do in setup.