Help please with arrays.

Hi everyone,

i´m trying to get the handle on arrays. Basically what I want to know is if there is a way to storage the arrays values in the Arduino. I heard that you can do so in the EEPROM or the SRAM memories. However, if that´s the case, I don´t know how to do so.

The other question is if I can use arrays to program for example a line maze solver.

Any simple code on the subjet of arrays and ways to storage values on Arduino would be very helpful.

Thanks!

Yes you can use arrays in both SRAM and program memory. Program memory can only be used for arrays that never change like look up tables. You have to use SRAM, (that's normal memory) for an array you want to write to.

Arrays have many uses, see:- http://www.thebox.myzen.co.uk/Tutorial/Arrays.html

Very helpful Grumpy_Mike. It helps me to understand a better way arrays and its possibilities.

However, to know if i understand the basics well please tell me if this more basic approach is also right.

This is my array:

int mySensVals[6] = {2, 4, -8, 3, 2};

I can assing a new value to my array, so for example now the value 2 is replaced by the value 10:

mySensVals[0] = 10;

Is this value stored in the SRAM or fhe Flash memory of the Arduino? I think it´s stored in the SRAM memory buy please let me know if I am right.

Now to retrieve a value from my array:

x = mySensVals[4];

In this case the value that I will retrieve would be the number 3.

Please tell me if my reasoning is right o wrong. Thanks!

I can assing a new value to my array, so for example now the value 2 is replaced by the value 10:

mySensVals[0] = 10;

Yes.

I think it´s stored in the SRAM memory

Correct.

Now to retrieve a value from my array: x = mySensVals[4]; In this case the value that I will retrieve would be the number 3.

Doh! So close, You forgot that array elements start at 0 so that line would retrieve the fifth element ( good name for a film ) from the array which is 2, to retrieve the 3 use:-

x = mySensVals[3];

With your help Grumpy_Mike I´m getting much closer to finally understand arrays.

Let me ask you one more thing please. I am trying to make a code where I replace the value 2 by 10 and then serial monitor shows the number 10.

I know that some of the instructions should be like the following below:

int mySensVals[6] = {2, 4, -8, 3, 2};

mySensVals[0] = 10;

Serial.println x = mySensVals[0];

However I don´know how to structure the instructions in a code correctly to get the task done. I have tried many variations on the code and always get errors as I don´t know well what should I put in void setup and void loop.

Can you please show my how to make this very first code with arrays? Thanks!

int mySensVals[6] = {2, 4, -8, 3, 2};

void setup(){
Serial.begin(9600);
mySensVals[0] = 10;
}
void loop(){
x = mySensVals[0];
Serial.println(x);
// or 
Serial.println (mySensVals[0]);
delay(800);
}

I know I am being a pain in the neck. But I still can´t get it working.

When I upload this code I don´t get any print on serial monitor:

int mySensVals[5] = {2, 4, -8, 3, 2};

void setup(){
Serial.begin(9600);
mySensVals[0] = 10;
}
void loop(){
mySensVals[0];
Serial.println (mySensVals[0]);
delay(800);
}

On the other hand, when I upload the other code:

int mySensVals[6] = {2, 4, -8, 3, 2};

void setup(){
Serial.begin(9600);
mySensVals[0] = 10;
}
void loop(){
x = mySensVals[0];
Serial.println(x);
delay(800);
}

I get the error "x" was not declared in this scope

Is there anything else that I am still missing? Thanks.

The first code works, I have just tried it. The second code needs

int x = mySensVals[0];

No if you are not seeing anything have you got the serial monitor set to the same baud rate as the sketch. Look in the bottom right hand corner of the window frame, if it does not say 9600 then use the drop down menu to make it say so.

You were right. There was some kind of problem with Serial Monitor. I restarted the PC and now both codes work perfectly.

So the original quest is sorted thanks to your help Grumpy_Mike. Now I know how to use a simple array and how the values are stored in the SRAM memory.

To go a little bit further, can you tell me please what should I change in the code we where working with to save the mySensVals[0] = 10 in the flash memory and retreive it from it when I restart the Arduino? I know I should use PROGMEN but all I have seen use sample codes far too complicated for me to understand.

can you tell me please what should I change in the code to save the mySensVals[0] = 10 in the flash memory and retreive it from it even if I restart the Arduino?

You can not do this. The only arrays in program memory are fixed and can not be changed.

To initialise an array in program memory use:-

const prog_uchar PROGMEM bankLSB[] = { 0, 16, 25, 43, 96 };

to retrieve these data use:- x = pgm_read_byte_near(&bankLSB[0]) where the number in [ ] is the element number you want to access. You can never change the contents.

Thanks again.

I uploaded the code to store the values in frash memory:

void setup(){
Serial.begin(9600);
}
void loop(){
const prog_uchar PROGMEM bankLSB[] = { 0, 16, 25, 43, 96 };
}

Then I tried to retrieve a value using this code below:

int x =pgm_read_byte_near

void setup(){
Serial.begin(9600);
}
void loop(){
x = pgm_read_byte_near(&bankLSB[0])
Seriel.println(x);
delay(800);
}

When I try to upload the code I get the error "expected constructor, destructor or type conversion before void" I think the problem may be the int x =pgm_read_byte_near sentence, but I don´t know how should I declare the variable in this case.

So all in all, it seems that I can store the values in PROGMEM with the first code shown but the code to retrieve and print in serial monitor one of the values has something wroing in it.

Can you tell me please what am I doing wrong now? Thanks for the patience Grumpy_Mike!

You can not do this.
When you store something in program memory it becomes part of the program. That data is not available to any other program, so when you load a new program that data is wiped out. The point about using program memory is that the array takes up no SRAM which is free for other things.

What you are trying to do is to make a value persistent through power down and through change in program. The only way to do this is to store data in EEPROM memory.
This is not like an array but is a 1K space where you can store bytes. You have to specift where in that 1K space you want to store things, this is called an address. You access it with the EEPROM write instructions. However there is a limit on how many times you can write to it ( 100,000 ) so never put these instructions in a loop that gets executes over and over, as you could destroy this memory in less than a minute.

To store date in EEPROM you can do this:-

  #include <EEPROM.h>
void setup(){
EEPROM.write(0, 20);  // address & contents
EEPROM.write(1, 21);
EEPROM.write(2, 22);
}
void loop(){
}

then another program (any other ) can retrieve this

  #include <EEPROM.h>
void setup(){
Serial.begin(9600);
}
void loop(){
byte x;
for(int i=0; i<3; i++){
x = EEPROM.read(i);
println(x);
}
delay(800);
}

If you want to store anything other than bytes then you break them down into bytes or use the EEPROM write anything library which does this for you.

I didn´t expect yersterday when I wrote the post that I would learn so many things in one day.

Now it´s time to put it all in practice.

¡Thank you so much Grumpy_Mike for all the help!!!