curious problem with arrays in simple play/record device

Hi

I made a simple record and playback device for a lab task at university. You play sound through a piezo element, controlling the frequency with a potentiometer. Every time you play something the values are recorded in an array and can then be played back by pressing a button. A weird thing I found was that if the array size is over around 400 (it’s a float array), then the whole thing just doesn’t work. There’s no error message or anything, just nothing happens. It can’t be because there’s not enough space on the arduino board – a way bigger array should still easily fit, plus the IDE would tell me if this was the case anyway.

If you delete the bit of code that fills the array with the current freq values (the bit that records), then the whole thing works again (although obviously it won’t record anymore, it’ll just play sound). So I’m pretty sure that’s the bit it doesn’t like. With an array size of around 400 or less though, it all works perfectly. It feels like it’s a matter of processing power, and the board can’t handle it. I don’t understand how that could be the case either though, as for each iteration essentially the same process is happening regardless of the array size → a value is stored into an array and then the index is increased by one. It’s not even a massive problem because 400 is easily big enough for the sake of the project. I’m more curious as to why this happens. Does anyone have any Idea?
Here’s the entire code if it helps:
(NOTE: 400 isn’t the exact value it won’t work after. I didn’t bother finding that out, but it definitely won’t work with values much bigger like 500)

int piezoPin = 8; // pin for piezo element.
int potenPin = 0; // pin for potentiometer.
int playPin = 12; // pin for playback button.
int recPin = 13; // pin for record button.
int recSwitch = 0; // value for record button.
int playSwitch = 0; // value for playback button.
int recPos = 0; // position in array for recording.
boolean erase = false;
float rec[400]; // array holds frequency values to play back.
int sampleRate = 50;

void setup(){
//Serial.begin(9600);
pinMode(piezoPin,OUTPUT);
pinMode(potenPin,INPUT);
pinMode(playPin,INPUT);
pinMode(recPin,INPUT);

}

void loop(){
int potenVal = analogRead(potenPin);
// map the potentiometer values to desired freq values.
float freq = map(potenVal,0,1023,100,1000);
// read button values.
recSwitch = digitalRead(recPin);
playSwitch = digitalRead(playPin);
/* when record button is pressed:
(play button has to not be pressed so you cant try and
record and play back at the same time) /
if(recSwitch ==LOW && playSwitch == HIGH){
// playback live
tone(piezoPin,freq,100); //
if (recPos < 400){
/
record current value of frecuency into array while recPos
is smaller than array size, increasing recPos by 1 each sample
/
rec[recPos] = freq; // ?----- THIS IS THE BIT IT DOESN’T LIKE WITH BIG ARRAYS
recPos++;
// delay by sample rate value
delay(sampleRate);
//Serial.println(recPos);
erase = true;
}
}
// when record is button released:
else{
/
erase all possible previously recorded data passed what has
already been rewritten*/
if(erase == true){
for(int i=recPos;i<400;i++){
rec*=0;*

  • }*

  • }*
    _ /* erase boolean set to true while recording and to false_

  • after data is erased so it only erases once and doesn’t*
    _ continuously repeat and erase all the data */_

  • erase = false;*

  • // set rec pos back to zero*

  • recPos = 0;*

  • }*

  • // when play back button pressed:*

  • if(playSwitch == LOW && recSwitch == HIGH){*

  • // go through rec array and play it back*

  • for(int i=0;i<400;i++){*
    _ /* stop playback if end of recording is before the_
    _ record limit /_
    _ if(rec == 0){_
    _
    break;_
    _
    }_
    _ tone(piezoPin,rec,100);
    delay(sampleRate);
    }
    / delay so button works like switch, giving enough time to_

    _ lift finger before calling functions again*/
    * delay(1000);
    }
    }*_

A weird thing I found was that if the array size is over around 400 (it’s a float array), then the whole thing just doesn’t work. There’s no error message or anything, just nothing happens. It can’t be because there’s not enough space on the arduino board – a way bigger array should still easily fit, plus the IDE would tell me if this was the case anyway.

In Flash, yes. In SRAM, no.

You need to take explicit action to keep the array in PROGMEM (Flash), which you are not doing. Not that it makes any sense in your case, since flash memory is read-only.

ahhh ok thanks : )