Initiating library stops OLed working fully

Long term listerner, first time caller.

I am trying to write some code that displays air flow that is corrected for standard temperature and pressure. The signal is sent via 4-20mA and is read by the arduino with a ADS1115. The Display is an OLED using 4-pin spi. The user is able to add their parameters using a rotary encoder, and some buttons. This processing is done on an arduino nano (old boot loader).

This is where my problem comes in. I can diplay live data on the OLED, but when i initiate a rotary encoder libary the background data on the OLED disappears, ie the lables for the live data.

I have tried about 7 different rotary encoder libaries and they all have the same problem. I have tried the basic Morse libary and the OLED works. Looking at the CPP of the SimpleRotary lib, there doesnt seam to be anything too challenging for the arduino.

Its time to open this challenge to people smarter than myself, any help will be appreciated.

my code;

// ADS

#include <Adafruit_ADS1X15.h>

Adafruit_ADS1115 ads;  /* Use this for the 16-bit version */

// EEPROM

#include <EEPROM.h>

// Rotary encoder

#include <SimpleRotary.h>

// Pin A, Pin B, Button Pin
SimpleRotary rotary(2,3,4); // The OLED displays correctly when this line is commented out.

// OLED 1309

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

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for SSD1306 display connected using software SPI (default case):
#define OLED_MOSI   9 // connects to D1
#define OLED_CLK   10 // clock to D0
#define OLED_DC    11 // DC
#define OLED_CS    12 // to CS
#define OLED_RESET 13 // to RES
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
                         OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);

// MISC VARS

  uint32_t RAW_Value;
  uint32_t Last_RAW_Value;

  uint32_t Uncorrected_Flow;
  uint32_t Last_Uncorrected_Flow;

  uint32_t Corrected_Flow;
  uint32_t Last_Corrected_Flow;
  
  String Serial_String = "";
  
  float multiplier;

// TIMER VARS

  uint32_t Reading_Timer;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  // ADS
  if (!ads.begin()) {
    Serial.println("Failed to initialize ADS.");
    while (1);
  }

  // OLED 1309
  if (!display.begin(SSD1306_SWITCHCAPVCC)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }

  // EEPROM
  load_From_EEPROM();

  // Timers

  Reading_Timer = millis() + 200;

  Last_RAW_Value = 69;

  Display_labels_and_grid();
}

void loop() {

  // Take reading

  if (Reading_Timer > millis()){

    RAW_Value = ads.readADC_Differential_0_3();

    Reading_Timer = millis() + 200;

    if (RAW_Value != Last_RAW_Value){

      // Print Values

      Display_Readings();
    }
  }

  //Serial.println(RAW_Value);

  while (Serial.available() > 0 ) {
    
    Serial_String = Serial.readString();
    Serial_String.toUpperCase();
    
    Update_Var_and_EEPROM(&multiplier, "M");
    
    Save_to_EEPROM();
    load_From_EEPROM();
  }
}

void Display_Readings() {

  Corrected_Flow = 160;

  print_reading(&Corrected_Flow,&Last_Corrected_Flow , 2, 58, 2);

  print_reading(&RAW_Value,&Last_RAW_Value , 1, 27, 56);

  Uncorrected_Flow = RAW_Value * multiplier;
  print_reading(&Uncorrected_Flow,&Last_Uncorrected_Flow , 1, 97, 56);

  display.display();

}

void print_reading(uint32_t *Value, uint32_t *Last_Value, int Size, int x, int y){

    display.setCursor(x, y);
    display.setTextWrap(false);
    display.setTextSize(Size);

    display.setTextColor(SSD1306_BLACK, SSD1306_BLACK);

    display.print(int(*Last_Value));

    display.setCursor(x, y);

    //display.display();
    display.setTextColor(SSD1306_WHITE, SSD1306_BLACK);
    display.print(int(*Value));

    Serial.print("Last val: ");
    Serial.print(*Last_Value);
    Serial.print(" This val: ");
    Serial.println(*Value);

    *Last_Value = *Value;

}

void Display_labels_and_grid() {

  display.clearDisplay();

  Print_label("Corrected", 1, 0, 0);
  Print_label("Airflow:", 1, 0, 9);

  Print_label("l/min", 1, 97, 9);

  display.drawFastHLine(0, 18, 128, 1);

  display.drawFastHLine(0, 53, 128, 1);

  Print_label("Raw:", 1, 3, 56);
  Print_label("UnCF:", 1, 64, 56);

  display.display();

}

void Print_label(String Name, int Size, int x, int y)
{
  display.setCursor(x, y);
  display.setTextWrap(false);
  display.setTextSize(Size);
  display.setTextColor(SSD1306_WHITE);
  display.print(Name);
}

void Update_Var_and_EEPROM(float *My_var, String Search_Char) {
  Search_Char.toUpperCase();
  int String_Index = Serial_String.indexOf(Search_Char);
  //Serial.print("String index = ");
  //Serial.println(String_Index);
  if (String_Index == 0) {
    //Serial.println("Updated...");

    *My_var = Serial_String.substring(String_Index + 1).toFloat();

    Serial.println(*My_var);
    //Serial.print("Inside the function the var is : ");
    //Serial.println(*My_var);
  }
}

void Save_to_EEPROM() {

  EEPROM.put(10, multiplier);

}

void load_From_EEPROM() {

  EEPROM.get(10, multiplier);

  Serial.println("The following data has been read from EEPROM");
  Serial.println("To change any of these values, Type [a letter] followed by a number.");
  Serial.println("eg. 'M0.164'\n");

  Serial.print("\t[M]ultiplier:");
  Serial.println(multiplier,12);

}

Well, that's odd in a few ways:
1: the actual problem description isn't very clear; perhaps two photos would help, one showing the correct situation and one showing the problem
2: the rotary encoder as far as I can tell right now doesn't actually do anything. It's odd that it would interfere with the displayed information.

A schematic of your circuit might help a bit.

How much free sram does your sketch leave? Yiu say this is a light task for a Nano, but you're doing a bit of graphics and that's more taxing than you might imagine. Did you see any warnings about low memory and stability issues?

Also, I wouldn't use eeprom.put but .update instead. You'll wear out the eeprom in the foreseeable future the way you're doing it now.

At this point I agree with @anon35827816 it feels like it could be memory issues. The Adafruit_SSD1306 library will use a little over half of the Nano's 2K of ram memory, leaving only around 800 bytes free for everything else.

Maybe consider switching to the U8G2 library. That would involve restructuring your code to degree. The advantage is that it uses less ram by updating the display one section at a time, so only needs enough ram for that one section, which might be only one quarter or one eighth as much.

Thanks you all for your help.

I have managed to get the code working, but i dont know how.

I will check out the U8G2 lib if i get any other issues with this project.

koraks, thank you for the advise on using .update, i thought .put did this but looking at the documentation you are correct.

Thanks again.

.put uses .update when writing to the EEPROM.

Ok, my bad, I mixed up .write and .put.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.