Go Down

Topic: big array occures freeze (Read 2275 times) previous topic - next topic

Flub

Thank you all guys! i got it...
I didnt knew that a byte is something like : B00000000...

Code: [Select]

byte sequenzen[2560];
...
void setup() {
for (int i = 0; i < 2560; i++) {
   sequenzen[i]=B00000000;
}
...
}
...
boolean ReadValue(int seq, int takt, int channel, int p) {
takt--;
int count = (seq*4*10*32)+(takt*10*32)+(channel*32)+p;
count--;
int x = count % 8;
int x2 = (count-x)/8;
return bitRead(sequenzen[x2],x);
}

void WriteValue(int seq, int takt, int channel, int p, boolean value) {
takt--;
int count = (seq*4*10*32)+(takt*10*32)+(channel*32)+p; 
count--;
int x = count % 8;
int x2 = (count-x)/8;
bitRead(sequenzen[x2],x); ;
bitWrite(sequenzen[x2],x,value);
}


this works fine. Now its a pitty that this doesnt fit on the eprom, so i cannot save the sequenzes while the device is off...

AWOL

#16
May 16, 2011, 06:02 pm Last Edit: May 16, 2011, 06:04 pm by AWOL Reason: 1
Doing a bitRead in WriteValue is just a waste of time.
This:
Code: [Select]
takt--;
int count = (seq*4*10*32)+(takt*10*32)+(channel*32)+p;
count--;
int x = count % 8;
int x2 = (count-x)/8;

is common code - why not factor it into a single function?
"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.

jraskell

Quote
Now its a pitty that this doesnt fit on the eprom, so i cannot save the sequenzes while the device is off...


A microSD shield would give you access to up to 2GB of storage.

Flub

oh i forgot to clear the function out of mistakes... i need two for several purposes for example copy and pasting sequences.

Code: [Select]

boolean ReadValue(int seq, int takt, int channel, int p, int Switch) {
takt--;
int count = (seq*4*10*32)+(takt*10*32)+(channel*32)+p;
int x = count % 8;
int x2 = (count-x)/8;
count=-1;
boolean BIT = bitRead(sequenzen[x2],x);
if (Switch) {
  bitWrite(sequenzen[x2],x,inv(BIT));
} else {
  return BIT;
}
}

void WriteValue(int seq, int takt, int channel, int p, boolean value) {
takt--;
int count = (seq*4*10*32)+(takt*10*32)+(channel*32)+p; 
int x = count % 8;
int x2 = (count-x)/8;
count=-1;
bitWrite(sequenzen[x2],x,value);
}


if I see this the right way.... i dont even need the micro sd thing because ive got 4kB eprom : http://arduino.cc/en/Main/ArduinoBoardMega2560

with the Mega1280 ...

I ll see how to save it on the eprom.


Fletcher Chr

Hi Flub,

It may not be an issue with your code but this line:
Code: [Select]
int count = (seq*4*10*32)+(takt*10*32)+(channel*32)+p; 

has a potential risk of roll-over (at 32,767).

-Fletcher

Nick Gammon

#20
May 18, 2011, 08:18 am Last Edit: May 18, 2011, 08:22 am by Nick Gammon Reason: 1
A bit of testing shows you can do it with the STL and a minimal amount of mucking around. I fitted 10 sequences on the Uno (just) so you should be OK with the Mega1280.

Code: [Select]
#include <iterator>
#include <vector>
#include <new.cpp>

#define SEQUENCES 10
#define BARS 4
#define CHANNELS 10
#define NOTES 32

std::vector<bool> sequenzen (SEQUENCES * BARS * CHANNELS * NOTES);

void setItem (const byte seq, const byte bar, const byte  channel, const byte note, const bool value = true)
{
 int i = (seq * BARS * CHANNELS * NOTES) +
         (bar * CHANNELS * NOTES) +
         (channel * NOTES) +
         note;
 sequenzen [i] = value;        
}

bool getItem (const byte seq, const byte bar, const byte  channel, const byte note)
{
 int i = (seq * BARS * CHANNELS * NOTES) +
         (bar * CHANNELS * NOTES) +
         (channel * NOTES) +
         note;
 return sequenzen [i];
}

void setup ()
{
 Serial.begin (115200);

 // testing
 setItem (2, 3, 5, 1);
 setItem (4, 1, 8, 2);

 setItem (SEQUENCES - 1, BARS - 1, CHANNELS - 1, NOTES - 1);
 setItem (0, 0, 0, 0);
 setItem (0, 0, 1, 0);
 setItem (0, 1, 0, 0);
 setItem (1, 0, 0, 0);
 
 Serial.println ("These are set:");
 
 // debugging display
 int j = 0;
 for (std::vector<bool>::const_iterator i = sequenzen.begin ();
      i != sequenzen.end ();
      i++, j++)
    if (*i)
      Serial.println (j);
     
}  // end of setup

void loop ()
{}


The STL "bool" vector uses an efficient storage technique that lets you address bits individually (and fit 8 into a byte). This theoretically would take 1600 bytes (and a memory check confirmed this). My example setItem and getItem show how you just multiply out the wanted sequence/bar/channel/note by the correct number (I hope I got it right) to get to the correct bit in the vector.

My numbers are zero-relative (ie. bars 0, 1, 2, 3 rather than 1, 2, 3, 4) but if you prefer one-relative just change the way the index value is calculated. eg.

Code: [Select]
int i = ((seq - 1) * BARS * CHANNELS * NOTES) +
        ((bar - 1) * CHANNELS * NOTES) +
        ((channel - 1) * NOTES) +
        note - 1;


STL from here:

http://andybrown.me.uk/ws/2011/01/15/the-standard-template-library-stl-for-avr-with-c-streams/

Follow installation instructions on that page.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

MarkT

Consider an external serial EEPROM or even SDcard to store sequences.
[ I won't respond to messages, use the forum please ]

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy