With some help on this forum, I've got a small routine that flashes programmable LEDs with different patterns to work. I plan to merge this code with other code that makes use of a keypad to determine what messages / patterns get displayed as well as code that alternates individual lights in a specific order.
However, this code alone is pushing the limits of my card's memory (see message below).
Any help on how to get this to work using less memory would be appreciated.
"Sketch uses 8878 bytes (27%) of program storage space. Maximum is 32256 bytes.
Global variables use 1599 bytes (78%) of dynamic memory, leaving 449 bytes for local variables. Maximum is 2048 bytes. Low memory available, stability problems may occur."
And yes...I will be deleting the print statements used for debugging.
// Big thanks to david_2018 on Arduino forum for getting the pointer fun to work
#define NUM_ARRAYS 7
#define ARRAY_SIZE 50
//#include <HashMap.h>
#include <Arduino.h>
#include <FastLED.h>
#define DATA_PIN 7 //this is the data pin connected to the LED strip. If using WS2801 you also need a clock pin
#define NUM_LEDS 50 //change this for the number of LEDs in the strip
#define COLOR_ORDER RGB
CRGB leds[NUM_LEDS];
const CRGB red = CRGB (255, 0, 0);
const CRGB green = CRGB (0, 255, 0);
const CRGB blue = CRGB(0, 0, 255);
const CRGB orange = CRGB(255, 165, 0);
const CRGB white = CRGB(255, 255, 255);
const CRGB off = CRGB(0, 0, 0);
//define an enum so patterns can be referred to by their names
enum patterns {
RedOnly_leds,
GreenOnly_leds,
BlueOnly_leds,
OrangeOnly_leds,
WhiteOnly_leds,
RGBOW_leds,
AllOff_leds,
NUM_patterns //used to indicate the number of patterns
};
//this array is only needed if you want to print out the names corresponding to the pattern arrays
const char patternNames[][16] = {
"RedOnly_leds",
"GreenOnly_leds",
"BlueOnly_leds",
"OrangeOnly_leds",
"WhiteOnly_leds",
"RGBOW_leds",
"AllOff_leds"
};
// Red, Green, Blue, Orange, White with alternating offs Sequence
CRGB patternArrays[NUM_patterns][ARRAY_SIZE] = {
// only the RED bulbs (in same position as when all lit)
//RedOnly_leds
{ red, off, off, off, off, off, off, off, off, off, off, off,
red, off, off, off, off, off, off, off, off, off, off, off,
red, off, off, off, off, off, off, off, off, off, off, off,
red, off, off, off, off, off, off, off, off, off, off, off,
red, off
},
// only the GREEN bulbs (in same position as when all lit)
//GreenOnly_leds
{ off, off, green, off, off, off, off, off, off, off, off, off,
off, off, green, off, off, off, off, off, off, off, off, off,
off, off, green, off, off, off, off, off, off, off, off, off,
off, off, green, off, off, off, off, off, off, off, off, off,
off, green
},
// only the BLUE bulbs (in same position as when all lit)
//BlueOnly_leds
{ off, off, off, off, blue, off, off, off, off, off, off, off,
off, off, off, off, blue, off, off, off, off, off, off, off,
off, off, off, off, blue, off, off, off, off, off, off, off,
off, off, off, off, blue, off, off, off, off, off, off, off,
off, off
},
// only the ORANGE bulbs (in same position as when all lit)
//OrangeOnly_leds
{ off, off, off, off, off, off, orange, off, off, off, off, off,
off, off, off, off, off, off, orange, off, off, off, off, off,
off, off, off, off, off, off, orange, off, off, off, off, off,
off, off, off, off, off, off, orange, off, off, off, off, off,
off, off
},
// only the WHITE bulbs (in same position as when all lit)
//WhiteOnly_leds
{ off, off, off, off, off, off, off, off, white, off, off, off,
off, off, off, off, off, off, off, off, white, off, off, off,
off, off, off, off, off, off, off, off, white, off, off, off,
off, off, off, off, off, off, off, off, white, off, off, off,
off, off
},
//RGBOW_leds
{ red, off, green, off, blue, off, orange, off, white, off, green, off,
red, off, green, off, blue, off, orange, off, white, off, green, off,
red, off, green, off, blue, off, orange, off, white, off, green, off,
red, off, green, off, blue, off, orange, off, white, off, green, off,
red, green
},
//AllOff_leds
{ off, off, off, off, off, off, off, off, off, off, off, off,
off, off, off, off, off, off, off, off, off, off, off, off,
off, off, off, off, off, off, off, off, off, off, off, off,
off, off, off, off, off, off, off, off, off, off, off, off,
off, off
}
};
patterns i;
void setup( void )
{
Serial.begin(9600);
FastLED.addLeds<WS2811, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS); //setting up the FastLED
//for( int i=0; i<NUM_ARRAYS; i++ )
//{
// pArrayPtr = (int *)pgrArrayPtrs[i];
// flash_pattern( pArrayPtr );
//}
}
void loop( void )
{
// Set lights manually to RGBOW colors at the start
memcpy (leds, patternArrays[RGBOW_leds], sizeof patternArrays[RGBOW_leds]);
FastLED.show();
delay(5000);
//for (i = (patterns)0; i < NUM_patterns; i = (patterns)(i + 1)) {
//cycles through all the patterns
//the (pattern) type casting is needed because the compiler doesn't like incrementing an enum directly
int user_input1 = 2;
flash_pattern((patterns)user_input1);
Serial.println( "got here" );
Serial.println( patternNames[user_input1] );
const char user_input2 = OrangeOnly_leds;
flash_pattern((patterns)user_input2);
Serial.println( "got here" );
Serial.println( patternNames[user_input2] );
delay(4000);
//}
}
void flash_pattern( patterns pPtr ) { //flash the pattern passed into this function, then off with 4 seconds delay
memcpy (leds, patternArrays[pPtr], sizeof patternArrays[pPtr] );
FastLED.show();
}