array of structs corrupts Adafruit_SSD1306?

The struct and array:

typedef struct{
  String displayName;
  String loginName;
  String password;
  bool autoSwitch;
}loginData;


const loginData c1 = {"Amazon", "AmazonLogin", "AmazonPassword", true};
const loginData c2 = {"Aliexpress", "AliexpressLogin", "AliexpressPassword", true};
const loginData c3 = {"Ebay", "ebayLogin", "ebayPassword", true};


const loginData creds[] = {c1, c2};

ssd1306 init:

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }

If the array (creds) contains 2 elements, display.begin(SSD1306_SWITCHCAPVCC, 0x3C) returns true, however some pixels randomly light up. If the array contains 3 elements display.begin(SSD1306_SWITCHCAPVCC, 0x3C) return false. Sofar I tested different string lengths and optimization levels but nothing helps. Does somebody know the reason for this?

Try here

My guess is insufficient RAM, but in the absence of code, it's just that. A guess.

Thank you for taking a look at it, here is the complete code and the memory consuption:

#include <Arduino.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 64 // OLED display height, in pixels


#define OLED_RESET     
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


typedef struct{
  String displayName;
  String loginName;
  String password;
  bool autoSwitch;
}loginData;


const loginData c1 = {"Amazon", "AmazonLogin", "AmazonPassword", true};
const loginData c2 = {"Aliexpress", "AliexpressLogin", "AliexpressPassword", true};
const loginData c3 = {"Ebay", "ebayLogin", "ebayPassword", true};


const loginData creds[] = {c1, c2};



void setup(){

  Serial.begin(9600);

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }

  Serial.println("SSD1306 init done");

}

void loop(){

  //

}

DATA: [==== ] 35.9% (used 736 bytes from 2048 bytes)
PROGRAM: [===== ] 52.7% (used 16180 bytes from 30720 bytes)

A 128x64 single bit display is still going to need 1k byte of RAM at least - that brings memory up to nearly 86%.

I'd drop the use of the String class, and put the constant literal strings into PROGMEM.

Without the String class the problems disappear. Here is the final working code:

#include <avr/pgmspace.h>


typedef struct{
  char displayName[20];
  char loginName[20];
  char password[20];
  bool autoSwitch;
}loginData;


const PROGMEM loginData c1 = {"Amazon", "AmazonLogin", "AmazonPassword", true};
const PROGMEM loginData c2 = {"Aliexpress", "AliexpressLogin", "AliexpressPassword", true};
const PROGMEM loginData c3 = {"Ebay", "ebayLogin", "ebayPassword", true};

const loginData creds[] = {c1, c2, c3};

An unused String requires six bytes of RAM, so I think you're still close to the limit.

I'd put in a bit more work to ensure constant string literals stay in PROGMEM.

Mampfi:
Without the String class the problems disappear.

As one would expect. :roll_eyes:

Regrettably, we are unable to make necessary corrections to the Arduino "reference" pages which proceeds to detail a String class but is missing the critical highlighted warning at the top of the page advising not to use that class in programs/ "sketches". :astonished: