SSD1306 display.clearDisplay()

Is it possible to clear a small portion of the display after void setup() for void loop() code to run.
This is what I have try'd to do and it's not woking, if I add the display.clearDisplay(); in the loop, obviously it will clear what was run in void setup() and the display.print(encoderPos, DEC); is displayed on it's own..

Thanks,

  display.begin(SSD1306_SWITCHCAPVCC);

  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.print("Linear");
  display.setCursor(0,16);
  display.print("Position");
  //display.setCursor(0,40);
  //isplay.setTextSize(3);
  //display.print(encoderPos, DEC);
  display.setTextSize(2);
  display.setCursor(0,24);
  display.print("__________");
  display.setTextSize(2);
  display.setCursor(96,47);
  display.print("mm");
  display.setTextSize(2);
  display.setCursor(0,50);
  display.print("__________");
  display.clearDisplay();
}
void loop(){ 
display.clearDisplay(10,10);
  display.setCursor(0,40);
  display.setTextSize(3);
  display.print(encoderPos, DEC);
}

I guess that's a no then... =(

I guess that's a no then...

No link to the device in question.
No link to the library you are using.
Incomplete code.

And, you expect an answer in less than 2 hours?

Ok... Lets simplify this...

Can, you "clear" x by x pixels, not the entire display?

Ok... Lets simplify this...

Can, you "clear" x by x pixels, not the entire display?

Yes. I can. I use Windex and a Q-tip.

There is a sticky topic at the top of this sub-forum describing how to get help on this forum - please read it.

Particularly see the list in the section "6. Getting help on the forum"...

Save the bumf, just a polite yes or no chaps, that's all I wanted :wink:

1 Like

TronSR:
Can, you "clear" x by x pixels, not the entire display?

It is probably possible, see in the library you are using (since you don't want to tell us what you are using...) there is probably a function setPixelColor or something like that, use it in combination with setCursor and for loops and you have your own function ClearRectangle( x1, y1, x2, y2 ) :wink:

1 Like

This is the display I'm using.. :slight_smile: with a Mega2560

And,

the code.

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_DC 48
#define OLED_CS 52
#define OLED_CLK 46
#define OLED_MOSI 44
#define OLED_RESET 50
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
#if (SSD1306_LCDHEIGHT != 32)
#endif
enum PinAssignments {
  encoderPinA = 20,
  encoderPinB = 21,
  zeroButton = 42
};
int encoderPos = 0;
int lastReportedPos = 0;
boolean A_set = false;
boolean B_set = false;
void setup() {
  pinMode(encoderPinA, INPUT); 
  pinMode(encoderPinB, INPUT); 
  pinMode(zeroButton, INPUT);
  digitalWrite(encoderPinA, HIGH);
  digitalWrite(encoderPinB, HIGH);
  digitalWrite(zeroButton, HIGH);
  attachInterrupt(2, doEncoderA, CHANGE);
  attachInterrupt(3, doEncoderB, CHANGE);
  display.begin(SSD1306_SWITCHCAPVCC);
  display.display();
  delay(1000);
}
void loop(){ 
  if (lastReportedPos != encoderPos) {
    lastReportedPos = encoderPos;
  }
  if (digitalRead(zeroButton) == LOW)  {
    encoderPos = 0;
  }
  display.display();
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.print("Linear");
  display.setCursor(0,16);
  display.print("Position");
  display.setCursor(0,40);
  display.setTextSize(3);
  display.print(encoderPos, DEC);
  display.setTextSize(2);
  display.setCursor(0,24);
  display.print("__________");
  display.setTextSize(2);
  display.setCursor(96,47);
  display.print("mm");
  display.setTextSize(2);
  display.setCursor(0,50);
  display.print("__________");
}
void doEncoderA(){
  A_set = digitalRead(encoderPinA) == HIGH;
  encoderPos += (A_set != B_set) ? +1 : -1;
}
void doEncoderB(){
  B_set = digitalRead(encoderPinB) == HIGH;
  encoderPos += (A_set == B_set) ? +1 : -1;
}

I would like

  display.display();
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.print("Linear");
  display.setCursor(0,16);
  display.print("Position");              
//
  display.setTextSize(2);
  display.setCursor(0,24);
  display.print("__________");
  display.setTextSize(2);
  display.setCursor(96,47);
  display.print("mm");
  display.setTextSize(2);
  display.setCursor(0,50);
  display.print("__________");

in the void setup()

and this part in void loop()

  display.setCursor(0,40);                
  display.setTextSize(3);           
  display.print("0");

The resin is the refresh on the OLED is a bit slow and I want it nice and fast, much like ruing the code like this

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_DC 48
#define OLED_CS 52
#define OLED_CLK 46
#define OLED_MOSI 44
#define OLED_RESET 50
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
#if (SSD1306_LCDHEIGHT != 32)
#endif

void setup() {
    display.begin(SSD1306_SWITCHCAPVCC);
  display.display();
}
void loop(){ 
  display.display();
  display.clearDisplay();
            
  display.setCursor(0,40);                
  display.setTextSize(3);           
  display.print("0");                     //encoderPos, DEC);

Thanks,

This is the example code for the ssd1306.spi

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

#define OLED_DC 11
#define OLED_CS 12
#define OLED_CLK 10
#define OLED_MOSI 9
#define OLED_RESET 13
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);

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

#define LOGO16_GLCD_HEIGHT 16 
#define LOGO16_GLCD_WIDTH  16 
static unsigned char PROGMEM logo16_glcd_bmp[] =
{ B00000000, B11000000,
  B00000001, B11000000,
  B00000001, B11000000,
  B00000011, B11100000,
  B11110011, B11100000,
  B11111110, B11111000,
  B01111110, B11111111,
  B00110011, B10011111,
  B00011111, B11111100,
  B00001101, B01110000,
  B00011011, B10100000,
  B00111111, B11100000,
  B00111111, B11110000,
  B01111100, B11110000,
  B01110000, B01110000,
  B00000000, B00110000 };

#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif

void setup()   {                
  Serial.begin(9600);
  
  // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  display.begin(SSD1306_SWITCHCAPVCC);
  // init done
  
  display.display(); // show splashscreen
  delay(2000);
  display.clearDisplay();   // clears the screen and buffer

  // draw a single pixel
  display.drawPixel(10, 10, WHITE);
  display.display();
  delay(2000);
  display.clearDisplay();

  // draw many lines
  testdrawline();
  display.display();
  delay(2000);
  display.clearDisplay();

  // draw rectangles
  testdrawrect();
  display.display();
  delay(2000);
  display.clearDisplay();

  // draw multiple rectangles
  testfillrect();
  display.display();
  delay(2000);
  display.clearDisplay();

  // draw mulitple circles
  testdrawcircle();
  display.display();
  delay(2000);
  display.clearDisplay();

  // draw a white circle, 10 pixel radius
  display.fillCircle(display.width()/2, display.height()/2, 10, WHITE);
  display.display();
  delay(2000);
  display.clearDisplay();

  testdrawroundrect();
  delay(2000);
  display.clearDisplay();

  testfillroundrect();
  delay(2000);
  display.clearDisplay();

  testdrawtriangle();
  delay(2000);
  display.clearDisplay();
   
  testfilltriangle();
  delay(2000);
  display.clearDisplay();

  // draw the first ~12 characters in the font
  testdrawchar();
  display.display();
  delay(2000);
  display.clearDisplay();

  // draw scrolling text
  testscrolltext();
  delay(2000);
  display.clearDisplay();

  // text display tests
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.println("Hello, world!");
  display.setTextColor(BLACK, WHITE); // 'inverted' text
  display.println(3.141592);
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.print("0x"); display.println(0xDEADBEEF, HEX);
  display.display();
  delay(2000);

  // miniature bitmap display
  display.clearDisplay();
  display.drawBitmap(30, 16,  logo16_glcd_bmp, 16, 16, 1);
  display.display();

  // invert the display
  display.invertDisplay(true);
  delay(1000); 
  display.invertDisplay(false);
  delay(1000); 

  // draw a bitmap icon and 'animate' movement
  testdrawbitmap(logo16_glcd_bmp, LOGO16_GLCD_HEIGHT, LOGO16_GLCD_WIDTH);
}


void loop() {
  
}


void testdrawbitmap(const uint8_t *bitmap, uint8_t w, uint8_t h) {
  uint8_t icons[NUMFLAKES][3];
  srandom(666);     // whatever seed
 
  // initialize
  for (uint8_t f=0; f< NUMFLAKES; f++) {
    icons[f][XPOS] = random() % display.width();
    icons[f][YPOS] = 0;
    icons[f][DELTAY] = random() % 5 + 1;
    
    Serial.print("x: ");
    Serial.print(icons[f][XPOS], DEC);
    Serial.print(" y: ");
    Serial.print(icons[f][YPOS], DEC);
    Serial.print(" dy: ");
    Serial.println(icons[f][DELTAY], DEC);
  }

  while (1) {
    // draw each icon
    for (uint8_t f=0; f< NUMFLAKES; f++) {
      display.drawBitmap(icons[f][XPOS], icons[f][YPOS], logo16_glcd_bmp, w, h, WHITE);
    }
    display.display();
    delay(200);
    
    // then erase it + move it
    for (uint8_t f=0; f< NUMFLAKES; f++) {
      display.drawBitmap(icons[f][XPOS], icons[f][YPOS],  logo16_glcd_bmp, w, h, BLACK);
      // move it
      icons[f][YPOS] += icons[f][DELTAY];
      // if its gone, reinit
      if (icons[f][YPOS] > display.height()) {
	icons[f][XPOS] = random() % display.width();
	icons[f][YPOS] = 0;
	icons[f][DELTAY] = random() % 5 + 1;
      }
    }
   }
}


void testdrawchar(void) {
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0);

  for (uint8_t i=0; i < 168; i++) {
    if (i == '\n') continue;
    display.write(i);
    if ((i > 0) && (i % 21 == 0))
      display.println();
  }    
  display.display();
}

void testdrawcircle(void) {
  for (int16_t i=0; i<display.height(); i+=2) {
    display.drawCircle(display.width()/2, display.height()/2, i, WHITE);
    display.display();
  }
}

void testfillrect(void) {
  uint8_t color = 1;
  for (int16_t i=0; i<display.height()/2; i+=3) {
    // alternate colors
    display.fillRect(i, i, display.width()-i*2, display.height()-i*2, color%2);
    display.display();
    color++;
  }
}

void testdrawtriangle(void) {
  for (int16_t i=0; i<min(display.width(),display.height())/2; i+=5) {
    display.drawTriangle(display.width()/2, display.height()/2-i,
                     display.width()/2-i, display.height()/2+i,
                     display.width()/2+i, display.height()/2+i, WHITE);
    display.display();
  }
}

void testfilltriangle(void) {
  uint8_t color = WHITE;
  for (int16_t i=min(display.width(),display.height())/2; i>0; i-=5) {
    display.fillTriangle(display.width()/2, display.height()/2-i,
                     display.width()/2-i, display.height()/2+i,
                     display.width()/2+i, display.height()/2+i, WHITE);
    if (color == WHITE) color = BLACK;
    else color = WHITE;
    display.display();
  }
}

void testdrawroundrect(void) {
  for (int16_t i=0; i<display.height()/2-2; i+=2) {
    display.drawRoundRect(i, i, display.width()-2*i, display.height()-2*i, display.height()/4, WHITE);
    display.display();
  }
}

void testfillroundrect(void) {
  uint8_t color = WHITE;
  for (int16_t i=0; i<display.height()/2-2; i+=2) {
    display.fillRoundRect(i, i, display.width()-2*i, display.height()-2*i, display.height()/4, color);
    if (color == WHITE) color = BLACK;
    else color = WHITE;
    display.display();
  }
}
   
void testdrawrect(void) {
  for (int16_t i=0; i<display.height()/2; i+=2) {
    display.drawRect(i, i, display.width()-2*i, display.height()-2*i, WHITE);
    display.display();
  }
}

void testdrawline() {  
  for (int16_t i=0; i<display.width(); i+=4) {
    display.drawLine(0, 0, i, display.height()-1, WHITE);
    display.display();
  }
  for (int16_t i=0; i<display.height(); i+=4) {
    display.drawLine(0, 0, display.width()-1, i, WHITE);
    display.display();
  }
  delay(250);
  
  display.clearDisplay();
  for (int16_t i=0; i<display.width(); i+=4) {
    display.drawLine(0, display.height()-1, i, 0, WHITE);
    display.display();
  }
  for (int16_t i=display.height()-1; i>=0; i-=4) {
    display.drawLine(0, display.height()-1, display.width()-1, i, WHITE);
    display.display();
  }
  delay(250);
  
  display.clearDisplay();
  for (int16_t i=display.width()-1; i>=0; i-=4) {
    display.drawLine(display.width()-1, display.height()-1, i, 0, WHITE);
    display.display();
  }
  for (int16_t i=display.height()-1; i>=0; i-=4) {
    display.drawLine(display.width()-1, display.height()-1, 0, i, WHITE);
    display.display();
  }
  delay(250);

  display.clearDisplay();
  for (int16_t i=0; i<display.height(); i+=4) {
    display.drawLine(display.width()-1, 0, 0, i, WHITE);
    display.display();
  }
  for (int16_t i=0; i<display.width(); i+=4) {
    display.drawLine(display.width()-1, 0, i, display.height()-1, WHITE); 
    display.display();
  }
  delay(250);
}

void testscrolltext(void) {
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(10,0);
  display.clearDisplay();
  display.println("scroll");
  display.display();
 
  display.startscrollright(0x00, 0x0F);
  delay(2000);
  display.stopscroll();
  delay(1000);
  display.startscrollleft(0x00, 0x0F);
  delay(2000);
  display.stopscroll();
  delay(1000);    
  display.startscrolldiagright(0x00, 0x07);
  delay(2000);
  display.startscrolldiagleft(0x00, 0x07);
  delay(2000);
  display.stopscroll();
}

So if I understand correctly you just want to clear the encoderPos ?

Do you have a problem when for example value is '987', and then the next value is '2' and you see on the display '982', the '98' from the previous value, that's what you want to erase, correct?

Yep. :slight_smile:
The code works fine, I just want to speed up the refresh rate on the display so less is going on in the loop(). Also I just want to learn more about how the code works with the display.

Just had a go at Fritzing

What if you do something like this:

display.setCursor(0,40);                
display.setTextSize(3);
display.print("       "); // print blank spaces to erase previous value?
display.print(encoderPos, DEC);

Also, looked at the library, the display.display(); should be called last, after you draw/print things, because whe you print something it just adds to a buffer, and the display() function is what copy that buffer to the display itself.

But as you call it at the top of the loop, it's like if it was at the end, no difference, just confusing.

Sorry, I'll adjust.. :slight_smile:

guix:
What if you do something like this:

display.setCursor(0,40);                

display.setTextSize(3);
display.print("      "); // print blank spaces to erase previous value?
display.print(encoderPos, DEC);




Also, looked at the library, the display.display(); should be called last, after you draw/print things, because whe you print something it just adds to a buffer, and the display() function is what copy that buffer to the display itself.

But as you call it at the top of the loop, it's like if it was at the end, no difference, just confusing.

i just flip the text color, and re draw the same text:

display.setTextSize(0);
display.setTextColor(BLACK);
display.setCursor(0,0);
//s= Serial.readString();// read the incoming data as string
display.print(voltage);
display.display();
//display.clearDisplay(); // clears the screen and buffer
delay(50);
display.setCursor(0,0);
display.setTextColor(WHITE);
display.print(voltage);
display.display();

guix:
What if you do something like this:

display.setCursor(0,40);                

display.setTextSize(3);
display.print("      "); // print blank spaces to erase previous value?
display.print(encoderPos, DEC);

I was having the same question and this works great for me, thanks :slight_smile:

This is one of those "doh, why didn't i think of this" monent! I just wanted to have a calendar type thing so only wanted to update the time and not the Day/Date so this works perfectly.

display.setCursor(0,25);
display.print(" ");
display.setCursor(0,25);
display.println(timeClient.getFormattedTime());