Thanks for the reply,
I have tried printing empty lines. No good!
One thing I have discovered is that by reducing the FlashStringHelper for loop to just loop once then it simply prints "Messaging" 4 times. This can be adjusted by changing int itemsPerScreen.
Still stuck i'm afraid and the more I look at this code the more messy I think it is and there must be an elegant way of doing this, or that could just be the mechanical engineer inside me screaming to get out ![]()
Anyway, here's the current full code:
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <avr/pgmspace.h>
int itemsPerScreen;
const int fontSize = 8;
static int pinA = 2; // Our first hardware interrupt pin is digital pin 2
static int pinB = 3; // Our second hardware interrupt pin is digital pin 3
static int enSW = 9;
volatile byte aFlag = 0; // let's us know when we're expecting a rising edge on pinA to signal that the encoder has arrived at a detent
volatile byte bFlag = 0; // let's us know when we're expecting a rising edge on pinB to signal that the encoder has arrived at a detent (opposite direction to when aFlag is set)
volatile uint16_t encoderPos = 0; //this variable stores our current value of encoder position. Change to int or uin16_t instead of byte if you want to record a larger range than 0-255
volatile uint16_t
oldEncPos = 0; //stores the last encoder position value so we can compare to the current reading and see if it has changed (so we know when to print to the serial monitor)
volatile byte reading = 0; //somewhere to store the direct values we read from our interrupt pins before checking to see if we have moved a whole detent
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
PROGMEM const char Messaging[] ="Messaging";
PROGMEM const char UVScanner[] ="UV Scanner";
PROGMEM const char SecuritySystem[] ="Security";
PROGMEM const char BACK[] ="Main Menu";
int cnt = 0;
int itemSelected, subMenuSelected;
int itemsToDisplay=0;
int curPos,startPos, endPos;
const char* menu_table[] = {Messaging, UVScanner, SecuritySystem, BACK};
void setup() {
 display.begin();
 pinMode(enSW, INPUT_PULLUP);
 pinMode(pinA, INPUT_PULLUP);
 pinMode(pinB, INPUT_PULLUP);
 attachInterrupt(0,PinA,RISING);
 attachInterrupt(1,PinB,RISING);
 display.clearDisplay();Â
 display.setTextSize(fontSize/8);
 display.setTextColor(WHITE);
 display.println(F("Initialising...."));
 display.display();
 MySerial.begin(1200);
 Serial.begin(1200);
 while(!Serial);//wait for serial to connect
Â
Â
Â
}
void loop() {
Â
Â
 display.setTextSize(1);
 display.clearDisplay();
 display.setCursor(25,0);
 display.println(F("MENU SYSTEM"));
 display.setCursor(20,25);
 display.println(F("Press control to enter the menu system"));
 display.setTextSize(fontSize/8);
 display.display();
Â
 Â
Â
// Build the top level menu if select Switch is pressed
if(digitalRead(enSW)==0){
  while(digitalRead(enSW)==0);//wait till switch is released.
 Â
  Buildmenu();
}
  Â
}Â
 Â
 Â
/*******************************Utility Functions *******************************/
void PinA(){
 cli(); //stop interrupts happening before we read pin values
 reading = PIND & 0xC; // read all eight pin values then strip away all but pinA and pinB's values
 if(reading == B00001100 && aFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
  encoderPos --; //decrement the encoder's position count
  bFlag = 0; //reset flags for the next turn
  aFlag = 0; //reset flags for the next turn
 }
 else if (reading == B00000100) bFlag = 1; //signal that we're expecting pinB to signal the transition to detent from free rotation
 sei(); //restart interrupts
}
void PinB(){
 cli(); //stop interrupts happening before we read pin values
 reading = PIND & 0xC; //read all eight pin values then strip away all but pinA and pinB's values
 if (reading == B00001100 && bFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
  encoderPos ++; //increment the encoder's position count
  bFlag = 0; //reset flags for the next turn
  aFlag = 0; //reset flags for the next turn
 }
 else if (reading == B00001000) aFlag = 1; //signal that we're expecting pinA to signal the transition to detent from free rotation
 sei(); //restart interrupts
}
Â
Â
 void Buildmenu(){
 Â
int itemsPerScreen = 4;Â //CHANGING THIS INT WILL ADJUST THE NUMBER OF REPEATED PRINTS OF THE
                    MENU OPTIONS???????Â
int menuLength = 4;
do{
  Â
      startPos = encoderPos%menuLength; Â
      display.clearDisplay();
  Â
      endPos = itemsPerScreen;
     Â
      if(menuLength < itemsPerScreen)
      {
        endPos = menuLength -startPos;Â
      }
  Â
      if((menuLength-startPos)<itemsPerScreen)
      {
        endPos = menuLength -startPos;
      }
  Â
      for(cnt = 0; cnt<=(endPos-1); cnt++){
        if(cnt == 0)
        {
         display.setCursor(0,0);
         display.print(F("Scroll to select"));
         display.drawFastHLine(0,10,200,WHITE);
         display.setCursor(0,16 +cnt*fontSize);// sets the selection pointer
         display.print(F("->"));
        Â
        }
       Â
        for(int i = 0; i < 1; i++){
        Â
       Â
         display.setCursor(16,16 +cnt*fontSize);// sets the height of the submenu text
         display.println((__FlashStringHelper*)menu_table[i]);
        Â
        Â
        Â
     Â
        Â
        Â
       Â
        }
       Â
       Â
      }
     Â
   display.display();
      cnt = 0;
     if(oldEncPos != encoderPos) {
      oldEncPos = encoderPos;Â
     }Â
}while(digitalRead(enSW));
while(digitalRead(enSW)==0); //wait till switch is reseleased
cnt = 0;
return startPos;
Â
Â
Â
}
As always, my undying gratitude and firstborn for any help!
Cheers