Trying to replace a Nokia5110 with an ssd1306 display

I have a nice little Time/Temp control application running on an UNO. It was all working well using the Adafruit_PCD8544.h library with the Nokia5110, but I didn't like the look of that display. I thought I'd be able to drop in an ssd1306 using the Adafruit_SSD1306.h library instead but have run into a brick wall - whenever I try to call my functions or use variables, the display allocation fails. If I comment out all the stuff I need to make this work, the display lights up nicely with my desired appearance. This makes no sense to me as the display should be found before any of my calls.

Here is the full code with the display working but all the calls commented out. As soon as I uncomment anything (i.e. a call to controlTemp()) I get SSD1306 allocation failed


///////////////////////////////////////////////////
//                                               //
//        TIME AND TEMPERATURE CONTROLLER        //
//                                               //
///////////////////////////////////////////////////

#include <SPI.h>
#include <Timer.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>



// Set up One-Wire Bus for DS18B20 Temperature Sensor
// https://github.com/milesburton/Arduino-Temperature-Control-Library
#include <OneWire.h>
#include <DallasTemperature.h>

//// One Wire Bus Settings - plugged into pin 8 on the Arduino UNO  
const byte ONE_WIRE_BUS = 8;
const byte TEMPERATURE_PRECISION = 10;

//// setup digital oneWire sensor addresses
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);



////time and temp variable declarations
#include <EEPROM.h>  //using EEPROM to store run time and temp target
// declare runTime and tempTarge to be updated from EPROM//
int runTime = 30;
double tempTarget = 200;
// declare current time and temp values
double temperature = 200.0;
double runningTime=0;
// invoke timer instance
Timer timer;



//// PID stuff https://github.com/br3ttb/Arduino-PID-Library
#include <PID_v1.h>
//Define Variables we'll be connecting to
double Output;
//Specify the links and initial tuning parameters
PID myPID(&temperature, &Output, &tempTarget, 2,5,1,P_ON_M, DIRECT); // Input = temperature, Setpoint = tempTarget default constants
//PID myPID(&temperature, &Output, &tempTarget, 1.5,1,4,P_ON_M, DIRECT); // constants per coffee-pid on github
//P_ON_M specifies that Proportional on Measurement be used
//P_ON_E (Proportional on Error) is the default behavior
int WindowSize = 2000;  //example is 5000
int whereInWindow;
unsigned long windowStartTime;



// Button adn I/O definitions
// reference https://www.instructables.com/Arduino-Button-Tutorial/
#define BTN_tmpDn        9  // Button
#define BTN_tmpUp        10  // Button
#define BTN_timeDn       11  // Button
#define BTN_timeUp       12  // Button
#define BTN_Start        13  // Button
#define HEAT             14  // to SSR
#define PUMP             15  // to Relay



////set up display
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library. 
// On an arduino UNO:       A4(SDA), A5(SCL)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32 - tg my 128x64 was 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


int i=0;


//////////////////////////////////////
//   Setup
//////////////////////////////////////
void setup()   {

  // for serial monitor output
        Serial.begin(31250);
            Serial.println("");
            Serial.println("");
        Serial.println("TIME/TEMP CONTROLLER Serial Monitor Reporting Restart");
            Serial.println("      **********************");
            Serial.println("      ********start*********");
            Serial.println("      **********************");


  //Initialize Display
	  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  } else Serial.println("found display");

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

  // Clear the buffer
  display.clearDisplay();


 
  //set up control pins
  pinMode(BTN_tmpDn, INPUT);
  pinMode(BTN_tmpUp, INPUT);
  pinMode(BTN_timeDn, INPUT);
  pinMode(BTN_timeUp, INPUT);
  pinMode(BTN_Start, INPUT);

  digitalWrite(BTN_tmpDn, HIGH); // pull-up
  digitalWrite(BTN_tmpUp, HIGH); // pull-up
  digitalWrite(BTN_timeDn, HIGH); // pull-up
  digitalWrite(BTN_timeUp, HIGH); // pull-up
  digitalWrite(BTN_Start, HIGH); // pull-up

  pinMode(HEAT, OUTPUT);
  pinMode(PUMP, OUTPUT);
  digitalWrite(HEAT, LOW);
  digitalWrite(PUMP, LOW);
  
  
  //get initial values for tempTarget and runTime from eeprom values 
  //  EEPROM address 0 is temp  1 is timer
  if (EEPROM.read(0)<50 || EEPROM.read(0)>220) EEPROM.write(0, 200); else tempTarget=EEPROM.read(0);
  if (EEPROM.read(1)<1 || EEPROM.read(1)>60) EEPROM.write(1, 30); else runTime=EEPROM.read(1);
  
        Serial.print("  tempTarget:");
        Serial.print(tempTarget);
        Serial.print("  runTime:");
        Serial.print(runTime);
        Serial.println();




  //Setup PID instance
  //initialize the variables we're linked to (already linked)
  //Input = temperature
  //Setpoint = tempTarget;
  //tell the PID to range between 0 and the full window size
  //myPID.SetOutputLimits(0, WindowSize);
  //turn the PID on
  //myPID.SetMode(AUTOMATIC);

        Serial.print("PID Window: ");
        Serial.print(WindowSize);
    
  
  // read temperature 
    sensors.requestTemperatures();
    temperature = sensors.getTempFByIndex(0);
        Serial.print(" Temp: ");
        Serial.print(temperature);

/*
  // run pid      
      myPID.Compute();
      Serial.print("  PID Output: ");
      Serial.println(Output);
*/




}

//////////////////////////////////////
//   Update Display
//////////////////////////////////////

void updateDisplay() {


 // text display tests
  display.clearDisplay();   // clears the screen and buffer

  //

  // Display Pump Status
 if (digitalRead(PUMP)){
    display.setTextSize(2);
    display.drawLine(1, 0, 58, 0, WHITE);
    display.setTextColor(BLACK, WHITE); // 'inverted' text
    display.setCursor(0,1);
    display.println(F(">BREW"));   // activate icon
  } else {
    display.drawLine(1, 0, 58, 0, WHITE);
    display.drawLine(1, 15, 58, 15, WHITE);
    display.drawLine(1, 0, 1, 15, WHITE);
    display.drawLine(58, 0, 58, 15, WHITE);
    display.setTextColor(WHITE);
    display.setTextSize(1);
    display.setCursor(18,4);
    display.print(F("BREW"));    // label inactive icon
  }
  
  // Display Heater Status
 if (digitalRead(HEAT)){
    display.setTextSize(2);
    display.drawLine(65, 0, 122, 0, WHITE);
    display.setTextColor(BLACK, WHITE); // 'inverted' text
    display.setCursor(64,1);
    display.println(F(">HEAT"));
} else {
    display.drawLine(65, 0, 122, 0, WHITE);
    display.drawLine(65, 15, 122, 15, WHITE);
    display.drawLine(65, 0, 65, 15, WHITE);
    display.drawLine(122, 0, 122, 15, WHITE);
    display.setTextColor(WHITE);
    display.setTextSize(1);
    display.setCursor(82,4);
    display.print(F("HEAT"));    // label inactive icon
  }

  // Display tmpTarget
  display.setTextColor(WHITE);
  display.setTextSize(1);
  display.setCursor(20,19);
  display.print(F("Set Temp: "));  
  display.println(tempTarget, 0);

  // Display Temp
  display.setTextColor(WHITE);
  display.setTextSize(2);
  display.setCursor(0,31);
  display.print(F("Temp:"));  
  display.println(temperature, 1);
  
  
  // Display Time
  display.setTextColor(WHITE);
  display.setTextSize(2);
  display.setCursor(0,49);
  display.print(F("Timer: "));  
  //display.println(runTime);
  display.println(i++);
      

  // Show your work  
  display.display();
  delay(2000);




}



//////////////////////////////////////
//   Change Run Time
//////////////////////////////////////
void changeRunTime(){

while (digitalRead(BTN_Start)) //  user not done
{

  delay(10);                     // debounce
  if (!digitalRead(BTN_timeDn)) { // down time button pushed
    runTime = --runTime;         // decrement run time
    delay(130);                  // reaction time delay
          Serial.println("time Dn");}
  else
  if (!digitalRead(BTN_timeUp)) { // up time button pushed
    runTime = ++runTime;         // increment run time
    delay(130);                  // reaction time delay
          Serial.println("time up");
  }

 // Display Run Time
  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setTextSize(2);
  display.setCursor(6,22);
  display.println("Run Time:");
  display.setCursor(30,36);
  display.println(runTime);
  display.display();
        Serial.println(runTime);
  
}
  // Save Timer Run Time to EEPROM
  if (!digitalRead(BTN_Start)) {  // start button pushed?  
    delay(10);                     // debounce
    if (!digitalRead(BTN_Start))  // button still pushed
    EEPROM.write(1, runTime);      // save new run time to eeprom  1 is timer
    delay(200);         // wait for release of start button
    }
  
}



//////////////////////////////////////
//   Change tempTarget
//////////////////////////////////////
void changeTempTarget(){

while (digitalRead(BTN_Start)) //  user not done
{

  delay(10);                     // debounce
  if (!digitalRead(BTN_tmpDn)) { // down time button pushed
    tempTarget = --tempTarget;         // decrement run time
    delay(130);                  // reaction time delay
          Serial.println("temp Dn");}
  else
  if (!digitalRead(BTN_tmpUp)) { // up time button pushed
    tempTarget = ++tempTarget;         // increment run time
    delay(130);                  // reaction time delay
          Serial.println("temp up");
  }

 // Display Run Time
  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setTextSize(2);
  display.setCursor(6,22);
  display.println("Set Temp:");
  display.setCursor(30,36);
  display.println(tempTarget);
  display.display();
        Serial.println(tempTarget);
  
}
  // Save Temp Target to EEPROM
  if (!digitalRead(BTN_Start)) {   // start button pushed?  
    delay(10);                     // debounce
    if (!digitalRead(BTN_Start))   // button still pushed
    EEPROM.write(0, tempTarget);   // save new run time to eeprom eprom values 0 is temp
    delay(200);                    // wait for release of start button
  }

 
}


//////////////////////////////////////
//   Control Temperature
//////////////////////////////////////
void controlTemp(){

  // read temperature 
  sensors.requestTemperatures();
  temperature = sensors.getTempFByIndex(0);

/*
// basic 1 degree control
  if (temperature < (tempTarget-1)){
    digitalWrite(HEAT, HIGH);
    //      Serial.println("Heat On");
    }
  if (temperature > (tempTarget)){
    digitalWrite(HEAT, LOW);
    //      Serial.println("Heat Off");
    }
*/


// PID control
  // update temp and setpoint
  //Setpoint = tempTarget;
  //Input = temperature;
  // call pid and update output
  myPID.Compute();
        Serial.print(temperature);
        Serial.print("<temp   tempTarget>");
        Serial.print(tempTarget);
        Serial.print("   output>");
        Serial.print(Output);
  whereInWindow = millis()-windowStartTime;
  // turn the output pin on/off based on pid output
  if (millis() - windowStartTime > WindowSize)
  { //time to shift the Relay Window
    windowStartTime += WindowSize;
  }
  // example: if (Output < millis() - windowStartTime) digitalWrite(HEAT, HIGH); else digitalWrite(HEAT, LOW);
              if (Output < whereInWindow) digitalWrite(HEAT, LOW); else digitalWrite(HEAT, HIGH);

        Serial.print("  whereinwindow>");
        Serial.print(whereInWindow);
        Serial.print("  millis>");
        Serial.print(millis());
        Serial.print("  heater>");
        Serial.println(digitalRead(HEAT));
  


}


//////////////////////////////////////
//   Run Timer
//////////////////////////////////////
void runTimer()  {


if (timer.state() != RUNNING){         // if we are stopped
  if (!digitalRead(BTN_Start)){         // start the timer
    timer.start();
          Serial.print("time started: ");
          Serial.println(timer.read());
    delay (100);                       // time to get finger off button
    }
} else {                               // we are running
  if (!digitalRead(BTN_Start)){        // stop the timer
      timer.stop();
      runningTime = 0;
      digitalWrite(PUMP, LOW);          //turn off pump
            Serial.println("timer stopped");
      delay (100);                      // time to get finger off button
      return;
      }
    if (timer.read() >= runTime*1000){           //end reached
              Serial.println("end of time");
        timer.stop();
        runningTime=0;                           //reset timer
        digitalWrite(PUMP, LOW);                 //turn off pump
      } else {                                   //else not done
        runningTime = timer.read();             //update time
        runningTime = runningTime/1000;         //scale ms to seconds
        digitalWrite(PUMP, HIGH);                //turn on pump
              Serial.print("time elapsed: ");
              Serial.println(runningTime);
    }
  }
 

  delay (50);  // slow it down a bit during prototyping
}




//////////////////////////////////////
//   Main Loop
//////////////////////////////////////

//int i=0;

void loop() {
/*
if (timer.state() == RUNNING)          // if we are running
  runTimer();
if (timer.state() != RUNNING)        // if we are stopped
  if (!digitalRead(BTN_Start)){         // start or stop the timer?
    delay (10);                        // debounce
    runTimer();
}
*/
/*
if (((WindowSize/4) % i) == 0)  // call pid 4 times per window
{
  i = 0;
  controlTemp();
} else ++i;
*/

//controlTemp();

updateDisplay();
delay(2000);

/*
bool val;
val = digitalRead(HEAT);
if (val== HIGH) {
  digitalWrite(HEAT, LOW);
 }
 else digitalWrite(HEAT, HIGH);
       Serial.print ("heater : ");
       Serial.println(val);

// report button status
        Serial.print(digitalRead(BTN_timeUp));
        Serial.print(" ");
        Serial.print(digitalRead(BTN_timeDn));
        Serial.print(" ");
        Serial.print(digitalRead(BTN_tmpUp));
        Serial.print(" ");
        Serial.print(digitalRead(BTN_tmpDn));
        Serial.print(" ");
        Serial.println(digitalRead(BTN_Start));
*/
/*
//// adjusting target values
if (!digitalRead(BTN_timeDn) || !digitalRead(BTN_timeUp)){
        Serial.println("runTime up or down");
 // up or down time button pushed
  changeRunTime();
  }
if (!digitalRead(BTN_tmpDn) || !digitalRead(BTN_tmpUp)){
        Serial.println("tempTarget up or down");
 // up or down temp button pushed
  changeTempTarget();
  }
*/


}




Try changing things like

Serial.println("TIME/TEMP CONTROLLER Serial Monitor Reporting Restart");

to

Serial.println(F("TIME/TEMP CONTROLLER Serial Monitor Reporting Restart"));

to not make copies of your literal strings in RAM. That'll free up some space for the 1K buffer that the Adafruit_SSD1306 library wants to allocate at run time.

And if that still doesn't do the trick, consider switching to an unbuffered text only library that doesn't have to allocate the 1K buffer.

Yikes! I think that fixed it (need to test further edit: tested further, yes that fixed it, marked as solution).

It appears that all those Serial.prints I put in to help me understand my problem were in fact my problem. I had used that F() literal in several places but not everywhere.

Thank you Van Der Decken!