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);
}