oLED help (clearing a span of pixels?)!?

Okay, So, I’m currently running a TMP-36 and 128x64 oLED display with my Arduino Leonardo Micro.

My goal here is to simply display the temperature read by the TMP on the oLED, so far it works, sorta.

Here is my code so far

int sensorPin = 0; //the analog pin the TMP36's Vout (sense) pin is connected to
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

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


#define LOGO16_GLCD_HEIGHT 16 
#define LOGO16_GLCD_WIDTH  16 

#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
#define yeet "test"
void setup()   {                
  Serial.begin(9600);

  // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64)
  // init done

  
display.clearDisplay();     
display.setTextColor(WHITE);
 display.setTextSize(2);
display.setCursor(32,0); 
display.print("Temp:");
display.display();
}


void loop() {
 int reading = analogRead(sensorPin);  
 
 // converting that reading to voltage, for 3.3v arduino use 3.3
 float voltage = reading * 5.0;
 voltage /= 1024.0; 
 
 // print out the voltage
// Serial.print(voltage); Serial.println(" volts");
 
 // now print out the temperature
 float temperatureC = (voltage - 0.5) * 100 ;  //converting from 10 mv per degree wit 500 mV offset
                                               //to degrees ((voltage - 500mV) times 100)
// Serial.print(temperatureC); Serial.println(" degrees C");
 
 // now convert to Fahrenheit
 float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
display.setTextColor(WHITE);
 display.setTextSize(3);
display.setCursor(24,24); 
display.print(temperatureF);
display.display();
Serial.print(temperatureF); Serial.println(" degrees F");
delay(750);
}

Now, the part I’m stuck on is clearing the previous reading on the oLED. Initially it displays the temperature just fine, but after the first, second, third update, all the pixels that where turned on to display the number before it stays on. The result is a garbled mess which isn’t readable after the first update.

My idea was to just turn the area of which the pixels span black (just to be clear, if the number takes up 12 pixels by 24 pixels, black that area). but I don’t know how to do that? Can anyone help me out? Thanks!

I think you have to draw a rectangle over the text you are updating... look at getTextBounds()

From the Adafruit tutorial:

the open parts of each character show the original background contents, but if you want the text to block out what’s underneath, a background color can be specified as an optional second parameter tosetTextColor().

BulldogLowell:
I think you have to draw a rectangle over the text you are updating… look at getTextBounds()

wildbill:
From the Adafruit tutorial:

Thanks guys, both great answers! Working great now!

I went with the ladder.

Here’s my revised code in case anyone ever stumbles upon this:

int sensorPin = 0; //the analog pin the TMP36's Vout (sense) pin is connected to
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

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


#define LOGO16_GLCD_HEIGHT 16 
#define LOGO16_GLCD_WIDTH  16 

#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
#define yeet "test"
void setup()   {                
  Serial.begin(9600);

  // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64)
  // init done

  
display.clearDisplay();     
display.setTextColor(WHITE);
 display.setTextSize(2);
display.setCursor(32,0); 
display.print("Temp:");
display.display();
}


void loop() {
 int reading = analogRead(sensorPin);  
 
 // converting that reading to voltage, for 3.3v arduino use 3.3
 float voltage = reading * 5.0;
 voltage /= 1024.0; 
 
 // print out the voltage
// Serial.print(voltage); Serial.println(" volts");
 
 // now print out the temperature
 float temperatureC = (voltage - 0.5) * 100 ;  //converting from 10 mv per degree wit 500 mV offset
                                               //to degrees ((voltage - 500mV) times 100)
// Serial.print(temperatureC); Serial.println(" degrees C");
 
 // now convert to Fahrenheit
 float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
display.setTextColor(WHITE, BLACK);
 display.setTextSize(3);
display.setCursor(24,24); 
display.print(temperatureF);
display.display();
Serial.print(temperatureF); Serial.println(" degrees F");
delay(750);
}