How do I change values in the middle of an array?

I have a rather large array, and I need to update some values in the middle of the array (on several rows, but all in the same few columns).

For example, take this array:

static uint8_t SampleImage[] PROGMEM = {
0x0,0x0,0x0,0x0,
0x0,[glow]0x0,0x0[/glow],0x0,
0x0,[glow]0x0,0x0[/glow],0x0,
0x0,0x0,0x0,0x0}

How do I change the highlighted items to something like 0xFF with some simple (easy to understand) code? What if my array is really 240 x 160 in size?

Before we get to any algorithms for updating arrays, do you realize that the PROGMEM keyword declares your array to be in FLASH, thus you cannot change it once your program is running?

--
Check out our new shield: http://www.ruggedcircuits.com/html/gadget_shield.html

I wasn't aware of that, thanks. If I do not store it in progmem, is there an easy way to make this update?

start by making it a two-dimensional array

byte SampleImage[10][20] = { ...  };

then you can access rows and columns separately.

240x160 may be too much for SRAM, though. Do you need a full byte for each "pixel" of the image?

Sorry, beginner's error. The array size is really 4800 bytes (each a hex value). This gets me to 240 x 160 pixels.

The array size is really 4800 bytes

Probably still about 704 bytes too big to fit into even a Mega's RAM.

Hello there;

Rather talking about what you can't do, I thought that it may be better offering you a solution of what you can do.

If you are still looking for a solution for a large array, you can use a microSD card SparkFun microSD Shield - DEV-12761 - SparkFun Electronics. You can download supporting files at the sparkfun site. They can be a bit tricky at first, but once you get it working, it's fine. (If you want some working code, just message me)

You can store large amount of data (I'm using a 2GB card) and only look at a few bytes of it at a time (using a simple offset from the start of a file).

As of now, I don't think that it is compatible with the mega, but I'm currently using it with the Duemilanove.


:slight_smile:

Okay, perhaps I am not stating this correctly. I already have a 4800 item array in progmem, and it works fine. Let's say I have another array of just 100 items stored in the normal Arduino memory which I can change. How do I change elements as a group the easiest way possible in the middle of the array? Or am I forced to call out items by row and column? This means changing 40 items in an array a long process!

You could assign the same value to a bunch of contiguous elements in a row with a call to memset():

uint8_t array[NumRows][NumCols] = { .... whatever .... };

for (i=0 ; i < NumRows; i++) {
   // Set 4 elements at array[i][ColOffset] to 0xFF
   memset(array[i] + ColOffset, '\xFF', 4);
}

--
Check out our new shield: http://www.ruggedcircuits.com/html/gadget_shield.html

But you want to change the array values at run time... right?...

Alligator, are you saying you want a big background image that will remain static during execution, except for a small rectangular area somewhere inside it which can change under program control? (like a window or a superimposed sprite)

Yes, with the exception that I have to update entire rows at a time on my display - not just the individual cells. However, the values near the edges will be zeros, so in essence, this is just the individual group of items in an array.

Ok. I suggest writing a class that has access to the big PROGMEM background image and also store the small window in SRAM.
It should have a getrow() method for use with your display function that will either

  1. read an entire image row from progmem if the row# is below or above the window or
  2. read a few bytes from progmem for the part to the left of the window, read one row from the window and then take the rest of the row from the right edge of the background image.

There are of course many unanswered questions: does the "window" combine with the background or simply replace it? Does the window appear at a fixed location?

That's good information. In this case, I'm trying to write 3 numbers over a specific spot on the screen. Thus, the values would replace the values that are there already. Because I want to count into the hundreds, it is not logical to simply create an entire group of rows to replace the existing rows or I will quickly run out of memory and end up with a very, very long program.

I'd like to create 10 small sections (the numbers 0 - 9) and simply replace those portions in the array. Then I redraw the screen with that portion of the array (those relevant rows) and I'm done.