Some time ago I was doing some experiments and wanted to store many small values in an Arduino UNO.
Lets for simplicity sake state it were 1000 analogRead()'s Declaring an array of 1000 int's would blow the memory and I didn't want to use a MEGA.
As an analogRead = 0..1023, it uses only 10 bits of every int. So If I could compact the values then I would only need 10/16 * 1000 = 1250 bytes to store the 1000 reads. A simple prototype worked quite well however with a fixed int ar[1250] I lost quite some valuable RAM.
So I decided it should use dynamic memory, so I could temporary allocate it and free at will. It should also work for any element size, 2,3,5, 8, 13, 21 bits whatever. Sounded not too difficult so I went for malloc() and free() but found out that on an UNO you can only allocate about 250-255 bytes at one time. That meant non continuous memory and a mapping function.
Long story short, it resulted in a class I named BitArray which allows compact int arrays of any size.
The library is still in beta, but stable enough to share. There are currently 2 demo's
demo1: 4000 dice rolls stored in an UNO
demo2: 1000 analogReads stored in an UNO with different bit depths (10, 6, 4) after another.
the demo's show diagnostic info like
Start sketch_nov22e.ino
LIB VERSION: 0.1.03
CAPACITY: 4000
MEMORY: 1500
BITS: 3
SEGMENTS: 8
CAPACITY is the number of elements,
BITS is the size of one element in bits,
MEMORY is memory allocated,
SEGMENTS is the number of allocated memory chunks.
As always comments and remarks are welcome.
Last version - Arduino/libraries/BitArray at master · RobTillaart/Arduino · GitHub
BitArray.zip (3.89 KB)