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();
}