GLCD menu help...... please

hi all im new to the programming world and am having a bit of trouble making a menu on a ks0108

basically what im trying to do it make menus and submenus so i can alter parameters in the submenus

in the code i have managed to highlight the menu headers which is what i was aiming for but i cant seem to enter the submenu, im using a matrix keypad as arrows, while im waiting for tactile buttons to turn up. but if i can get the matrix keypad working i will stick with it.

heres my code if anyone can help id be really appreciative


  • ks0108 GLCDexample
  • (example sketch from original ks0108 library)
  • Basic test code for the Arduino KS0108 GLCD library.
  • This code exercises a range of graphic functions supported
  • by the library and is an example of its use.
  • It also gives an indication of performance, showing the
  • number of frames drawn per second.

#include <glcd.h>
#include “fonts/allFonts.h” // system font
#include “bitmaps/allBitmaps.h” // bitmap
#include “RTClib.h” // rtc library
#include <Wire.h> //wire library for RTC
#include <SimpleTimer.h>
#include <Keypad.h>
#include <Button.h>

//#include “include/glcd_Deprecated.h”
const int led = 40;
const int button = 12;
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
Button button1 = 12;
byte pressCount = 0;
boolean unpiquePress;

// Variables

byte rowPins[ROWS] = {46, 47, 48, 49}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {50, 51, 52, 53}; //connect to the column pinouts of the keypad

//declare arrays
char keys[ROWS][COLS] = {
MainScreenx[2] = {“lights”, “power”};
char* Submenu1x[4] = {“C/W”, “N/W”, “BL”, “R/Bl”};
char* Submenu2x[1] = {“relays”};

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
SimpleTimer timer;

void setup(){
GLCD.Init(NON_INVERTED); // initialise the library, non inverted writes pixels onto a clear screen

pinMode(button, INPUT);
pinMode(led, OUTPUT);


if (! RTC.isrunning()) {
Serial.println(“RTC is NOT running!”);
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(DATE, TIME));
//start up screen
GLCD.DrawBitmap(Mickey, 28,4, BLACK); //draw the bitmap at the given x,y position
GLCD.SelectFont(SystemFont5x7); // switch to fixed width system font

// use timer to call voids
timer.setInterval (10000, GetTime);
timer.setInterval (100, menuScroll);


void loop(){;


void GetTime() {
DateTime now =;

GLCD.CursorTo (16,0);
GLCD.Puts (" “);
GLCD.CursorTo (16,0);
GLCD.print (now.hour(), DEC);
GLCD.print (”:");
GLCD.print (now.minute(), DEC);


void Mainscreen() {

GLCD.CursorTo (1, 1); // heading “lights”
GLCD.print (“Lights”);
GLCD.DrawRect (4, 6, 38, 10, BLACK);
GLCD.CursorTo (1, 3); // cool white
GLCD.print (“C/W”);
GLCD.CursorTo (1, 4); // neutral white and royal blue
GLCD.print (“N/W”);
GLCD.CursorTo (1, 5); // blue
GLCD.print (“Bl”);
GLCD.CursorTo (1, 6); // royal blue
GLCD.print (“R/Bl”);

// power
GLCD.CursorTo (8, 1);
GLCD.print (“Power”);
GLCD.DrawRect (46, 6, 32, 10, BLACK);
GLCD.CursorTo (8, 3);
GLCD.print (“rlay”);
GLCD.CursorTo (8, 4);
GLCD.print (“heat”);
GLCD.CursorTo (8, 5);
GLCD.print (“cool”);

// temp
GLCD.CursorTo (14, 2); // temp button
GLCD.print (“Temp”);
GLCD.DrawRect (82, 14, 26, 10, BLACK);
GLCD.CursorTo (14, 4); // sump tank
GLCD.print (“Led”);
GLCD.CursorTo (14, 5); // display tank
GLCD.print (“D/T”);
GLCD.CursorTo (14, 6); // led temp
GLCD.print (“S/T”);

// light boxes
GLCD.DrawRect (34, 25, 6, 6, BLACK); // cool white on/off
GLCD.DrawRect (34, 33, 6, 6, BLACK); // neutral white on/off
GLCD.DrawRect (34, 41, 6, 6, BLACK); // blue on/off
GLCD.DrawRect (34, 49, 6, 6, BLACK); // royal blue on/off

// power boxes
GLCD.DrawRect (73, 25, 6, 6, BLACK); // relay on/off
GLCD.DrawRect (73, 33, 6, 6, BLACK); // heater on/off
GLCD.DrawRect (73, 41, 6, 6, BLACK); // cooler on/off


void menuScroll() {
char key = keypad.getKey(); // uncomment this area if using matrix pad
// >>>>>
switch (key) // >>>>>>>
case ‘6’: // >>>>>>>>

/* if (button1.uniquePress()) */ // uncomment this line for use with buttons


case 1:
mainSelect1(); //go to main select 1
case 2:
mainSelect2(); //go to main select 2
case 3:
mainSelect3(); //go to main select 3
pressCount = 0;


void mainSelect1() {

GLCD.DrawRect (45, 5, 34, 12, WHITE);
GLCD.DrawRect (81, 13, 28, 12, WHITE);
GLCD.DrawRect (3, 5, 40, 12, BLACK);


void mainSelect2() {
GLCD.DrawRect (3, 5, 40, 12, WHITE);
GLCD.DrawRect (45, 5, 34, 12, BLACK);

void mainSelect3() {
GLCD.DrawRect (45, 5, 34, 12, WHITE);
GLCD.DrawRect (81, 13, 28, 12, BLACK);

void mainCancel() {

GLCD.DrawRect (3, 5, 40, 12, WHITE);
GLCD.DrawRect (45, 5, 34, 12, WHITE);
GLCD.DrawRect (81, 13, 28, 12, WHITE);

That code does something. You want it to do something. What are those two somethings, and how do they differ?

Sorry i was a bit vague it's the menuScroll function I'm trying to get to work I have a layout on the main screen and I want it so that when I press right button/6 on the matrix keypad it highlights each header the bit I can't get it to do is when I've highlighted it won't enter the submenu

There is a menu library that has support for glcd v3:

Might be worth taking a look at.

--- bill

I thought I downloaded glcd v 3 and was using it??

I thought I downloaded glcd v 3 and was using it??

M2tklib is an addon to GLCDv3. You need both, GLCDv3 and M2tklib. M2tklib has a lot of examples and tutorials to build menus.


Thank's for that I will go and have a play around with the examples.

looking at that library im getting so confused as to how it uses such little code to make keys scroll and stuff, how would i incorporate it into my code to make it scroll between the headers

code to make it scroll between the headers

Regarding M2tklib i probably would need a more detailed specification on what you excatly intend todo.
I tried to read your code, but i was not able to derive what menus you need.

However, i noticed, that you include keypad, which is not supported by M2tklib at the moment :frowning:

Instead M2tklib assumes two to six buttons directly attached to the Arduino Board.


ye i realised the keypad wont work which isnt to much of a loss i bought it to try it out while im waiting on some big buttons from china

i have one of them making it 5 so i can have up, down, left, right and enter/exit.

what im trying to do is create a controller.

i want the main screen to display some information and then be able to enter each section for more details and be able to change timers and stuff like that.

i want to be able to have this

menu 1 menu 2 lights on/off power on/off submenu 1 Submenu 2 light timer power timer

hope that makes a bit more sense

Indeed, nice buttons.

So, assuming you want to give a try to M2tklib, here is some example.

Let’s start with the first menu:
menu 1
lights on/off
submenu 1
light timer

First, i rename it from “menu 1” to “light”-menu, and i remove the submenu 1.
Then we have:
light menu
lights on/off
light timer

The basic M2tklib building block is a dialog window. The active dialog window is visible and data can be entered into it by the user. The light control dialog window for your first menu may look like this:

Light On:
< ok button >

This is the layout. Of course this is only my personal suggestion how to design this.
This layout has to be translated into m2tklib language. More specific, each layout component has to be translated into an m2tklib element. Elements are listed here:

The translation will have the following result:


Before I start to turn this into real code: M2tklib dialog windows interact with the remaing program by variables. This means we have to define variables, which will be filled by M2tklib with the data given by the user:

// the following variables contain the input values from the user
uint8_t light_timer = 0;
uint8_t light_on = 0;

Let us convert the first label into real code. The label text will be "Light On: "
The code is:

M2_LABEL(el_light_on_label, NULL, "Light On: ");

The first argument to the element is an element name (“el_light_on_label”). All elements in M2tklib start with their element name.
The second argument to the element is the format string. Also in M2tklib all elements have such a format string. I try to avoid the use of format strings as far as possible in this example, but in principle, a format string provides additional information for the element.
The third argument is the label text itself.
M2_LABEL is also discussed here:

Now I do not discuss all remaining elements, but just give the element translated code here:

M2_LABEL(el_light_on_label, NULL, "Light On: ");
M2_TOGGLE(el_light_on_toggle, NULL, &light_on);

M2_LABEL(el_light_timer_label, NULL, "Timer: ");
M2_U8NUM(el_light_timer_u8, "c2", 0, 99, &light_timer);

void light_dialog_ok(m2_el_fnarg_p fnarg) {
  /* do something with the values */
  /* ... */
  /* go back to main menu */
M2_BUTTON(el_light_ok, "f4", " ok ", light_dialog_ok);

Some notes:

  • M2_U8NUM expects a range (here from 0 to 99 as an example)
  • M2_U8NUM has a format option which limits the number of visible digits to 2
  • M2_BUTTON expects a callback function. This means, a procedure (light_dialog_ok) is called when the button is pressed.
  • Within a callback procedure you can change the complete dialog window by assigning a new dialog (root) elemet. This is el_top_controller_menu in our case.
  • M2_BUTTON option “f4” selects the so called highlighted font 0. This is done by placing a box around the text (see below).
  • Variables are provided as arguments to the related element, with a “&” in front of it
  • The format argument NULL means: No options, use default values.

How does M2tklib know the arrangement of elements? Well, we need to define it. For this reason we have so called container elements. One usefull container is the grid element. M2_GRID expects a list of elements and arranges them in a matrix style:

M2_LIST(list_light_dialog) = { 
    &el_light_on_label, &el_light_on_toggle, 
    &el_light_timer_label, &el_light_timer_u8,  
M2_GRIDLIST(el_light_grid, "c2", list_light_dialog);


  • M2_LIST ist NOT an element. It is a list
  • Elements are always refered by adding a “&” in front of it
  • Lists are always refered without a “&” in front of it
  • Format option “c2” defines two columns for the matrix

At last, we want to center the whole dialog window. This is done by M2_ALIGN. Unfortunately it requires a more complex format option, so this is given here without further explaination:

M2_ALIGN(el_top_light, "-1|1W64H64", &el_light_grid);

At the end, it will (more or less) look like this:

The actual style will depend on the underlaying graphics engine. Is it a character or graphics LCD? Which font is in use? Which cursor style has been selected? What is the shape of the toggle button?

One good thing on M2tklib is, that your code is portable. You can use the same code with a character display.

Finally let me discuss the main menu: Design could be this:

I already had introduced M2_BUTTON, but if you only want to switch to another dialog window, then M2_ROOT is more easier to use (it avoids the callback procedure):

M2_ROOT(list_controller_light, NULL, "Light", &el_top_light);
M2_ROOT(list_controller_power, NULL, "Power", &el_top_power);

// all menu lines are grouped by a vlist element
M2_LIST(list_controller_menu) = { &list_controller_light, &list_controller_power};
M2_VLIST(el_controller_menu_vlist, NULL, list_controller_menu);

// center the menu on the display
M2_ALIGN(el_top_controller_menu, "-1|1W64H64", &el_controller_menu_vlist);


  • Very similar to the previous dialog window…
  • Instead of M2_GRID, there is M2_VLIST which is a little bit simpler than M2_GRID: It arranges the buttons in a vertical list

I skip the power menu. I am sure you can design it by yourself.

I hope that this introduction was not too complicated and confusing, so if there are more questions, please let me know.


wow thats alot to take in, im trying to figure it all out but im having trouble working out how to put all that info into the compiler :astonished: is there not a library that talks in english :D

is there another library that is easier to work with?

can i not do the above ^^ without the m2tk lib?

does anybody know if its possible to display my pwm as a % on a lcd?

jbroadway: does anybody know if its possible to display my pwm as a % on a lcd?

Look at the map() function: to map the value, then display the new value any way you want.

--- bill

ok i really cant get my head around m2tk library, is there another way to do this??

also how would i go about making a changable time via the glcd
lighting time
start time 12:30
end time 18:45

how can i change them values so it displays it on screen but also processes it as the new time

Now the buttons have stopped working