Changing font in "loop" on OLED display

Hi,
I've nearly finished my plant watering project, but I'm not able to solve the last problem I'm experiencing.

I would like to place a text in a smaller font than the other one for the output values. When I upload the code on my Arduino Uno, it runs well for some miliseconds, then the "Plant Redeemer" text switches back from standard small to "FreeMono9pt7b". If I don't use "FreeMono9pt7b", all text is small, but I only want the headline to show up in small font. Where is my mistake? :o

#include <Adafruit_GFX.h>  // Include core graphics library for the display
#include <Adafruit_SSD1306.h>  // Include Adafruit_SSD1306 library to drive the display
Adafruit_SSD1306 display(128, 64);  // Create display
#include <Fonts/FreeMono9pt7b.h>  // Add a custom font



int Relay0 = 10;
int Relay1 = 11;
int Relay2 = 12;
int Olive;
int Papyrus;
int Bonsai;
int limit0 = 330;
int limit1 = 300;
int limit2 = 430;

#define soilSensor_Olive A0
#define soilSensor_Papyrus A1
#define soilSensor_Bonsai A2

// 'Plant_Redeemer', 16x20px
const unsigned char PlantRedeemer [] PROGMEM = {
  0x06, 0x00, 0x07, 0x80, 0x07, 0x80, 0x07, 0xc0, 0x07, 0xc0, 0x03, 0xc6, 0xfb, 0xdf, 0x7d, 0xbe, 
  0x3c, 0x3c, 0x1c, 0x30, 0x00, 0x00, 0x01, 0x80, 0x01, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 
  0x00, 0x80, 0x00, 0x00, 0x07, 0xf0, 0x0f, 0xf0
};


void setup()
{
  Serial.begin(9600);
  pinMode (10,OUTPUT); //Set pin 10 as output for the relay
  pinMode (11,OUTPUT); //Set pin 11 as output for the relay
  pinMode (12,OUTPUT); //Set pin 12 as output for the relay
  pinMode(A0,INPUT); //Set pin A0 as analog input for a capacitive soil moisture sensor #1
  pinMode(A1,INPUT); //Set pin A1 as analog input for a capacitive soil moisture sensor #2
  pinMode(A2,INPUT); //Set pin A2 as analog input for a capacitive soil moisture sensor #3
  
//OLED********************************
  delay(100);  // This delay is needed to let the display to initialize
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // Initialize display with the I2C address of 0x3C
  display.clearDisplay();        // Clear the buffer
  display.setTextColor(WHITE);  // Set color of the text
  display.setRotation(0);      // Set orientation. Goes from 0, 1, 2 or 3
  display.setTextWrap(false);  // By default, long lines of text are set to automatically “wrap” back to the leftmost column.
                               // To override this behavior (so text will run off the right side of the display - useful for
                               // scrolling marquee effects), use setTextWrap(false). The normal wrapping behavior is restored
                               // with setTextWrap(true).
  display.dim(0);  //Set brightness (0 is maximun and 1 is a little dim)

  /*display.drawBitmap(1,1,PlantRedeemer, 16, 20, WHITE);
  display.setTextSize(1);
  display.setCursor(22, 2);
  display.print("Plant");
  display.setCursor(22, 15);
  display.print("Redeemer");*/
////OLED********************************

}

void loop()
{
  int Olive = analogRead(soilSensor_Olive);
  int Papyrus = analogRead(soilSensor_Papyrus);
  int Bonsai = analogRead(soilSensor_Bonsai);

  Serial.print("Feuchtigkeitswert Olive:");
  Serial.println(Olive);
  delay(200);
  Serial.print("Feuchtigkeitswert Papyrus:");
  Serial.println(Papyrus);
  delay(200);
  Serial.print("Feuchtigkeitswert Bonsai:");
  Serial.println(Bonsai);
  delay(200);
  
//OLED********************************
  display.clearDisplay();
  display.drawBitmap(1,1,PlantRedeemer, 16, 20, WHITE);
  display.setTextSize(1);
  display.setCursor(22, 2);
  display.print("Plant");
  display.setCursor(22, 15);
  display.print("Redeemer");
  display.setTextSize(0);
  display.setFont(&FreeMono9pt7b);
  display.setCursor(0, 34);  
  display.print("Olive:  ");
  display.println(Olive); 
  display.setCursor(0, 48);
  display.print("Papyrus:");
  display.println(Papyrus);
  display.setCursor(0, 62);
  display.print("Bonsai: ");
  display.println(Bonsai);
  display.display();
////OLED*******************************
  
  if (Olive<limit0)
  {
  digitalWrite(10,LOW);
  }
  else
  {
    digitalWrite(10,HIGH);
  }
  delay(400);
//***********************************************************
  if (Papyrus<limit1)
  {
  digitalWrite(11,LOW);
  }
  else
  {
    digitalWrite(11,HIGH);
  }
  delay(400);
//***********************************************************
  if (Bonsai<limit2)
  {
  digitalWrite(12,LOW);
  }
  else
  {
    digitalWrite(12,HIGH);
  }
  delay(400);
}

Please post a complete program that illustrates the problem otherwise how do we know what you do before and after the snippet that you posted ?

If you want text in different fonts don't you need to explicitly set the required font before printing the text each time ?

Oh ok, I wanted to ease to you identification of the, probably, relevant code lines. I edited the post above.

Mhh....I honestly don't understand what you mean. I need to set the font for the variables to be displayed since I want to use "FreeMono9pt7b" for them, but not for the headline!?

The library needs to know which font to use when printing text and you do

  display.setFont(&FreeMono9pt7b);

In the sketch that you posted you do this once and never tell the program to use another font

I am suggesting that you :

set the font for the heading
position the cursor for the heading
print the heading
set the font for the variable value
position the cursor for the variable value
print the variable value

1 Like

Thank you very much for your help.

Yes I only set it for the variable values, because I would like to use the font Arduino is using when I don't set any font for the heading. The Adafruit GFX fonts are all too big for the heading, and I would like to avoid to create my own font.
Well, it seems I need to create my own font to solve this issue. I was not able to find the "standard" Arduino font, always this Adafruit GFX or ug8 libraries. I don't get it. :cold_sweat:

I gave it a try and created my own font via this website: Font Creator Now Creates Adafruit GFX Fonts – Squix – TechBlog + the youtube video: Create and use custom fonts for Adafruit_GFX libraries - YouTube
but the folder where he puts it to doesn't exist in my Arduino directory. :astonished: So I gave it a try which failed, but the compiler message helped a lot since I could find the correct path for the fonts:
C:\Users\Jogibaer\Documents\Arduino\libraries\Adafruit_GFX_Library\Fonts

Now I'm using my own font and it works as imagined, thank you. :slight_smile:

Final code:

/***************************************************************
 Project: Pflanzenrettung 5.0 by Jogibaer
 ***************************************************************/
#include <Adafruit_GFX.h>  // Include core graphics library for the display
#include <Adafruit_SSD1306.h>  // Include Adafruit_SSD1306 library to drive the display
Adafruit_SSD1306 display(128, 64);  // Create display
#include <Fonts/FreeMono9pt7b.h>  // Add a custom font
#include <Fonts/DejaVu_Sans_10.h>//#include <U8glib.h>
//U8GLIB_SH1106_128X64 u8g(4, 5, 6, 7);



int Relay0 = 10;
int Relay1 = 11;
int Relay2 = 12;
int Olive;
int Papyrus;
int Bonsai;
int limit0 = 330;
int limit1 = 300;
int limit2 = 430;


#define soilSensor_Olive A0
#define soilSensor_Papyrus A1
#define soilSensor_Bonsai A2

// 'Plant_Redeemer', 16x20px
const unsigned char PlantRedeemer [] PROGMEM = {
  0x06, 0x00, 0x07, 0x80, 0x07, 0x80, 0x07, 0xc0, 0x07, 0xc0, 0x03, 0xc6, 0xfb, 0xdf, 0x7d, 0xbe, 
  0x3c, 0x3c, 0x1c, 0x30, 0x00, 0x00, 0x01, 0x80, 0x01, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 
  0x00, 0x80, 0x00, 0x00, 0x07, 0xf0, 0x0f, 0xf0
};


void setup()
{
  Serial.begin(9600);
  pinMode (10,OUTPUT); //Set pin 10 as output for the relay
  pinMode (11,OUTPUT); //Set pin 11 as output for the relay
  pinMode (12,OUTPUT); //Set pin 12 as output for the relay
  pinMode(A0,INPUT); //Set pin A0 as analog input for a capacitive soil moisture sensor #1
  pinMode(A1,INPUT); //Set pin A1 as analog input for a capacitive soil moisture sensor #2
  pinMode(A2,INPUT); //Set pin A2 as analog input for a capacitive soil moisture sensor #3
  
//OLED**************************************************************************************************************
  delay(100);  // This delay is needed to let the display to initialize
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // Initialize display with the I2C address of 0x3C
  display.clearDisplay();        // Clear the buffer
  display.setTextColor(WHITE);  // Set color of the text
  display.setRotation(0);      // Set orientation. Goes from 0, 1, 2 or 3
  display.setTextWrap(false);  // By default, long lines of text are set to automatically “wrap” back to the leftmost column.
                               // To override this behavior (so text will run off the right side of the display - useful for
                               // scrolling marquee effects), use setTextWrap(false). The normal wrapping behavior is restored
                               // with setTextWrap(true).
  display.dim(0);  //Set brightness (0 is maximun and 1 is a little dim)

  /*display.drawBitmap(1,1,PlantRedeemer, 16, 20, WHITE);
  display.setTextSize(1);
  display.setCursor(22, 2);
  display.print("Plant");
  display.setCursor(22, 15);
  display.print("Redeemer");*/
////OLED**************************************************************************************************************

}

void loop()
{
  int Olive = analogRead(soilSensor_Olive);
  int Papyrus = analogRead(soilSensor_Papyrus);
  int Bonsai = analogRead(soilSensor_Bonsai);

  Serial.print("Feuchtigkeitswert Olive:");
  Serial.println(Olive);
  delay(200);
  Serial.print("Feuchtigkeitswert Papyrus:");
  Serial.println(Papyrus);
  delay(200);
  Serial.print("Feuchtigkeitswert Bonsai:");
  Serial.println(Bonsai);
  delay(200);
  
//OLED**************************************************************************************************************
  display.clearDisplay();
  display.drawBitmap(1,1,PlantRedeemer, 16, 20, WHITE);
  display.setFont(&DejaVu_Sans_10);
  display.setTextSize(0);
  display.setCursor(22, 15);
  display.print("Plant Redeemer");
  display.setTextSize(0);
  display.setFont(&FreeMono9pt7b);
  display.setCursor(0, 34);  
  display.print("Olive:  ");
  display.println(Olive); 
  display.setCursor(0, 48);
  display.print("Papyrus:");
  display.println(Papyrus);
  display.setCursor(0, 62);
  display.print("Bonsai: ");
  display.println(Bonsai);
  display.display();
////OLED**************************************************************************************************************
  
  if (Olive<limit0)
  {
  digitalWrite(10,LOW);
  }
  else
  {
    digitalWrite(10,HIGH);
  }
  delay(400);
//***********************************************************
  if (Papyrus<limit1)
  {
  digitalWrite(11,LOW);
  }
  else
  {
    digitalWrite(11,HIGH);
  }
  delay(400);
//***********************************************************
  if (Bonsai<limit2)
  {
  digitalWrite(12,LOW);
  }
  else
  {
    digitalWrite(12,HIGH);
  }
  delay(400);
}

I was not able to find the "standard" Arduino font

There is no such thing as the standard Arduino font. The library may well have a default font but as soon as you change to another one then it will continue to use that one, hence the need to switch between 2 fonts to do what you want