proper way of using U8GLIB

I am here again with a different problem!

Could someone please help me out here? I want to know the proper way of displaying a menu in GLCD using U8GLIB... The error I get is that the screen stays on the same text.. Not going to other menu...

what i want to achieve is that

1.) Display a welcome screen with 2 seconds delay
2.) Display a menu with options and wait until the user enters a value
3.) Go to the relevant option

Here is my code:

#include <Keypad.h>
#include "U8glib.h"
U8GLIB_ST7920_128X64_4X u8g(13, 12, 11);

const byte numRows= 4;
const byte numCols= 4;
char keymap[numRows][numCols]= {
                                 {'1', '2', '3', 'A'}, 
                                 {'4', '5', '6', 'B'}, 
                                 {'7', '8', '9', 'C'},
                                 {'*', '0', '#', 'D'}
                               };
byte rowPins[numRows] = {3,4,5,6};
byte colPins[numCols] = {7,8,9,10};
Keypad mykeypad = Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
char Password=1*2*3;

void setup()
{

    u8g.firstPage ();  
      do{
          welcome ();
        } while( u8g.nextPage () );
    delay(3000);
}

void welcome(void)
{
    u8g.setFont(u8g_font_fub17);
    u8g.setPrintPos(16, 25); 
    u8g.setColorIndex(1);
    u8g.drawBox(0,0,128,64);  
    u8g.setColorIndex(0);   
    u8g.print("Welcome!");
}

void authentication()
{
    char uauth;
    u8g.setFont(u8g_font_helvR08);
    u8g.setPrintPos(16, 25); 
    u8g.print("Please enter device");
    u8g.setPrintPos(20, 46); 
    u8g.print("PASSWORD");
    uauth = mykeypad.waitForKey();
    if (uauth == Password)
      {
           u8g.firstPage();  
           do{
               MainMenu();
             } while( u8g.nextPage() );
      }
    u8g.setFont(u8g_font_fub17);
    u8g.setPrintPos(16, 25); 
    u8g.print("Invalid Password");
}

void MainMenu(void)
{
    int MainOption;
    u8g.setFont(u8g_font_helvR08);
    u8g.setPrintPos(2, 25); 
    u8g.print("Choose the options below");
    u8g.setFont(u8g_font_fub17);
    u8g.setPrintPos(16, 25); 
    u8g.print(" 1. First Option");
    u8g.setPrintPos(16, 25); 
    u8g.print(" 2. Second Option");
    u8g.setPrintPos(16, 25); 
    u8g.print(" 3. Third Option");
    MainOption = mykeypad.getKey();
    if(MainOption == 1)
    {
        u8g.setFont(u8g_font_unifont);
        u8g.setPrintPos(64, 32); 
        u8g.print(MainOption);
    }
    else if(MainOption == 2)
    {
        u8g.setFont(u8g_font_unifont);
        u8g.setPrintPos(64, 32); 
        u8g.print(MainOption);      
    }
    else if(MainOption == 3)
    {
        u8g.setFont(u8g_font_unifont);
        u8g.setPrintPos(64, 32); 
        u8g.print(MainOption);      
    }
    else
    {
        u8g.setFont(u8g_font_unifont);
        u8g.setPrintPos(20, 26); 
        u8g.print("Invalid choice...");      
        u8g.setPrintPos(26, 48); 
        u8g.print("Try again!");      
    }   
}    


void loop(void)
{
    u8g.firstPage ();  
      do{
          authentication ();
        } while( u8g.nextPage () );
}

ERROR I GET :slightly_frowning_face: :cry: :cry: : Screen stays on Welcome! screen and never goes to other functions like authentication(); and MainMenu();

when i press any keys on the keypad, it either loops again or get irregular characters! And never displays a menu etc...

The normal program has to be slightly varied to display the output in U8GLIB

I badly need your help guys! Please help! Thanks!

Also attached the LCD setup for your reference!

Saturday_03_January_2015 11_49_16snap.jpg

This is because the change of spelling .

void authentication()
{
    char uauth;
    u8g.setFont(u8g_font_helvR08);
    u8g.setPrintPos(16, 25); 
    u8g.print("Please enter device");
    u8g.setPrintPos(20, 46); 
    u8g.print("PASSWORD");
    uauth = mykeypad.waitForKey();
    if (uauth == Password)
      {
           u8g.firstPage();  
           do{
               MainMenu();
             } while( u8g.nextPage() );
      }
    u8g.setFont(u8g_font_fub17);
    u8g.setPrintPos(16, 25); 
    u8g.print("Invalid Password");
}

you calling it as

 authentication ();

Look for yourself from the output screens!

auth.JPG

what problem you are facing Nw.??

you calling u8g.firstPage (); in loop instead of setup(); loop is while(1) condition it keep printing welcome.
In order to goto next stage you must see previous state of display.once key pressed you update screen just check how many time keys being pressed.

AMPS-N:
This is because the change of spelling .

void authentication()

{
    char uauth;
    u8g.setFont(u8g_font_helvR08);
    u8g.setPrintPos(16, 25);
    u8g.print("Please enter device");
    u8g.setPrintPos(20, 46);
    u8g.print("PASSWORD");
    uauth = mykeypad.waitForKey();
    if (uauth == Password)
      {
          u8g.firstPage(); 
          do{
              MainMenu();
            } while( u8g.nextPage() );
      }
    u8g.setFont(u8g_font_fub17);
    u8g.setPrintPos(16, 25);
    u8g.print("Invalid Password");
}



you calling it as


authentication ();

Did you specify about authenticate(); different from authenticate() the difference being a semicolon....?

I am really exhausted with this 20 days old problem.. Seeing it no progress!

Can someone be kind enough to give me the corrected program from my post #1 :frowning: :frowning: :frowning:

I don't have library to edit. copy below code. You can only give me answer here since you hardware with you

Try this code . let me know what happens.

#include <Keypad.h>
#include "U8glib.h"
U8GLIB_ST7920_128X64_4X u8g(13, 12, 11);

const byte numRows= 4;
const byte numCols= 4;
char keymap[numRows][numCols]= {
                                 {'1', '2', '3', 'A'}, 
                                 {'4', '5', '6', 'B'}, 
                                 {'7', '8', '9', 'C'},
                                 {'*', '0', '#', 'D'}
                               };
byte rowPins[numRows] = {3,4,5,6};
byte colPins[numCols] = {7,8,9,10};
Keypad mykeypad = Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
char Password=1*2*3;

void setup()
{

    u8g.firstPage ();  
    delay(3000);
}

void welcome(void)
{
    u8g.setFont(u8g_font_fub17);
    u8g.setPrintPos(16, 25); 
    u8g.setColorIndex(1);
    u8g.drawBox(0,0,128,64);  
    u8g.setColorIndex(0);   
    u8g.print("Welcome!");
}

void authentication()
{
    char uauth;
    u8g.setFont(u8g_font_helvR08);
    u8g.setPrintPos(16, 25); 
    u8g.print("Please enter device");
    u8g.setPrintPos(20, 46); 
    u8g.print("PASSWORD");
    uauth = mykeypad.waitForKey();
    if (uauth == Password)
      {
           u8g.firstPage();  
           do{
               MainMenu();
             } while( u8g.nextPage() );
      }
    u8g.setFont(u8g_font_fub17);
    u8g.setPrintPos(16, 25); 
    u8g.print("Invalid Password");
}

void MainMenu(void)
{
    int MainOption;
    u8g.setFont(u8g_font_helvR08);
    u8g.setPrintPos(2, 25); 
    u8g.print("Choose the options below");
    u8g.setFont(u8g_font_fub17);
    u8g.setPrintPos(16, 25); 
    u8g.print(" 1. First Option");
    u8g.setPrintPos(16, 25); 
    u8g.print(" 2. Second Option");
    u8g.setPrintPos(16, 25); 
    u8g.print(" 3. Third Option");
    MainOption = mykeypad.getKey();
    if(MainOption == 1)
    {
        u8g.setFont(u8g_font_unifont);
        u8g.setPrintPos(64, 32); 
        u8g.print(MainOption);
    }
    else if(MainOption == 2)
    {
        u8g.setFont(u8g_font_unifont);
        u8g.setPrintPos(64, 32); 
        u8g.print(MainOption);      
    }
    else if(MainOption == 3)
    {
        u8g.setFont(u8g_font_unifont);
        u8g.setPrintPos(64, 32); 
        u8g.print(MainOption);      
    }
    else
    {
        u8g.setFont(u8g_font_unifont);
        u8g.setPrintPos(20, 26); 
        u8g.print("Invalid choice...");      
        u8g.setPrintPos(26, 48); 
        u8g.print("Try again!");      
    }   
}    


void loop(void)
{
    
      do{
          authentication ();
        } while( u8g.nextPage () );
}

I doubt in this part of code. Here it wont allow you to goto next page

void loop(void)
{
    
      do{
          authentication ();
        } while( u8g.nextPage () );
}

Ask whatever you want! Just kindly give me the correct code!

I have attached the libraries below

keypad.zip (18.5 KB)

u8glib_arduino_v1.17.zip (1.08 MB)

Since i dont have hardware with me. I can't do any thing. When i start debugging it i found problem in this part. Without your reply i cant do any thing
error is this part of code.

uauth = mykeypad.waitForKey();

Have you upload code i shared. Let me know whats happening.

#include <Keypad.h>
#include "U8glib.h"
U8GLIB_ST7920_128X64_4X u8g(13, 12, 11);

const byte numRows= 4;
const byte numCols= 4;
char keymap[numRows][numCols]= {
                                 {'1', '2', '3', 'A'}, 
                                 {'4', '5', '6', 'B'}, 
                                 {'7', '8', '9', 'C'},
                                 {'*', '0', '#', 'D'}
                               };
byte rowPins[numRows] = {3,4,5,6};
byte colPins[numCols] = {7,8,9,10};
Keypad mykeypad = Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
char Password='4';

void setup()
{
Serial.begin(9600);
    u8g.firstPage ();  
    delay(3000);
}

void welcome(void)
{
    u8g.setFont(u8g_font_fub17);
    u8g.setPrintPos(16, 25); 
    u8g.setColorIndex(1);
    u8g.drawBox(0,0,128,64);  
    u8g.setColorIndex(0);   
    u8g.print("Welcome!");
    Serial.println("Welcome");
}

void authentication()
{
    char uauth;
    u8g.setFont(u8g_font_helvR08);
    u8g.setPrintPos(16, 25); 
    u8g.print("Please enter device");
    Serial.println("Please enter device");
    u8g.setPrintPos(20, 46);
   Serial.println("password"); 
    u8g.print("PASSWORD");
    uauth = mykeypad.getKey();
    Serial.print(uauth);
    if (uauth == Password)
      {
           u8g.firstPage();  
           do{
               MainMenu();
             } while( u8g.nextPage() );
      }
    u8g.setFont(u8g_font_fub17);
    u8g.setPrintPos(16, 25); 
    u8g.print("Invalid Password");
    Serial.println("invalid password");
}

void MainMenu(void)
{
  
  Serial.println("main menu:");
    int MainOption;
    u8g.setFont(u8g_font_helvR08);
    u8g.setPrintPos(2, 25); 
    u8g.print("Choose the options below");
    u8g.setFont(u8g_font_fub17);
    u8g.setPrintPos(16, 25); 
    u8g.print(" 1. First Option");
    u8g.setPrintPos(16, 25); 
    u8g.print(" 2. Second Option");
    u8g.setPrintPos(16, 25); 
    u8g.print(" 3. Third Option");
    MainOption = mykeypad.getKey();
    if(MainOption == 1)
    {
        u8g.setFont(u8g_font_unifont);
        u8g.setPrintPos(64, 32); 
        u8g.print(MainOption);
        Serial.print("option1");
    }
    else if(MainOption == 2)
    {
        u8g.setFont(u8g_font_unifont);
        u8g.setPrintPos(64, 32); 
        u8g.print(MainOption);  
     Serial.print("option2");    
    }
    else if(MainOption == 3)
    {
        u8g.setFont(u8g_font_unifont);
        u8g.setPrintPos(64, 32); 
        u8g.print(MainOption);    
       Serial.print("option3");  
    }
    else
    {
        u8g.setFont(u8g_font_unifont);
        u8g.setPrintPos(20, 26); 
        u8g.print("Invalid choice...");      
        u8g.setPrintPos(26, 48); 
        u8g.print("Try again!");  
     Serial.print("Try again");    
    }   
}    


void loop(void)
{
    
      do{
          authentication ();
        } while( u8g.nextPage () );
}

Please help us a bit because we don't have the same hardware as you.
Put some Serial.prints() in the code so that you know which parts are being executed and what the value of pertinent variables are at that point.

For instance, does the program ever get out of the setup() function ?
What do the firstPage and nextPage functions do and what values do they return ?

As you said, I have inserted the Serial.println commands as if to spot the errors!

From the code below

#include <Keypad.h>
#include "U8glib.h"
U8GLIB_ST7920_128X64_4X u8g(13, 12, 11);

const byte numRows= 4;
const byte numCols= 4;
char keymap[numRows][numCols]= {
                                 {'1', '2', '3', 'A'}, 
                                 {'4', '5', '6', 'B'}, 
                                 {'7', '8', '9', 'C'},
                                 {'*', '0', '#', 'D'}
                               };
byte rowPins[numRows] = {3,4,5,6};
byte colPins[numCols] = {7,8,9,10};
Keypad mykeypad = Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
char Password=1;

void setup()
{
  Serial.begin(9600);

    u8g.firstPage ();  
      do{
          welcome ();
        } while( u8g.nextPage () );
    delay(3000);
}

void welcome(void)
{
    u8g.setFont(u8g_font_fub17);
    u8g.setPrintPos(16, 25); 
    u8g.setColorIndex(1);
    u8g.drawBox(0,0,128,64);  
    u8g.setColorIndex(0);   
    Serial.println("Welcome is fine");
}

void authentication()
{
    char uauth;
    u8g.setFont(u8g_font_helvR08);
    u8g.setPrintPos(16, 25); 
    Serial.println("Authentication intro is fine");
    u8g.setPrintPos(20, 46); 
   // u8g.print("PASSWORD");
    uauth = mykeypad.waitForKey();
    if (uauth == Password)
      {
           u8g.firstPage();  
           do{
               MainMenu();
             } while( u8g.nextPage() );
      }
    u8g.setFont(u8g_font_fub17);
    u8g.setPrintPos(16, 25); 
    u8g.print("Invalid Password");
}

void MainMenu(void)
{
    int MainOption;
    u8g.setFont(u8g_font_helvR08);
    u8g.setPrintPos(2, 25); 
    Serial.println("Main Menu is fine");
    u8g.print("Choose the options below");
    u8g.setFont(u8g_font_fub17);
    u8g.setPrintPos(16, 25); 
    u8g.print(" 1. First Option");
    u8g.setPrintPos(16, 25); 
    u8g.print(" 2. Second Option");
    u8g.setPrintPos(16, 25); 
    u8g.print(" 3. Third Option");
    MainOption = mykeypad.getKey();
    if(MainOption == 1)
    {
        u8g.setFont(u8g_font_unifont);
        u8g.setPrintPos(64, 32); 
        u8g.print(MainOption);
    }
    else if(MainOption == 2)
    {
        u8g.setFont(u8g_font_unifont);
        u8g.setPrintPos(64, 32); 
        u8g.print(MainOption);      
    }
    else if(MainOption == 3)
    {
        u8g.setFont(u8g_font_unifont);
        u8g.setPrintPos(64, 32); 
        u8g.print(MainOption);      
    }
    else
    {
        u8g.setFont(u8g_font_unifont);
        u8g.setPrintPos(20, 26); 
        u8g.print("Invalid choice...");      
        u8g.setPrintPos(26, 48); 
        u8g.print("Try again!");      
    }   
}    


void loop(void)
{
    u8g.firstPage ();  
      do{
          authentication ();
        } while( u8g.nextPage () );
}

Here is the output i got( Image attached! )

Untitled.jpg

char Password=1;

Later in the program

    uauth = mykeypad.waitForKey();
    if (uauth == Password)///
      {
           u8g.firstPage();  
           do{
               MainMenu();
             } while( u8g.nextPage() );
      }

Which character that you can type on the keypad will match the password ?

Does the code ever pass the
if (uauth == Password)test ? Another Serial.print() perhaps ?

The concept is that wait for user to enter password
If the user enters then it checks whether the declared Password and uauth are equal and then executes the block of statements...

Actually at beginning I assigned

Password=123;

Even doesn't work then how am i suppose to check the output for 123 ??? Also, how to make program wait till user enters 5 characters and restrict user from entering more than the allowed length?

Try printing your Password variable. What do you see ?

This page may help you to see what you are doing wrong when you use

char password = 1;

char data type

irfanece:
The concept is that wait for user to enter password
If the user enters then it checks whether the declared Password and uauth are equal and then executes the block of statements...

Actually at beginning I assigned

Password=123;

Even doesn't work then how am i suppose to check the output for 123 ??? Also, how to make program wait till user enters 5 characters and restrict user from entering more than the allowed length?

if you check syntax functinality. so i replaced with getkey() function. when you know which key pressed you must see in serial print printing same key.

http://playground.arduino.cc/code/Keypad

char waitForKey()

This function will wait forever until someone presses a key. Warning: It blocks all other code until a key is pressed. That means no blinking LED's, no LCD screen updates, no nothing with the exception of interrupt routines.

char getKey()

Returns the key that is pressed, if any. This function is non-blocking.

AMPS-N:

char waitForKey()

This function will wait forever until someone presses a key. Warning: It blocks all other code until a key is pressed. That means no blinking LED's, no LCD screen updates, no nothing with the exception of interrupt routines.

char getKey()

Returns the key that is pressed, if any. This function is non-blocking.

That's what my objective is! I want the program to wait until the user enters a value............!

Can you give me an example program that waits until the user enters a value?

thats reason you cant get output. If key being pressed you should see the uauth value being update in serial monitor or in u8GLIB

currently you might getting on U8GLIB screen u8g.print("PASSWORD"); password & expecting some input.

When you upload the code ; what difference you find U8GLIB screen & serial screen you need to tell us.

both should give same result.

if key function

char waitForKey(); function called. check whether ur screen get your character or not. I think it will not get character

just replace char waitForKey() with char getKey() , this time you find the character,Now only concern will be receive char button pressed; stored it in buffer; compare with password string.

Do some reading about ASCII codes, characters, and character strings. You are missing some key concepts.

irfanece:
Password=123;

Even doesn't work then how am i suppose to check the output for 123 ???

A variable of type char (like password) can hold an 8-bit value. This can be a number from 0 to 255, or it can be an ASCII character (a convention that assigns a meaning to each value, so that alphabetic characters can be represented. For example, the letter 'A' is represented by the number 65.)

In programming, we represent numbers by just typing them in, like this:

char Password = 1;

And we represent ASCII characters by putting them in single quotes, like this:

char Password = '1';

The first one stores the numerical value 1 in Password, while the second one stores the numerical value 49 (the ASCII code for the character '1'. Now, your keypad matrix is defined in terms of ASCII characters (because that definition uses single quotes) but your assignment to Password uses a straight numerical value. When you press the '1' key, the keypad code returns 49. You then compare it to 1, which does not match: 1 does not equal 49.

Then, you tried Password = 123. Since there are no quotes there, this is a numerical expression, where the * character is multiplication. You are multiplying 1, 2, and 3 together, which results in 6, which is what gets stored in Password. (That's the numerical value 6, not the ASCII character 6, which has a numerical value of 54.)

If you want to make Password be the character sequence 123, you need to define it as a string or String (look it up in the reference, there is a difference between the two - note the capitalization, which is significant. String can be a bit easier to use, string is more efficient and uses less memory. From here out, I'll use the term string to refer to either String or string. Strings are defined by using double quotes (single quotes for single characters, double quotes for strings. Strings can also be zero or one characters long, you still use double quotes for them.) So your definition could be:

String Password = "1*2*3";  // A String
char Password[] = "1*2*3";  // A string

With that done, you need to make your code loop through multiple calls to read the keypad, adding each entered character to a temporary string. Then, when you have enough characters, compare it to Password to see if it matches. This a much more complicated construct than just reading and comparing a single character. Discussion of that should probably wait until you understand these concepts of char, strings, and ASCII encoding. Do some research until you get the moment when you say "a-ha, I see what he's saying!" Then we can explore the next steps.

These are some fundamental concepts that will apply to just about any program. They are not some arcane esoterica, so it makes sense to really understand it. That knowledge will serve you well.

I have taken both of your considerations! Remember, it's not about usual Arduino! The U8GLIB functions are of different syntax from the Arduino LiquidCrystal library functions!

Want to know a tricky part in U8GLIB?

{
  u8g.firstPage();  
  do{
    draw();
  } while( u8g.nextPage() );

We need the do-while loop to clear the screen in my display!

I am working really hard exploring this display!
It's not about waitforkey() since the screen is getting blank.... There is something related to U8GLIB itself and I will let you know this!!! Thanks :slight_smile:

irfanece:
It's not about waitforkey() since the screen is getting blank.... There is something related to U8GLIB itself and I will let you know this!!! Thanks :slight_smile:

OK. Just go ahead and believe that, if you you really want to get frustrated...

But believe me, this part, which has absolutely nothing to do with U8GLIB, will NOT work until you understand what I'm saying in my previous post:

   uauth = mykeypad.waitForKey();
    if (uauth == Password)

And that's especially true if you ever want the password to be more than a single digit. "Password = 123;" will never work as you intend.

Nothing you can do with U8GLIB will change that.

PS: I just noticed your tag line in your profile. Pardon me for being blunt, but I cannot fathom how you can be a final year ECE student and not know the difference between numbers, characters, and strings. That's freshman level material. With such a rookie mistake as that, I assumed this was your first-ever program! :o