2

I'm trying to compile my code but i keep exceeding my global variable memory by 12%. Is there anything i can do in my code to further reduce RAM usage? Or do i have to do modifications to the libraries I'm using? Here is my main program:

    #include <MIDI.h>
#include <FastLED.h>
#include <SoftwareSerial.h>

#define LED_PIN 3
#define NUM_LEDS 60
#define LEDMAP 0xAB5AB5AB5AB5AB5
#define OFFSET 36
#define RANGECHECK if (pitch > 35 && pitch < 96)

SoftwareSerial softSerial(2, 4);
CRGB leds[NUM_LEDS];

MIDI_CREATE_INSTANCE(SoftwareSerial, softSerial, midiB);

void setup() {
  FastLED.addLeds<WS2811, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.clear(true);
  midiB.begin(MIDI_CHANNEL_OMNI);
  midiB.setHandleNoteOn(HandleOn);
  midiB.setHandleNoteOff(HandleOff);
}

void loop() {
  midiB.read();
}

void HandleOn(byte channel, byte pitch, byte velocity)
{
  RANGECHECK
  {
    leds[pitch - OFFSET] = bitRead(LEDMAP, (pitch - OFFSET)) ? CRGB::NavajoWhite : CRGB::Salmon;
    FastLED.show();
  }
}

void HandleOff(byte channel, byte pitch, byte velocity)
{
  RANGECHECK
  {
    leds[pitch - OFFSET] = CRGB::Black;
    FastLED.show();
  }
}

I've reached my limit in optimizing my RAM usage here. Any suggestions for optimizing my code would be appreciated.

0

2 Answers 2

3

FastLED needs to keep the colors of all the pixel in RAM, so that is using a lot of RAM.

To get around this, you could use a strategy that generates the pixel data on the fly as you send it out to the strings. Here is an example of a project that does that...

https://wp.josh.com/2021/04/21/build-a-live-scrolling-tickertape/

Also, SoftSerial keeps a buffer (looks like 64 bytes?) which also uses some RAM. You could use a different approach to serial communications that does not allocate such a large buffer. It would take work, but you could even process the midi bytes as they were received and not need any byte buffer.

1
  • I already tried redefining _SS_MAX_RX_BUFF in my code, but this had no effect on the global variables used
    – Boyfinn
    Aug 22, 2021 at 6:10
1

Since i don't use sysex messages, i got my RAM usage down to 84% by setting the SysExMaxSize

in midi_settings.h to 0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.