Pages: [1] 2   Go Down
Author Topic: 1000 booleans?  (Read 1143 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 25
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 331
Posts: 16518
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: October 12, 2009, 08:39:38 pm by retrolefty » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 25
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Spokane, Washington
Offline Offline
God Member
*****
Karma: 0
Posts: 686
My name is Bob, and I'm an addict.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 163
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 25
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

here are some functions that I find useful for this kind of thing:
Code:
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
« Last Edit: October 12, 2009, 11:42:53 pm by mem » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 25
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Brunsbüttel, SH, F.Rep.GERM
Offline Offline
God Member
*****
Karma: 4
Posts: 596
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

-arne
« Last Edit: October 19, 2009, 11:00:56 am by RIDDICK » Logged

-Arne

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Brunsbüttel, SH, F.Rep.GERM
Offline Offline
God Member
*****
Karma: 4
Posts: 596
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

-Arne

0
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3420
20 LEDs are enough
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

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

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24353
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You'd have to write a custom bootloader, and it's already pretty tight up there.
Logged

"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.

Pages: [1] 2   Go Up
Jump to: