too many arrays

Okay this issue is driving me crazy. First off here is my code:

long freq[9];
int countnote = 0;
int countbeat = 0;
long a[5000];
int b[5000];
int j = 0;
long average1 = 0;
int play2 = 1;
int playback1 = 0;
int playstart = 0;
void setup() {
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(4, INPUT);
  pinMode(5, INPUT);
  pinMode(6, INPUT);
  pinMode(7, INPUT);
  pinMode(8, INPUT);
  pinMode(9, INPUT);
  pinMode(10, INPUT);
  pinMode(11, OUTPUT);
  pinMode(12,INPUT);
  pinMode(13,INPUT);
  freq[0] = 220;
  freq[1] = 246.9;
  freq[2] = 261.6;
  freq[3] = 293.7;
  freq[4] = 329.2;
  freq[5] = 349.2;
  freq[6] = 392;
  freq[7] = 440;
  freq[8] = 493.9;/*
  int i;
  for(i = 0;i<5000;i++)
  {
    a[i] = 0;
    b[i] = 0;
  }
  */
}
void play1()
{
  int i;
  long sum = 0;
  int count = 0;
  long average2 = 0;
  for(i =2;i<11;i++)
  {
    if(digitalRead(i) == HIGH)
    {
      sum = sum + freq[i-2];
      ++count;
    }
  }
  if(count > 0)
  {
    average2 = sum/count;
    tone(11,average2);
  }
  
  else
  {
    average2 = 0;
    noTone(11);
  }/*
//this if statement will run when a new note or non note is played(average1 is not equal to average2)
  if(average1 != average 2 && j < 5000)
  {
    a[j] = millis() - playstart;
    b[j] = average1;
    average1 = average2;
    playstart = millis();
    ++j;
  }
  
  */
}
/*
void playback()
{
  int i;
  for(i = 0; i<5000; i++)
  {
    if(b[i] > 0)
    {
      tone(11,b[i],a[i]);
    }
    else
    {
      delay(a[i]);
    }
  }
}  
*/

void loop()
{
  //  if(digitalRead(12) == HIGH)
 // {
      //playstart = millis();
  //  play2 = 1;
  //  playback1 = 0;
 // }
 // else if(digitalRead(13) == HIGH)
 // {
 //   playback1 = 1;
  //  play2 = 0;
  //}
  if(play2 == 1)
  {
    play1();
  }
  //else
  //{
   // playback();
  //}
}

Now I have a board set up so that when I press a specific button it plays a certain tone out to the speaker, so in effect it is an electronic keyboard. Now I am trying to code it so that when in play mode it will record and you can play it back. My issue is that when I uncomment the commented sections the speaker will no longer play tones when the buttons are pressed. As you can see it is locked in play mode with the comments. The commented sections only alter arrays which have nothing to do with playing the notes, and are only to store the notes played into memory. So when I uncomment the piano should still work. It's almost like my code has too many "statements" to process.

Help Please.

b stores frequencies
a stores the length of the note

With 5000 into and 5000 long variables you are using 30,000 of the available 2000 bytes of RAM available to you. No doubt that doesn't work.

Probably because your playback() function pause the program (due to the delay call) so it won't process the key presses. i suggest you look the "blink without delay" example to see how to make delayed code without pausing the program :slight_smile:

Delta_G:
With 5000 into and 5000 long variables you are using 30,000 of the available 2000 bytes of RAM available to you. No doubt that doesn't work.

Oh yes, this also..didn't see those big arrays! Try using PROGMEM to store such big arrays :slight_smile:

PS DeltaG how do you know he only have 2kb of SRAM? Maybe he is using another board, the Mega have 8kb :wink:

Ok, I didn't know that was taking up so much memory. I am trying to use PROGMEM but it is now telling me those variables are now only read only. How do I change values using this?

Sorry I forgot about progmem being read only... Do you really need such big arrays? Maybe use an external memory such as an SD card ?

(If you try the Ehnanced Arduino IDE, it will report an error when you use too much memory, instead of uploading a broken program...)

Maybe record a few hundred notes rather than 5 thousand?

If you store the time interval as 3 bytes you can still get 4.66 hours between notes (hardly realistic!) with a resolution of 1 mS.

If you store the time as 2 bytes (unsigned int) you could get 1 minute between each note.

Or if you cut the resolution down a bit (eg, to the nearest 250th of a second), 4 minutes between notes (with 2 bytes per time interval).

I just realized I am already overboard with memory even before those arrays. Once I comment out the playback() and play()(record) menu section I am already up to 3716 bytes. I will have to study about using sd cards along with an arduino. I can use smaller arrays but I still need a lot of memory.

I am already up to 3716 bytes

What do you mean? The figure reported by the compiler is program memory. You have around 32 K of that.

Oh, you are right, there was another issue with the code. Since I did not have pins 12 and 13 connected I was getting random digitalread() values back. I will continue working on this...

guix:
PS DeltaG how do you know he only have 2kb of SRAM? Maybe he is using another board, the Mega have 8kb :wink:

8kb still < 30kb

I got it to work. Hooray! My arrays store int values and can store up to 450 values each. My IDE still only shows how much memory the program is taking up, but doesn't show how much memory the variables are taking up.

What is the download link to this enhanced IDE you speak of?

And by the way I only have 2kb of SRAM. I have the arduino uno. I think thats correct. Thanks guys. I wouldn't have known it was a memory issue.

Here is link to Ehnanced Arduino IDE: http://arduino.cc/forum/index.php/topic,118440.0.html