Go Down

Topic: i need some help in g lcd with a keypad 4x4 (Read 1 time) previous topic - next topic

mostafa_zied

i have a graphical lcd and i want to connect it with keypad
i use this library "u8glib" for the graphical lcd and it is work correctly
but when i interface it with a (4*4) keypad
it not work correctly
it delay too much !!!it's need to press 8 times to show the number in the g. lcd


HazardsMind

My GitHub:
https://github.com/AndrewMascolo?tab=repositories

mostafa_zied

Code: [Select]

#include "U8glib.h"
#include <Keypad.h>
U8GLIB_T6963_240X128 u8g(49, 48, 47, 46, 45, 44, 43, 42, 35, 34, 37, 36, 16);
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'1','2','3','a'},
  {'4','5','6','b'},
  {'7','8','9','c'},
  {'#','0','*','d'}
};

byte rowPins[ROWS] = {A3, A2, A1, A0}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {A8, A7, A5, A4}; //connect to the column pinouts of the keypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
//------------------------------------------------------------------------------------
int val; // Y value (Read from the Analog input)
int xPos = 0; // X position
int prevXPos = 0; // Previous x position
int scanVal[128]; // Array to hold the graphs values as it scans.
int xDraw; // Used to
int p =0;
int oldvalue =0 ;
int newvalue =0 ;
int a =0;
int b =0;
int c =0;
int t =0;
int i =0;
int D =0;
int h =0;
int drflowrate = 0 ;
int timesolenoid = 0 ;
const int buttonPin1 = 9;
const int buttonPin2 = 8;
int vaul1;
int vaul2;
//--------------------------------------------

void drawText(void) {
 
  val = analogRead(A0);// Poll ADC
  val = map(val,0,1023,63,0);// Scale things down to fit the vertical pixel count of the LCD.

  scanVal[xPos] = val;// Write the Y value to the appropriate element (X value).

  u8g.setPrintPos(100, 100);// Set Cursor.
  u8g.print("Y");
  p = ((val-0.2)/0.09);
  u8g.print(p);// Print out the Y value.
  u8g.print("kp");

  u8g.setPrintPos(20, 100);// Set Cursor.
  u8g.print("X");
  u8g.print(xPos);// Print out the X value.
}

//--------------------------------------------

void drawScan(void) {// This function cycles 128 times per update to draw the full scan of the line.
  val = scanVal[xDraw];// Pull the Y value from the appropriate element.
  newvalue = val;
  u8g.drawLine(xDraw,oldvalue,xDraw+1,newvalue);// Set a pixel.
  xDraw++;// Increment the column position (X value).
  oldvalue = newvalue ;
}// End of scan

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

void setup(void)
{
  u8g.setRot180();// Flip screen
  u8g.setFont(u8g_font_6x10r);// Set Font
  u8g.setPrintPos(1, 1);// Set Cursor.
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
 
}

//--------------------------------------------------------------------
void draw ()
{                vaul1 = digitalRead(buttonPin1);
                 vaul2 = digitalRead(buttonPin2);
                 u8g.setPrintPos(1, 10);// Set Cursor.
                 u8g.print("pleas choose the mode from the following");
                 u8g.setPrintPos(1, 30);// Set Cursor.
                 u8g.print("press 'f1'for 'CMV' ");
                 u8g.setPrintPos(1, 50);// Set Cursor.
                 u8g.print("press 'f2' for 'SIMV'");
                 u8g.setPrintPos(1, 70);// Set Cursor.
                 u8g.print("press'f3' for 'cpap'");
                 char key = customKeypad.waitForKey();
                 if (key !=NO_KEY)
                 {
                 if (key == a)
                     {
                        u8g.setPrintPos(130, 10);// Set Cursor.
                        u8g.print("the mode selected is CMV ");
                         
                                 if (i==0)
                                     {
                                        char keym = customKeypad.waitForKey();
                                        if (keym !=NO_KEY)
                                              {
                                               u8g.setPrintPos(130, 30);// Set Cursor.
                                               u8g.print(keym);
                                               const char keya [2]={keym , '\n'};
                                               a = atoi (keya)*1000;
                                               i=1;
                                               delay(1000);
                                               
                                            }
                                    }
                                 if (i==1)
                                     {
                                        char keyn = customKeypad.waitForKey();
                                        if (keyn !=NO_KEY)
                                          {
                                            u8g.setPrintPos(134,30);// Set Cursor.
                                            u8g.print(keyn);
                                            const char keyb [2] = {keyn , '\n'};
                                            b = atoi(keyb)*100;
                                            i=2;
                                            delay(1000);
                                           
                                          }
                                     }
                                if (i==2)
                                    {
                                      char keyz = customKeypad.waitForKey();
                                      if (keyz !=NO_KEY)
                                        {
                                          u8g.setPrintPos(138,30);// Set Cursor.
                                          u8g.print(keyz);
                                          const char keyc[]={keyz ,'\n'};
                                          c =atoi(keyc)*10;
                                          i=3;
                                          delay(1000);
                                         
                                        }
                                    }

                                 if (i==3)
                                     {
                                      char keyH = customKeypad.waitForKey();
                                      if (keyH !=NO_KEY)
                                         {
                                           u8g.setPrintPos(142,30);// Set Cursor.
                                           u8g.print(keyH);
                                           const char keyh[]={keyH ,'\n'};
                                           int h =atoi(keyh);
                                           i=4;
                                           delay(1000);
                                           
                                         }
                                     }
                                  if (i == 4)
                                      {
                                        drflowrate = a+c+b+h ;
                                        timesolenoid = (drflowrate * 60)/10000 ;
                                        u8g.setPrintPos(150, 100);// Set Cursor.
                                        u8g.print("drflowrate");
                                        u8g.print(drflowrate);// Print out the number of the flowrate value.
                                        u8g.print("timesolenoid");
                                        u8g.setPrintPos(150, 100);// Set Cursor.
                                        i=0;
                                       delay (3000);
                 
                                     }
             
                               }
                 }
               
}
//-----------------------------------------------------------
void loop(void) {
   u8g.firstPage();
   do
              {
               draw();
               
               } while( u8g.nextPage() );

}

mostafa_zied


HazardsMind

1) Use Ctrl + T to Auto Format your code!

2) What Arduino are you using that has 9 analog inputs? "byte colPins[COLS] = {A8, A7, A5, A4}; "

3) int p =0; Why are making P an int, but you are using floats here, "p = ((val-0.2)/0.09);" ???

4) I would condense these, unless you want to show it like this
Quote
val = map(val,0,1023,63,0);// Scale things down to fit the vertical pixel count of the LCD.
  scanVal[xPos] = val;// Write the Y value to the appropriate element (X value).


5) Problem here, if (key == a), it should be if (key == 'a') it needs single quotes ' '  Do the same for the rest of them.

6) Take out the delays
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

mostafa_zied

first thanx for your replies

i upgrade the code as you say
and i used the serial monitor to debug the values and the cod it's work correctly but without any show in the glcd
i check the connection it's okey and i use a default program to show a "hallow world "  it's work!!!
but with this code no shows in the glcd  :~  :~  :smiley-sad-blue:
Code: [Select]

#include "U8glib.h"
#include <Keypad.h>
U8GLIB_T6963_240X128 u8g(49, 48, 47, 46, 45, 44, 43, 42, 35, 34, 37, 36, 16);
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {
    '1','2','3','a'      }
  ,
  {
    '4','5','6','b'      }
  ,
  {
    '7','8','9','c'      }
  ,
  {
    '#','0','*','d'      }
};

byte rowPins[ROWS] = {
  A0, A1, A2, A3}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {
  A4, A5, A6, A7}; //connect to the column pinouts of the keypad

//------------------------------------------------------------------------------------
int val; // Y value (Read from the Analog input)
int xPos = 0; // X position
int prevXPos = 0; // Previous x position
float p ;
int scanVal[128]; // Array to hold the graphs values as it scans.
int xDraw; // Used to
int oldvalue =0 ;
int newvalue =0 ;
int a =0;
int b =0;
int c =0;
int t =0;
int i =0;
int D =0;
int h =0;
int drflowrate = 0 ;
int timesolenoid = 0 ;
//--------------------------------------------

void drawText(void) {

  val = analogRead(A0);// Poll ADC
  val = map(val,0,1023,63,0);// Scale things down to fit the vertical pixel count of the LCD.

  scanVal[xPos] = val;// Write the Y value to the appropriate element (X value).

  u8g.setPrintPos(100, 100);// Set Cursor.
  u8g.print("Y");
  p = ((val-0.2)/0.09);
  u8g.print(p);// Print out the Y value.
  u8g.print("kp");

  u8g.setPrintPos(20, 100);// Set Cursor.
  u8g.print("X");
  u8g.print(xPos);// Print out the X value.
}

//--------------------------------------------

void drawScan(void) {// This function cycles 128 times per update to draw the full scan of the line.
  val = scanVal[xDraw];// Pull the Y value from the appropriate element.
  newvalue = val;
  u8g.drawLine(xDraw,oldvalue,xDraw+1,newvalue);// Set a pixel.
  xDraw++;// Increment the column position (X value).
  oldvalue = newvalue ;
}// End of scan

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

void setup(void)
{
  if ( u8g.getMode() == U8G_MODE_R3G3B2 )
    u8g.setColorIndex(255);     // white
  else if ( u8g.getMode() == U8G_MODE_GRAY2BIT )
    u8g.setColorIndex(3);         // max intensity
  else if ( u8g.getMode() == U8G_MODE_BW )
    u8g.setColorIndex(1);         // pixel on
  u8g.setRot180();// Flip screen
  Serial.begin(9600);
}

//--------------------------------------------------------------------
void draw ()
{           
  u8g.setFont(u8g_font_unifont);
  u8g.drawStr( 1, 10, "pleas choose the mode ");
  u8g.drawStr( 1, 30, "press 'f1'for 'CMV' ");
  u8g.drawStr( 1, 50, "press 'f2' for 'SIMV' ");
  u8g.drawStr( 1, 70, "press 'f2' for 'SIMV' ");
  Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
  int mod = customKeypad.waitForKey();
  if (mod !=NO_KEY)
  {
    Serial.println("key");
    if (mod == 'a')
    {
      Serial.println("helow");
      u8g.drawStr( 1, 1, "                                                                             ");
      u8g.drawStr( 10, 10, "the mode selected is CMV ");
      Serial.println("the mode selected is CMV");

      if (i==0)
      {
        char keym = customKeypad.waitForKey();
        if (keym !=NO_KEY)
        {
          u8g.setPrintPos(130, 30);
          u8g.print(keym);
          const char keya [2]={
            keym , '\n'                              };
          a = atoi (keya)*1000;
          Serial.println(keym);
          i=1;

        }
      }
      if (i==1)
      {
        char keyn = customKeypad.waitForKey();
        if (keyn !=NO_KEY)
        {
          u8g.setPrintPos(130, 34);
          u8g.print(keyn);
          const char keyb [2] = {
            keyn , '\n'                              };
          b = atoi(keyb)*100;
          Serial.println(keyn);
          i=2;


        }
      }
      if (i==2)
      {
        char keyz = customKeypad.waitForKey();
        if (keyz !=NO_KEY)
        {
          u8g.setPrintPos(130, 38);
          u8g.print(keyz);
          const char keyc[]={
            keyz ,'\n'                              };
          c =atoi(keyc)*10;
          Serial.println(keyz);
          i=3;


        }
      }

      if (i==3)
      {
        char keyH = customKeypad.waitForKey();
        if (keyH !=NO_KEY)
        {
          u8g.setPrintPos(130, 42);
          u8g.print(keyH);
          const char keyh[]={
            keyH ,'\n'                              };
          h =atoi(keyh);
          Serial.println(h);
          i=4;


        }
      }
      if (i == 4)
      {
        drflowrate = a+c+b+h ;
        Serial.println(drflowrate);
        timesolenoid = (drflowrate * 60)/10000 ;
        u8g.drawStr( 100, 100, "drflowrate");
        u8g.setPrintPos(150, 114);
        u8g.print(drflowrate);
        i=0;
      }

    }
  }

}
//-----------------------------------------------------------
void loop(void) {
  u8g.firstPage();
  do
  {
    draw();

  }
  while( u8g.nextPage() );

}




mostafa_zied


HazardsMind

#7
May 23, 2013, 06:37 pm Last Edit: May 23, 2013, 06:41 pm by HazardsMind Reason: 1
int mod = customKeypad.waitForKey(); Needs to be char, not int.

What does the screen show, do you see anything at all?
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

mostafa_zied

the screen not refresh the show to the new data until i press more than 4 times in the keypad and when it show it's show a part from it it's like " THE MODE" it's show the upper part of the litters and the lower part not appear
where is the problem ???
then i need to know how i can  the screen when i need ??

HazardsMind

Quote
u8g.drawStr( 1, 1, "                                                                             ");

Something like this should never be used to clear a line, unless you know how much to clear. This is TOO much white space and it will cause many visual issues.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

HazardsMind

Here are two functions I'm going to see if I can get put into the new LCD library that I have. You can adapt these to fit to your library.

Code: [Select]
void ClearSect(uint8_t row, uint8_t start, uint8_t stop)
{
  for(start; start !=  stop; start++)
  {
    lcd.setCursor(start, row);
    lcd.print(' ');
  }
}

void ClearLine(uint8_t row, uint8_t size)
{
  for(uint8_t start = 0; start < size; start++)
  {
    lcd.setCursor(start, row);
    lcd.print(' ');
  }
}
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

olikraus

The basic problem of your code is this: The draw() procedure does not repeat exactly the same code. Instead the graphics commands depend on the result of the key events. This will never work.

Please have a look at http://code.google.com/p/u8glib/wiki/tmenu. The suggested solution is to separate draw() and keyboard polling (update() procedure in the referenced tutorial).

This should be done once in the setup:
Code: [Select]
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
 

"mod" should be global and the check for the key should be in an additional procedure:

Code: [Select]
mod = customKeypad.waitForKey();

Oliver

mostafa_zied

sadly nothings works at all
so i used two arduino kits ; one for glcd and the other thing is for keypad .
thanx for your help
but i still need to know >>>>>why ?????? :( :( :( :( :( :(

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy