PROGMEM and OLED Display


Apologies if this has been posted in the wrong section. Please either delete or move to the appropriate section.

I’m working on a project that displays random words on a screen.

I’ve got the words being displayed on the serial monitor with no problem but when I add the OLED display (128x32 I2C) using the adafruit library nothing shows up on the screen.

It’s probably something really easy but I just can’t see what may be wrong.

I’ve checked the screen and it works in my other projects and also with the Adafruit example in the library examples.

Would anybody be willing to look over the code below and see what may be wrong?

#include <avr/pgmspace.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
const char string_0[] PROGMEM = "0"; // "String 0" etc are strings to store - change to suit.
const char string_1[] PROGMEM = "1";
const char string_2[] PROGMEM = "2";
const char string_3[] PROGMEM = "3";
const char string_4[] PROGMEM = "4";
const char string_5[] PROGMEM = "5";
const char string_6[] PROGMEM = "6";
const char string_7[] PROGMEM = "7";
const char string_8[] PROGMEM = "8";
const char string_9[] PROGMEM = "9";
const char string_10[] PROGMEM = "A";
const char string_11[] PROGMEM = "ABLE";
const char string_12[] PROGMEM = "ABORT";
const char string_13[] PROGMEM = "ABOUT";
const char string_14[] PROGMEM = "ABOVE";
const char string_15[] PROGMEM = "ABYSS";
const char string_16[] PROGMEM = "ACROSS";
const char string_17[] PROGMEM = "ACT";
const char string_18[] PROGMEM = "ACTION";
const char string_19[] PROGMEM = "ACTUALLY";
const char string_20[] PROGMEM = "ADAM";
const char string_21[] PROGMEM = "ADD";
const char string_22[] PROGMEM = "ADDITION";
const char string_23[] PROGMEM = "ADJECTIVE";
const char string_24[] PROGMEM = "ADJUST";
const char string_25[] PROGMEM = "ADULTERY";
const char string_26[] PROGMEM = "AFFAIR";
const char string_27[] PROGMEM = "AFRAID";
const char string_28[] PROGMEM = "AFRICA";
const char string_29[] PROGMEM = "AFTER";
const char string_30[] PROGMEM = "AFTERLIFE";
const char string_31[] PROGMEM = "AGAIN";
const char string_32[] PROGMEM = "AGAINST";
const char string_33[] PROGMEM = "AGE";
const char string_34[] PROGMEM = "AGENT";
const char string_35[] PROGMEM = "AGO";
const char string_36[] PROGMEM = "AGREED";
const char string_37[] PROGMEM = "AGRESSION";
const char string_38[] PROGMEM = "AH";
const char string_39[] PROGMEM = "AHEAD";

int answer = 0;

// Then set up a table to refer to your strings.

const char *const string_table[] PROGMEM = {string_0,string_1,string_2,string_3,string_4,string_5,string_6,string_7,string_8,string_9,

char buffer[15];  // make sure this is large enough for the largest string it must hold

void setup() {
  //while (!Serial);  // wait for serial port to connect. Needed for native USB
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever

  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  delay(2000); // Pause for 2 seconds

  // Clear the buffer

void loop() {
  /* Using the string table in program memory requires the use of special functions to retrieve the data.
     The strcpy_P function copies a string from program space to a string in RAM ("buffer").
     Make sure your receiving string in RAM  is large enough to hold whatever
     you are retrieving from program space. */

    answer = random(40);
    strcpy_P(buffer, (char *)pgm_read_word(&(string_table[answer])));  // Necessary casts and dereferencing, just copy.
//tried moving this into the loop to see if it works and it still doesn't show anything
    display.print(buffer);  //NOT WORKING!! Nothing displayed on screen (screen is blank)


void showWord(){
    Serial.println(buffer); //WORKING!
//Adafruit SSD1306 display
    display.setCursor(10, 0);
    display.println(buffer); //NOT WORKING!! Nothing displayed on screen (screen is blank)
    display.display();      // Show initial text

Many Thanks,


All sorted now.

Had to use this line: display.println(strcpy_P(buffer, (char *)pgm_read_word(&(string_table[answer])))); in the showWords() function.

All working now.

Just curious, will the following line work? I'm only able to test with Serial.

  display.println((__FlashStringHelper *)pgm_read_word(&string_table[answer]));

Seems odd that printing buffer to Serial works, but printing the same buffer to the SSD1306 does not. Which arduino are you using? The display take a big chunk of ram for a buffer, it could be that you are running short on available memory.