Oled screen won't always clear with display.clearDisplay()

Hi, This is my first question on here but not my first arduino project, but definitely my most ambitious!
I am making an Avengers themed bed for my son with a sliding door driven my a stepper motor. A keypad to enter a password on and an oled screen to display some images and also text.

My problem is the screen stays on the last image called. The code seams to skip the display.clearDisplay() command, even without a delay the image or text stays until another screen is displayed. The clearDisplay works as the next screen is not written over current screen but I want the screen to revert to clear/blank most of the time.

I am using a mega 2560 and 4tronix oled 128x64, I have removed the stepper commands and images to shorten the code

Any help would be gratefully recieved

#include <AccelStepper.h>
#include <Password.h>
#include <Keypad.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

#define XPOS 0
#define YPOS 1
#define DELTAY 2

Password password = Password( "1234" );

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
    {'1','2','3'},
    {'4','5','6'},
    {'7','8','9'},
    {'*','0','#'}
};

byte rowPins[ROWS] = {8, 7, 6, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {4, 3, 2}; //connect to the column pinouts of the keypad

// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

//screen 1
const unsigned char PROGMEM screen1 [] = {
 
};

//screen 2
const unsigned char PROGMEM screen2 [] = {
 
};

// Define a stepper and the pins it will use
AccelStepper motor(1, 9, 10);
int Enable = 11;  // Stepper enabled when low
int OpenClose = 52;  // Door open/close button
int limitswitchOpen = 51;  // Open position limit switch
int limitswitchClose = 50;  // Closed position limit switch

int pos = 6000;

void setup()
{  
  pinMode(Enable, OUTPUT);
  pinMode(OpenClose, INPUT_PULLUP);
  pinMode(limitswitchOpen, INPUT_PULLUP);  
  pinMode(limitswitchClose, INPUT_PULLUP);
//  motor.setMaxSpeed(5000);
//  motor.setAcceleration(5000);
//  motor.setCurrentPosition(0);
//  digitalWrite(Enable, HIGH);
  
  Serial.begin(9600);
  keypad.addEventListener(keypadEvent); //add an event listener for this keypad

  display.begin(SSD1306_SWITCHCAPVCC, 0x3c);  // initialize with the I2C addr 0x3c (for the 128x64)
  // init done
  // Show image buffer on the display hardware.
  // Since the buffer is intialized with an Adafruit splashscreen
  // internally, this will display the splashscreen.
  display.display();
  delay(2000); //omit delay to hide adafruit slashscreen, sorry!
  display.clearDisplay();
 
  // display splashscreen bitmap display
  display.drawBitmap(5, 5,  screen1, 120, 56, 1);
  display.display();
  delay(2000);
  display.clearDisplay();
}

void loop(){
  keypad.getKey();
  
//display.clearDisplay(); 
  
  if (digitalRead(limitswitchClose) == LOW && digitalRead(OpenClose) == LOW) //open limit switch and open/close button pressed
  {
//    motor.moveTo(13500);             //Set target 
//    dooropen();  		    	 // run the moveup subroutine
    
    
  }
  if (digitalRead(limitswitchOpen) == LOW && digitalRead(OpenClose) == LOW) //open limit switch and open/close button pressed
  {      		    	 
    display.clearDisplay();
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.setCursor(0,1);
    display.println("    DOOR");
    display.println("  CLOSING");
    display.setTextSize(1);
    display.println("");
    display.println("     KEEP CLEAR");
    display.display();
    display.clearDisplay();
   
  }

}

void keypadEvent(KeypadEvent eKey){
  switch (keypad.getState()){
    case PRESSED:
        display.clearDisplay();
        display.setTextSize(2);
        display.setTextColor(WHITE);
        display.setCursor(0,1);
        display.println(" BUTTON");
        display.println(" PRESSED");
        display.display();
        delay(500);
        display.clearDisplay();
        Serial.print("Pressed: ");
	Serial.println(eKey);
	switch (eKey){
	  case '*': checkPassword(); break;
	  case '#': password.reset(); break;
	  default: password.append(eKey);
        
     }
  }
}
void checkPassword(){
  if (password.evaluate() && digitalRead(limitswitchClose) == LOW){  // Password and door on limit switch close
    display.clearDisplay();
    display.drawBitmap(1, 1, screen2, 120, 56, 1); // display Aaron Avengers screen
    display.display();
    display.clearDisplay();
    Serial.println("PASSWORD CORRECT");
//    motor.moveTo(13500); //Set target, open position 
//    dooropen(); // run the dooropen subroutine
    
  }
  else{
    Serial.println("WRONG PASSWORD");
    //display.clearDisplay();
    display.setTextSize(2);
    display.setTextColor(WHITE);
    display.setCursor(0,1);
    display.println("  WRONG");
    display.println(" PASSWORD");
    display.setTextSize(1);
    display.println("");
    display.println("     TRY AGAIN!");
    display.display();
    delay(1500);
  }

}

Hi

I assume clearDisplay() will only clear the internal RAM and an additional call to display.display() is required to transfer the cleared RAM to the display RAM.

But... its only a guess.

Oliver

Hi Oliver

That works!! Brilliant!! Thank you so much. I wish I'd asked before upgrading to a Mega assuming I was on the limit of the Uno's RAM.

Thanks again

Andy