Go Down

Topic: too many arrays (Read 987 times) previous topic - next topic

mjbourquin

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

Code: [Select]

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

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.

guix

#2
Nov 27, 2012, 04:23 am Last Edit: Nov 27, 2012, 04:30 am by guix Reason: 1
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 :)


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

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

mjbourquin

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?

guix

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

Go Up