How to use FastLed array with flash memory

Hello,

I am coming from the desktop programming and I need some help for my small arduino project.

The project

  • I am building a staircase lights with Leonardo
  • There are 10 stairs with 60 leds per stair.
  • The leds' (WS2812B) data is connected in series
  • There are two sensors. One is located on the top (called MID in the code), the other is located at the bottom.
  • The leds 0 index is on the lowest stair and the 600 index is on the highest.
  • When the MID sensor detects motion it triggers lights stair by stair from MOD to BOT. The BOT sensor is doing the opposite.

The problem
As you could guess, there will be more stairs because right now I have MID and BOT sensors with future TOP sensor. My problem problem is the memory aspect. I realize that using FastLed allocates 3*60=180 bytes per stair. Right now I am getting the following warning:

Sketch uses 6966 bytes (24%) of program storage space. Maximum is 28672 bytes.
Global variables use 2037 bytes (79%) of dynamic memory, leaving 523 bytes for local variables. Maximum is 2560 bytes.

So, if I add the other 300 leds the SRAM will not be enough because the setup function allocates the memory and it is not enough.

Possible solution
I have been reading that people are using the flash memory (which is 10 times bigger) to store the array data and read from there. However, I have no idea how to do that and I need your help.

May be there is other solution (without changing the hardware), I have no idea.

The code so far
NOTE: How do you add code in posts?

#include <FastLED.h>

#define NUM_LEDS 600

#define DATA_PIN 12
#define SENSOR_MID_PIN 5
#define SENSOR_BOT_PIN 6

#define LEDS_PER_STAIR 60

// Define the array of leds
CRGB leds[NUM_LEDS];

void setup() {
 pinMode(SENSOR_MID_PIN, INPUT);
 pinMode(SENSOR_BOT_PIN, INPUT);
 
 Serial.begin(9600);
 while (!Serial);  // wait for serial port to connect. Needed for native USB
 Serial.println("OK");
 
 FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
}

void loop() {
 //Serial.println(F("hello"));
 FastLED.clear();
 byte sensorMid = digitalRead(SENSOR_MID_PIN);
 byte sensorBot = digitalRead(SENSOR_BOT_PIN);

 if (sensorMid == 1 || sensorBot == 1)
 {
   if (sensorBot == 1)
   {
     for (int16_t i = 0; i < NUM_LEDS; i += LEDS_PER_STAIR)
     {
       LitBotToMid(CRGB::Blue, i, LEDS_PER_STAIR + i);
       delay(600);
     }
   }
   else if (sensorMid == 1)
   {
     for (int16_t i = NUM_LEDS; i > 0; i -= LEDS_PER_STAIR)
     {
       LitMidToBot(CRGB::Red, i, i - LEDS_PER_STAIR);
       delay(400);
     }
   }
 }
 else
 {
   Off();
 }

 delay(1000);
}

void Off()
{
 LitBotToMid(CRGB::Black, 0, NUM_LEDS);
}

void LitBotToMid(CRGB color, int16_t start, int16_t end)
{
 for (int16_t i = start; i < end; i++)
 {
   leds[i] = color;
 }
 FastLED.show();
}

void LitMidToBot(CRGB color, int16_t start, int16_t end)
{
 for (int16_t i = start; i >= end; i--)
 {
   if (i >= 0)
   {
     leds[i] = CRGB::Red;
     continue;
   }
   break;
 }
 FastLED.show();
}

Flash memory is not useful for smart LED arrays. It is tricky to write and I am guessing perhaps too slow to read, or at the very least not what the available libraries will deal with.

Who do you say is using them? I’d like to be wrong about this.

If you switched to SPI smart LEDs, you would be able to take your time and compute the contents of each pixel on the fly, as the pesky timing issues with three wire LEDs (which more or less requires one to prepare the entire strip contents ahead of any “show” call) would disappear.

Look for APA 102 strips.

a7

Move to a more capable processor board with more resources.

NOTE: How do you add code in posts?

By putting it inside [code][/code] tags.

One can also use the Code tags button </> on the menu to open a box

That you then paste your code into

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.