Can't get FastLEDs and SSD1306 to work together

Hello, I have spent days on this. Some description within the code attached. In short I have a FastLED programs changing patterns with push button. I want to display the pattern on a OLED. I can get each to work independently but not together. Sure would like some suggestions. I have used OLED with multiple temperature, time, date etc. successfully but this has me stumped.
Any help, as always, greatly appreciated.
Thanks,


//_________________________________________________________________________
#include <FastLED.h>
#include <OneButton.h>
//#include <SPI.h)>
#include <Wire.h>;
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET  4
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


#define NUM_LEDS  50
#define LED_PIN   3
#define BTN_PIN   7
#define leds_i
CRGB leds[NUM_LEDS];

uint8_t patternCounter = 0;
uint8_t data[ NUM_LEDS];
// Push button connected between pin 7 and GND (no resistor required)
OneButton btn = OneButton(BTN_PIN, true, true);
void setup() {
 display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  FastLED.addLeds<WS2812B, LED_PIN, RGB>(leds, NUM_LEDS);
  FastLED.setBrightness(200);
  Serial.begin(9600);
 /*
  * #define NUMFLAKES     10 // Number of snowflakes in the animation example

#define LOGO_HEIGHT   16
#define LOGO_WIDTH    16
static const unsigned char PROGMEM logo_bmp[] =
{ 0b00000000, 0b11000000,
  0b00000001, 0b11000000,
  0b00000001, 0b11000000,
  0b00000011, 0b11100000,
  0b11110011, 0b11100000,
  0b11111110, 0b11111000,
  0b01111110, 0b11111111,
  0b00110011, 0b10011111,
  0b00011111, 0b11111100,
  0b00001101, 0b01110000,
  0b00011011, 0b10100000,
  0b00111111, 0b11100000,
  0b00111111, 0b11110000,
  0b01111100, 0b11110000,
  0b01110000, 0b01110000,
  0b00000000, 0b00110000 };


 display.display();
  delay(2000); // Pause for 2 seconds

  // Clear the buffer
  display.clearDisplay();

 I CAN GET THE PROGRAM OR THE OLED BUT NOT BOTH TOGETHER???
*/
/*  HELP!! I have been working on this for days, almost non stop. Trying to output the color selected via push button to OLED.
 *   It seems that if I have this anywhere in the program, it prevents the program from working. I can display on the OLED 
 *   without the program running? I have used these OLEDs in other projects but can't seem to get this to work.
//  display.clearDisplay();
//  display.setTextSize(2);
//  display.setTextColor(WHITE);
//  display.setCursor(5, 15);
//  display.println("HELP!! "); // WANT COLOR OF PATTERN TO SHOW ON OLED
//  display.display(); //delay(2000);
 
*/
  btn.attachClick(nextPattern);
  }

//___________________________________________________________________
void loop() {
switch (patternCounter) {

    case 0:
      redSolid();
      break;

    case 1:
      greenSolid();
      break;

    case 2:
      blueSolid();
      break;

    case 3:
      purpleSolid();
      break;

    case 4:
      blueWhiteSolid();
      break;

    case 5:
      blueGlitter();
      break;
}
FastLED.show();
  btn.tick();
}

void nextPattern() {
  patternCounter = (patternCounter + 1) % 6;
  // Change the number after the % to the number of patterns you have
}

                           //------- Put your patterns below -------//

void redSolid() {
  //  _________________________________________________________________________________________RED

  
  Serial.print(patternCounter);
  Serial.println("RED");
  for (int x = 0; x < NUM_LEDS; x += 2) {
    leds[x] = CRGB:: Red; //                                                       RED
  }
  for (int x = 1; x < NUM_LEDS; x += 2) {
    leds[x] = CRGB::Red; //                                                       RED
  }
  FastLED.show();
}

//______________________________________________________________________//GREEN____________

void greenSolid() {
  Serial.print(patternCounter);

  Serial.println("GREEN");
  for (int x = 0; x < NUM_LEDS; x += 2) {
    leds[x] = CRGB::Green; //                                                     GREEN
  }
  for (int x = 1; x < NUM_LEDS; x += 2) {
    leds[x] = CRGB::Green; //                                                     GREEN
  }

  FastLED.show();

}

//_______________________________________________________________________ BLUE_______________

void blueSolid() {
  Serial.print(patternCounter);

  Serial.println("BLUE");
  for (int x = 0; x < NUM_LEDS; x += 2) {
    leds[x] = CRGB::Blue; //                                                     BLUE
    for (int x = 1; x < NUM_LEDS; x += 2) {
      leds[x] = CRGB::Blue; //                                                     BLUE
    }

    FastLED.show();
  }
}
//___________________________________________________________________________________

void purpleSolid() {
  Serial.print(patternCounter);

  Serial.println("PURPLE");
  for (int x = 0; x < NUM_LEDS; x += 2) {
    leds[x] = CRGB::Purple; //                                                     PURPLE
    for (int x = 1; x < NUM_LEDS; x += 2) {
      leds[x] = CRGB::Purple; //                                                     PURPLE
    }

    FastLED.show();
  }
}

//__________________________________________________________________________________________________

void blueWhiteSolid() {
  Serial.print(patternCounter);

  Serial.println("BLUE/WHITE");
  for (int x = 0; x < NUM_LEDS; x += 2) {
    leds[x] = CRGB::Blue; //                                                   BLUE/WHITE
    for (int x = 1; x < NUM_LEDS; x += 2) {
      leds[x] = CRGB::White; //                                                 BLUE/WHITE

    }

    FastLED.show();

  }
}
//___________________________________________________________________________________________________
void blueGlitter() {
  fill_data_array();
  render_data_with_palette();
  add_glitter();
  Serial.print(patternCounter);

  Serial.println("BLUE/GLITTER");
  FastLED.show();
  FastLED.delay(20);
}

void fill_data_array()
{
  static uint8_t startValue = 0;
  startValue = startValue + 2;

  uint8_t value = startValue;
  for ( int i = 0; i < NUM_LEDS; i++) {
    data[i] = triwave8( value); // convert value to an up-and-down wave
    value += 7;
  }
}


CRGBPalette16 gPalette (
  CRGB::Blue, CRGB::Blue, //BLACK                                              CHANGE THESE FOR FOURTH OF JULY RED,WHITE,BLUE
  CRGB::Blue,   CRGB::Blue,  CRGB::Blue,  CRGB::Blue, //RED
  CRGB::Blue,  CRGB::Blue, CRGB::Blue, CRGB::Blue,// WHITE
  CRGB::Blue,  CRGB::Blue, CRGB::Blue, CRGB::Blue,//BLUE
  CRGB::Blue, CRGB::Blue //BLACK
);

void render_data_with_palette()
{
  for ( int i = 0; i < NUM_LEDS; i++) {
    leds[i] = ColorFromPalette( gPalette, data[i], 128, 1);
  }
}

void add_glitter()
{
  int chance_of_glitter =  10; // percent of the time that we add glitter
  int number_of_glitters = 1; // number of glitter sparkles to add

  int r = random8(100);
  if ( r < chance_of_glitter ) {
    for ( int j = 0; j < number_of_glitters; j++) {

      int pos = random16( NUM_LEDS);
      leds[pos] = CRGB::White; // very bright glitter
    }
  }
}



You didn't mention which board you're compiling for, but if it's an Uno or Nano, you're almost certainly running out of memory. Which the return value from the display.begin call would have told you.

Compiling for an Uno gives the following memory usage:

Global variables use 992 bytes (48%) of dynamic memory, leaving 1056 bytes for local variables. Maximum is 2048 bytes.

The Adafruit_SSD1306 library attempts to allocate a further 1024 bytes at runtime, bringing your RAM usage up to 2016 of the available 2048 bytes. A brief look at the FastLED library revealed some more runtime RAM requirements thanks to some new() calls, so I'm going to guess that there isn't 1024 bytes left by the time the program starts running, thanks to FastLED and whatever the stack needs.

You might look into one of the text only SSD1306 libraries that don't use a 1K screen buffer.

Does this compile? Are you getting any error messages?

I am using an NANO, and the program will compile with SSD1306 but the program won't run. uses (19258 of 30720 bytes). Global is (1000 of 2048 bytes).
If I put a Serial.println("TEST"); just under Serial.begin the monitor will display the TEST message then I get four backwards question marks a couple small capital "I's" then four more backward question marks with a small box beside them. And the program doesn't run?? Hope this helps.
Thanks for the input.

Yes it will compile. I have described this in the above replay to van_der_decken.
Thanks

You are using 1000 bytes of RAM for global variables.

The Adafruit_SSD1306 library allocates another 1024 bytes of RAM at runtime.

That leaves 24 bytes of RAM for FastLED and the stack to split between themselves.

That's nowhere near enough. Your program will crash.

I don't know how to make that any clearer.

ok, Thanks. Is there another board I can use that will work?? I have a Teensy 4.0 but have never used one of those also an ESP82xx which I also have not used.

I will repeat my original suggestion, wish you good luck and say good-bye.

OK Thanks

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