Skip to main content
fix off-by-one error
Source Link
Mud
  • 191
  • 3

This is a bad strategy in general. You should not be issuing print calls for things you don't want to print and relying on the lcd library to filter for you because they're out of bounds.

Call print for only the choices you want to display.

char* menu[] = {
    "choice 1",
    "choice 2",
    "choice 3",
    "choice 4",
    "choice 5",
};
const int numMenuItems = sizeof menu / sizeof *menu;
const int numLcdRows = 2;


// start out at the top of the menu
int scrollPos = 0;

void drawMenu() {
    for (int i=0; i < numLcdRows; ++i) {
        lcd.setCursor(0, i); 
        lcd.print(menu[i + scrollPos]);
    }
}

To scroll up or down, just adjust the scroll offset:

inline void scrollUp() {
    if (scrollPos > 0)
        scrollPos--;
}

inline void scrollDown() {
    if (scrollPos < numMenuItems - numLcdRows - 1)
        scrollPos++;
}

Note this is untested code written for a library I've never seen (just inferring how it works from your post); it's for illustration purposes only.

This is a bad strategy in general. You should not be issuing print calls for things you don't want to print and relying on the lcd library to filter for you because they're out of bounds.

Call print for only the choices you want to display.

char* menu[] = {
    "choice 1",
    "choice 2",
    "choice 3",
    "choice 4",
    "choice 5",
};
const int numMenuItems = sizeof menu / sizeof *menu;
const int numLcdRows = 2;


// start out at the top of the menu
int scrollPos = 0;

void drawMenu() {
    for (int i=0; i < numLcdRows; ++i) {
        lcd.setCursor(0, i); 
        lcd.print(menu[i + scrollPos]);
    }
}

To scroll up or down, just adjust the scroll offset:

inline void scrollUp() {
    if (scrollPos > 0)
        scrollPos--;
}

inline void scrollDown() {
    if (scrollPos < numMenuItems - numLcdRows - 1)
        scrollPos++;
}

Note this is untested code written for a library I've never seen (just inferring how it works from your post); it's for illustration purposes only.

This is a bad strategy in general. You should not be issuing print calls for things you don't want to print and relying on the lcd library to filter for you because they're out of bounds.

Call print for only the choices you want to display.

char* menu[] = {
    "choice 1",
    "choice 2",
    "choice 3",
    "choice 4",
    "choice 5",
};
const int numMenuItems = sizeof menu / sizeof *menu;
const int numLcdRows = 2;


// start out at the top of the menu
int scrollPos = 0;

void drawMenu() {
    for (int i=0; i < numLcdRows; ++i) {
        lcd.setCursor(0, i); 
        lcd.print(menu[i + scrollPos]);
    }
}

To scroll up or down, just adjust the scroll offset:

inline void scrollUp() {
    if (scrollPos > 0)
        scrollPos--;
}

inline void scrollDown() {
    if (scrollPos < numMenuItems - numLcdRows)
        scrollPos++;
}

Note this is untested code written for a library I've never seen (just inferring how it works from your post); it's for illustration purposes only.

Source Link
Mud
  • 191
  • 3

This is a bad strategy in general. You should not be issuing print calls for things you don't want to print and relying on the lcd library to filter for you because they're out of bounds.

Call print for only the choices you want to display.

char* menu[] = {
    "choice 1",
    "choice 2",
    "choice 3",
    "choice 4",
    "choice 5",
};
const int numMenuItems = sizeof menu / sizeof *menu;
const int numLcdRows = 2;


// start out at the top of the menu
int scrollPos = 0;

void drawMenu() {
    for (int i=0; i < numLcdRows; ++i) {
        lcd.setCursor(0, i); 
        lcd.print(menu[i + scrollPos]);
    }
}

To scroll up or down, just adjust the scroll offset:

inline void scrollUp() {
    if (scrollPos > 0)
        scrollPos--;
}

inline void scrollDown() {
    if (scrollPos < numMenuItems - numLcdRows - 1)
        scrollPos++;
}

Note this is untested code written for a library I've never seen (just inferring how it works from your post); it's for illustration purposes only.