1.3" OLED with U8g2lib - Menus

Hi Guys,
I am following a tutorial on creating menus and submenus.
First of thanks to EEEnthusiast for such tutorial.
His code is here.

I am not using an LCD but a 1.3" OLED with U8g2 library.
I tested this library using “Hello World” example and my OLED does work.

Now I am trying to use the above tutorial using my OLED but for some reasons, I can’t make it work.
I am sure I am doing some silly mistake and can’t seem to figure out where.

I will very much appreciate if anyone can help me solve the problem.

Thanks.

Code:

#include <Arduino.h>
#include <U8g2lib.h>
#include <SPI.h>
#include <Wire.h>
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);

//Input & Button Logic
const int numOfInputs = 4;
const int inputPins[numOfInputs] = {8,9,10,11};
int inputState[numOfInputs];
int lastInputState[numOfInputs] = {LOW,LOW,LOW,LOW};
bool inputFlags[numOfInputs] = {LOW,LOW,LOW,LOW};
long lastDebounceTime[numOfInputs] = {0,0,0,0};
long debounceDelay = 5;


const int numOfScreens = 10;
int currentScreen = 0;
String screens[numOfScreens][2] = {{"Motor Voltage","Volts"}, {"Motor Current", "Amps"}, 
{"Motor Rated HP","HP"},{"Overload Temp.","degC"}, {"Accel Time", "Secs"}, {"Restart Time","Mins"},
{"Analog Out. Curr.","mA"},{"Input Temp.","degC"}, {"Run Time", "Hours"}, {"Start Times","times"}};
int parameters[numOfScreens];


void setup(void) {
  
  for(int i = 0; i < numOfInputs; i++) {
    pinMode(inputPins[i], INPUT);
    digitalWrite(inputPins[i], HIGH); // pull-up 20k
  }
  
  u8g2.begin();
}

void loop(void) {
  u8g2.firstPage();
  do {
    u8g2.setFont(u8g2_font_ncenB14_tr);
    //u8g2.drawStr(0,24,"Hello World!");
    setInputFlags();
    resolveInputFlags();
    printScreen();
  } while ( u8g2.nextPage() );
}

void setInputFlags() {
  for(int i = 0; i < numOfInputs; i++) {
    int reading = digitalRead(inputPins[i]);
    if (reading != lastInputState[i]) {
      lastDebounceTime[i] = millis();
    }
    if ((millis() - lastDebounceTime[i]) > debounceDelay) {
      if (reading != inputState[i]) {
        inputState[i] = reading;
        if (inputState[i] == HIGH) {
          inputFlags[i] = HIGH;
        }
      }
    }
    lastInputState[i] = reading;
  }
}

void resolveInputFlags() {
  for(int i = 0; i < numOfInputs; i++) {
    if(inputFlags[i] == HIGH) {
      inputAction(i);
      inputFlags[i] = LOW;
      printScreen();
    }
  }
}

void inputAction(int input) {
  if(input == 0) {
    if (currentScreen == 0) {
      currentScreen = numOfScreens-1;
    }else{
      currentScreen--;
    }
  }else if(input == 1) {
    if (currentScreen == numOfScreens-1) {
      currentScreen = 0;
    }else{
      currentScreen++;
    }
  }else if(input == 2) {
    parameterChange(0);
  }else if(input == 3) {
    parameterChange(1);
  }
}

void parameterChange(int key) {
  if(key == 0) {
    parameters[currentScreen]++;
  }else if(key == 1) {
    parameters[currentScreen]--;
  }
}

void printScreen() {
  u8g2.print(screens[currentScreen][0]);
  //u8g2.setCursor(0,1);
  u8g2.print(parameters[currentScreen]);
  u8g2.print(" ");
  u8g2.print(screens[currentScreen][1]);
}

ok, not a pure u8g2 question, but I still try my best...

1) The function calls

    setInputFlags();
    resolveInputFlags();

should be called outside the first-page / next-page while loop.

2) printScreen() is called twice, which is redundant and will just waste time: Remove printScreen() call from resolveInputFlags().

3) u8g2.print() requires u8g2.setCursor() for each of the statements. Also note, that the reference position for a string is the lower left of the baseline of the chars (unless you change the reference position): This means you should use u8g2.setCursor(0,15) before the first call to print. Also note that print does not do any carrige returns.

Oliver

Thanks Oliver,

  1. I moved them out right after I posted.. didn't help though.

  2. Alright.

void printScreen() {
  u8g2.setCursor(0,15);
  u8g2.print(screens[currentScreen][0]);
  //u8g2.setCursor(0,1);
  u8g2.print(parameters[currentScreen]);
  u8g2.print(" ");
  u8g2.print(screens[currentScreen][1]);
}

I tried but no success :(

Request to Mods, Please move this thread to Programming section.

Thanks.