SOLVED Memory issue on Arduino Mega

Hello,
I am working on a project that aims at playing signals that are made of quite a high number of values. They are arrays of 10000 values, values being pwm values, from 0 to 255. Each signal is in its own file.

My code works for up to about 6 signals, and then over that the analogWrite doesn't do anything anymore. If I print to Serial, the value is there, but the output doesn't output anything.
I suspect a memory issue but I am not sure why or how to fix it.
I use an arduino Mega.
Anyway, here is my code. If anyone has an idea why the analogWrite stops working after 6 signals or so. that would be of great help.
Cheers.

signal1.ino (42.4 KB)

memory_tests_forum.ino (2.16 KB)

You have posted two programs. What does each of them do? Do we really have to look at the very long one?

...R

If you can post code, please do so

extern const byte signal1 [] PROGMEM;
extern const byte signal2 [] PROGMEM;
extern const byte signal3 [] PROGMEM;
extern const byte signal4 [] PROGMEM;
extern const byte signal5 [] PROGMEM;
extern const byte signal6 [] PROGMEM;
extern const byte signal7 [] PROGMEM;
extern const byte signal8 [] PROGMEM;
extern const byte signal9 [] PROGMEM;
extern const byte signal10 [] PROGMEM;
extern const byte signal11 [] PROGMEM;
extern const byte signal12 [] PROGMEM;

int sine_values = 9018;
byte* signal_to_play;
int pause = 3333;
int i = 0;
unsigned long last_tick = 0;
int input = 1;
void setup() {
  Serial.begin(9600);
  pinMode(9, OUTPUT);  // sets the pin as output
  TCCR2B = TCCR2B & B11111000 | B00000001;  // for PWM frequency of 31372.55 Hz Arduino Mega
}

void loop() {
    if ((micros() - last_tick) > pause){
      last_tick = micros();
      analogWrite(9, pgm_read_byte(signal_to_play + i));
      i++;
      if (i>= sine_values){
        i = 0;
     
      }
   }
   if (i%1000 == 0){
    serialread();
   }
}
void serialread(){
  if(Serial.available() > 0) {
        //reading serial port
          input = Serial.parseInt();
          select_signal(input);
        }
}

void select_signal(int signal_number){
  if (signal_number > 0 and signal_number < 7){
      pause = 2000;
      sine_values = 10000;   
  }
    else if (signal_number > 6 and signal_number < 13){
      pause = 3333;
      sine_values = 9014;
  }

  if(signal_number == 1){
    signal_to_play = signal1;
  }
  else if(signal_number == 2){
    signal_to_play = signal2;
  }
  else if(signal_number == 3){
    signal_to_play = signal3;
  }
  else if(signal_number == 4){
    signal_to_play = signal4;
  }
  else if(signal_number == 5){
    signal_to_play = signal5;
  }
  else if(signal_number == 6){
    signal_to_play = signal6;
  }
  else if(signal_number == 7){
    signal_to_play = signal7;
  }
  else if(signal_number == 8){
    signal_to_play = signal8;
  }
  else if(signal_number == 9){
    signal_to_play = signal9;
  }
  else if(signal_number == 10){
    signal_to_play = signal10;
  }
  else if(signal_number == 11){
    signal_to_play = signal11;
  }
  else if(signal_number == 12){
    signal_to_play = signal12;
  }
}

Ah I know this Problem. Had the same on my project... I cant Check your ino files since I am on mobile atm. So please Provider some Information. You save your 10.000 long array in progmem? If Not, your 8kb memory runs out ( 10k Byte array in 8kb memory...)

If you using progmem and pgm_read then you need to save your Arrays in proper sector, Else they override the System relevant parts of progmem and nothing works properly anymore.

One more Thing. Using arduino mega, you need pgm_read_far functions. Since pgm read is trimmed to 64kb and mega has 256kb.

Just give me a call about your Code. :slight_smile:

Ok thanks for Posting Code. Here is the solution for your Problem.

Check the file radarpositions.h and check out how I save the Arrays into progmem. Lines 1 and 3.

Also check main.ino for how I read from mega memory. You need this since you have 12x 10kb and this is larger than 64kb.

Wooop, it works!

First of all sorry for attaching the code and not posting it, thanks TheMemberFormerlyKnownAsAWOL!

And yes indeed it was the issue you mentionned dr-o. Using your code as a reference I manage to make mine work. Such an amazing help!
Thank you all!

If that helps somebody, what made it work:

#define PROGMEM_LATE __attribute__((section(".fini1")))
const byte signal [] PROGMEM_LATE = {255,...,...} ;
uint_farptr_t signal_to_play;

//to access the values
signal_to_play = pgm_get_far_address (signal);
analogWrite(9, pgm_read_byte_far((signal_to_play + i * sizeof(byte))));