Go Down

Topic: 1000 booleans? (Read 1 time) previous topic - next topic

magician13134

If I create a 1000 item boolean array, my sketch will not run, even though I have 12000 bytes free, aren't booleans only 1 byte a piece? I can run the program fine with 750 booleans. Any ideas?

Thanks

retrolefty

#1
Oct 13, 2009, 03:36 am Last Edit: Oct 13, 2009, 03:39 am by retrolefty Reason: 1
Quote
Any ideas?


Yea, your "booleans" are variables. Variables are stored in SRAM so they can change value while your program runs. Program statements don't change, they are stored in FLASH memory. 168 chip has 16k of FLASH and 1K of SRAM, 328 chip doubles both those values.

AVR chip has 'Harvard' computer architecture, that means it uses separate program and data memories, unlike Von neumann architecture (like your PC) where program and data reside in same memory.


Make sense?


Lefty

magician13134

Arggg. That makes TONS of sense, thanks so much. Is there any way I can get around that? I have a 328, but I'm using it in another project. Can I store variables like that in EEPROM? Thanks

CaptainObvious

Sorry I don't have any useful info, but I'm curious, what are you using these 1000 booleans for?

I'm not sure how your program is going to operate, but it seems like there would be an easier way. (I could be totally wrong, I'm just so lost on what you'd use 1000 of em for:D)

Fjornir

Slightly more work but you could use an array of characters - index operator to pick a byte, bitwise operator to pick a bit. That way for each byte consumed you're getting 8 bits to set.

magician13134

The character thing seems like a good idea. I'm using this to record input from an IR remote and play them back later.

mem

#6
Oct 13, 2009, 06:35 am Last Edit: Oct 13, 2009, 06:42 am by mem Reason: 1
here are some functions that I find useful for this kind of thing:
Code: [Select]
const int maxBits = 1024;  // the number of bits to store (make this evenly divisible by 8)

unsigned char bitData[maxBits / 8];  // uses 128 bytes to store up to 1024 bits

// stores the given bitValue using an index ranging from 0 to 1023
void writeBitArray( int bitIndex, boolean bitValue)
{
  int arrayIndex = bitIndex / 8;
  bitWrite(bitData[arrayIndex], bitIndex % 8, bitValue);
}

// returns the bitValue stored at the given index
boolean readBitArray(int bitIndex)
{
  int arrayIndex = bitIndex / 8;
  return  bitRead(bitData[arrayIndex], bitIndex % 8 );
}


useage:
   writeBitArray(i, bit);   // save bit in location given by i
   readBitArray(i);   // return the bit value at location i

magician13134

Ha ha! Thanks, that's a very useful piece of code right there. I will definitely use that.

RIDDICK

#8
Oct 19, 2009, 05:58 pm Last Edit: Oct 19, 2009, 06:00 pm by RIDDICK Reason: 1
i would like to point to this:
http://www.arduino.cc/en/Reference/PROGMEM
(just for completeness)...

-arne
-Arne

zhanx

Quote
The character thing seems like a good idea. I'm using this to record input from an IR remote and play them back later.


Quote
i would like to point to this:
http://www.arduino.cc/en/Reference/PROGMEM
(just for completeness)...

-arne


You can change the stuff store in the PROGMEM at runtime. Cool tell me how so i can do it.

RIDDICK

ohoh
i wasnt aware that u want to write to that array...
i dont know how to write to the flash memory (but it must be possible somehow)... writing to flash memory might significantly reduce the life time of the flash memory (but i'm not sure)...

sorry...   :-[

-arne
-Arne

Udo Klein

Have a look at the atmel application notes  AVR101 and AVR105

--> writing large amounts of data to flash is faster than writing them to EEprom

--> writing flash is more tricky than writing EEprom

--> there are algorithms for "wear leveling" the application notes describe a very practical one.

Check out my experiments http://blog.blinkenlight.net

mem

Hi Udo,

As it says in AVR105, Flash can only be reprogrammed from the Boot section of memory and so modifying flash at runtime means modifying the Boot code. Are you aware of anyone that has successfully done this with the Arduino boot code?

zhanx

if it is possible i would love to know how. it would make life so much easier for me.


AWOL

You'd have to write a custom bootloader, and it's already pretty tight up there.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up