My OLED display stops working once I add a second array

My display works fine when only one array (lowercase letters) is used, but once I add about half of the second array (capital letters), my display stops working and it only continues to work in the serial monitor. I thought it might be a power issue (powered with USB from PC), but I can't test any alternatives at the moment.

//This code lets me select any capitalized or lowercase letter and 4 symbols by scrolling through an array with a potentiometer and capitalizing the letter with a button press.

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(128,64,&Wire,OLED_RESET);

int potentValRaw = 0;     //Potentiometer value raw           = 0 - 1023
int potentVal27 = 0;      //Potentiometer value 27: (1023/37) = 0 - 27
String displayChar = "";

String letterLow[] = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",".","-"};
String letterCap[] = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",",","_"};

void setup() {
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64)
  display.clearDisplay();   // clears the screen and buffer
  Serial.begin(9600);
}

void loop() {
  potentValRaw = analogRead(A7);  //Potentiometer pin
  potentVal27 = potentValRaw/37;  //1023/37 = 0 - 27

  if (digitalRead(12) == LOW) displayChar = letterLow[potentVal27];   //Pin 12 is the button pin
  if (digitalRead(12) == HIGH) displayChar = letterCap[potentVal27];  //Used to capitalize letters

  Serial.println(displayChar);

  //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  //Everything above works great in the serial monitor, not so much in the OLED display.
  //The weirdest part is that the display works perfectly fine when only one array is used.
  //This issue only arises once the second array has about half the capital letters. I'm
  //powering this with usb from the PC, so is it a power issue? I can't test it because
  //I have no alternative power sources at the moment.
  
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(64-6,32-12);
  display.println(displayChar);
  display.display();
}

Update; I combined the two arrays into one:

String letterNum[] = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",".","-","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",",","_"};

I also changed line 26 so it adds 28 to potentVal27:

  if (digitalRead(12) == HIGH) displayChar = letterNum[potentVal27+28];  //Used to capitalize letters

I also changed the name of the array. Nothing has changed. The screen still doesn't work. I also tried my second identical OLED screen, and that didn't work either.

The following sketch (your one with slight modifications) shows the characters well with NANO (in font size 1) as I rotate the pot. My OLED has no RESET pin; so, I have set it to -1. 0x3C is the slave address for 128x32 and 0x3D is the slave address for 128x64.

//This code lets me select any capitalized or lowercase letter and 4 symbols by scrolling through an array with a potentiometer and capitalizing the letter with a button press.

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET -1
Adafruit_SSD1306 display(128, 32, &Wire, OLED_RESET);

int potentValRaw = 0;     //Potentiometer value raw           = 0 - 1023
int potentVal27 = 0;      //Potentiometer value 27: (1023/37) = 0 - 27
String displayChar = "";

String letterLow[] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", ".", "-"};
String letterCap[] = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", ",", "_"};

void setup() {
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64)
  display.clearDisplay();   // clears the screen and buffer
  Serial.begin(9600);
  pinMode(12, INPUT_PULLUP);
  display.setTextSize(1);
}

void loop() {
  potentValRaw = analogRead(A7);  //Potentiometer pin
  potentVal27 = potentValRaw / 37; //1023/37 = 0 - 27

  if (digitalRead(12) == LOW)
    displayChar = letterLow[potentVal27];   //Pin 12 is the button pin
  if (digitalRead(12) == HIGH)
    displayChar = letterCap[potentVal27];  //Used to capitalize letters

  Serial.println(displayChar);

  //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  //Everything above works great in the serial monitor, not so much in the OLED display.
  //The weirdest part is that the display works perfectly fine when only one array is used.
  //This issue only arises once the second array has about half the capital letters. I'm
  //powering this with usb from the PC, so is it a power issue? I can't test it because
  //I have no alternative power sources at the moment.

  display.clearDisplay();
  display.setCursor(5, 5);
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(64 - 6, 32 - 12);
  display.println(displayChar);
  display.display();
}

Make your display working and then optimize codes.

1 Like

What is your arduino board?

Using a separate String variable for each letter is definitely like memory killer
Read something about the difference between characters, character arrays, and the String type. For letters you should use char type

1 Like

This code works partially! Thanks! One thing though, the text is stretched. I'll try and figure it out.

Show the picture of the OLED you are using. Is it 4-pin or 5-pin or more? What Arduino you are using?

I agree, you make a good point. It's also easier to type than having to type all those quotation marks. I only learnt about char variables yesterday so I didn't think to use it.

I'm using an arduino nano and this screen White 128X64 OLED LCD LED Display Module For Arduino 0.96" I2C IIC Seria.KF | eBay
It wasn't stretched when I had it working before.

Adafruit_SSD1306 display(128, 32, &Wire, OLED_RESET);

Would changing the resolution here change anything?

note, that your letterLow[] array with String requires at least 8 times more memory than it char analog.
Your program may not run due to lack of memory

1 Like

My setup fully agrees with your setup. So, the sketch of post#3 should produce the same output for both the OLEDs.

b707 made a good point about char variables and memory. I tried it out and it works better now. Out of curiosity, it isn't stretched on your OLED screen?

Thanks for your help. It works great now!

So, what are the flaws that you have discovered in your sketch of post #1?

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