LCD Buttons and Random Numbers

I am trying to get the select button on my lcd keypad shield to generate random numbers when pressed plz help. P.S the buttons are assigned to A0 and how can you use the analog pis vs degital pins (like how does it know wether you are using a analog pin vs a degital pin?

I am trying to get the select button on my lcd keypad shield to generate random numbers when pressed

So, you have some code you didn’t post, that some something that you didn’t explain, and that somehow differs from the mysterious behavior you expect.

plz help

Now, really. How can we?

how can you use the analog pis vs degital pins

Well, analog pin 2 is on one side of the board and digital pin 2 is on the other. It’s not hard to determine which pin you connected something to. It’s not hard to determine which pin to connect an analog device to and which pin to connect a digital device to.

tarbear123:
I am trying to get the select button on my lcd keypad shield to generate random numbers when pressed plz help. P.S the buttons are assigned to A0 and how can you use the analog pis vs degital pins (like how does it know wether you are using a analog pin vs a degital pin?

If the LCD keypad is the one I think it is then try this program

/* FILE:    ARD_LCD_HCARDU014_Buttons_Example.pde
   DATE:    02/07/12
   VERSION: 0.1

 This is a simple example of how to use the buttons included on the
 HobbyComponents Arduino LCD shield (HCARDU0014). The shield will work
 with the standard Arduino LiquidCrystal library.

 The buttons when pressed apply different offset voltages to the
 Arduino ADC0. Reading the ADC will give an ADC code in the following
 range depending on which button has been pressed:

     Between 0 & 49 = RIGHT button pressed.
     Between 50 & 194 = UP button pressed.
     Between 195 & 379 = DOWN button pressed.
     Between 380 & 555 = LEFT button pressed.
     Between 555 & 789 = SELECT button pressed.

     Above and ADC code of 790, assume no button has been pressed.

LCD Backlight considerations

By default the LiquidCrystal library does not attempt to control the backlight
and therefore the backlight will default to being permanently on. If you do not
need to change the state of the backlight you can ignore the following information:

When overriding the state of the backlight via DIO10 from your own code, you must
drive the pin in one of two modes:

Backlight   DIO10
OFF      OUTPUT/LOW
ON      INPUT

DO NOT configure DIO to be and OUTPUT/HIGH as this can cause excessive current
to be drawn from DIO10.

You may copy, alter and reuse this code in any way you like but please leave
reference to HobbyComponents.com in your comments if you redistribute this code.

THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
REASON WHATSOEVER.

*/



/* Include the standard LiquidCrystal library */
#include <LiquidCrystal.h>


/* DIO pin definitions */
#define LCD_DATA4 4         /* LCD data DIO pin 4 */
#define LCD_DATA5 5         /* LCD data DIO pin 5 */
#define LCD_DATA6 6         /* LCD data DIO pin 6 */
#define LCD_DATA7 7         /* LCD data DIO pin 7 */
#define LCD_RESET 8         /* LCD Reset DIO pin */
#define LCD_ENABLE 9        /* LCD Enable DIO pin */
#define LCD_BACKLIGHT 10    /*LCD backlight DIO pin */


/* Definitions for current button state */
typedef enum
{
  E_LCD_BTN_RIGHT,          /* LCD RIGHT button pressed */
  E_LCD_BTN_UP,             /* LCD UP button pressed */
  E_LCD_BTN_DOWN,           /* LCD DOWN button pressed */
  E_LCD_BTN_LEFT,           /* LCD LEFT button pressed */
  E_LCD_BTN_SELECT,         /* LCD SELECT button pressed */
  E_LCD_BTN_NONE,           /* LCD NONE button pressed */
}
teButtonState;


/* Initiliase the LiquidCrystal library with the correct DIO pins */
LiquidCrystal lcd(LCD_RESET, LCD_ENABLE, LCD_DATA4, LCD_DATA5, LCD_DATA6, LCD_DATA7);


void setup()
{
  /* Set the correct display size (16 character, 2 line display) */
  lcd.begin(16, 2);
}


/* Main program loop */
void loop()
{
  /* Put the LCD cursor on the first row and prompt the user to press a button */
  lcd.setCursor(0, 0);
  lcd.print("Press a button...");
  /* Keep checking for a button press */
  while (1)
  {
    /* Put the LCD cursor on the second row */
    lcd.setCursor(0, 1);
    /* Get the current state of the LCD buttons */
    switch (iGetLCDButtonState())
    {
      /* If the RIGHT button has been pressed then display this on the LCD */
      case E_LCD_BTN_RIGHT:
        lcd.print("RIGHT ");
        break;
      /* If the LEFT button has been pressed then display this on the LCD */
      case E_LCD_BTN_LEFT:
        lcd.print("LEFT   ");
        break;
      /* If the UP button has been pressed then display this on the LCD */
      case E_LCD_BTN_UP:
        lcd.print("UP    ");
        break;
      /* If the DOWN button has been pressed then display this on the LCD */
      case E_LCD_BTN_DOWN:
        lcd.print("DOWN  ");
        break;
      /* If the SELECT button has been pressed then display this on the LCD */
      case E_LCD_BTN_SELECT:
        lcd.print("SELECT");
        break;
      /* If no button has been pressed then display NONE on the LCD */
      case E_LCD_BTN_NONE:
        lcd.print("NONE  ");
        break;
    }
  }
}

/* Read the current state of the LCD buttons using the ADC */
int iGetLCDButtonState()
{
  int iADC_Value;
  int iADC_Button_State = E_LCD_BTN_NONE;
  iADC_Value = analogRead(0);      /* Read the ADC */
  /* If ADC reads above 1000 then assume no buttons have been pressed */
  if (iADC_Value > 1000)
  {
    iADC_Button_State = E_LCD_BTN_NONE;
  }
  else /* If it was below 1000 then a button is pressed... */
  {
    /* If ADC reads between 0 & 49, then the RIGHT button has been pressed */
    if (iADC_Value < 50)
    {
      iADC_Button_State = E_LCD_BTN_RIGHT;
    }
    else
    {
      /* If ADC reads between 50 & 194, then the UP button has been pressed */
      if (iADC_Value < 195)
      {
        iADC_Button_State = E_LCD_BTN_UP;
      }
      else
      {
        /* If ADC reads between 195 & 379, then the DOWN button has been pressed */
        if (iADC_Value < 380)
        {
          iADC_Button_State = E_LCD_BTN_DOWN;
        }
        else
        {
          /* If ADC reads between 380 & 555, then the LEFT button has been pressed */
          if (iADC_Value < 555)
          {
            iADC_Button_State = E_LCD_BTN_LEFT;
          }
          else
          {
            /* If ADC reads between 555 & 789, then the SELECT button has been pressed */
            if (iADC_Value < 790)
              iADC_Button_State = E_LCD_BTN_SELECT;
          }
        }
      }
    }
  }
  return iADC_Button_State;
}

It is an example of reading the buttons on the shield and reporting which one was pressed.

ok but i want to push the select button to roll diceOne and diceTwo, wait a till the button is pushed again to roll diceOne

ok but i want to push the select button to roll diceOne and diceTwo, wait a till the button is pushed again to roll diceOne

Where are you stuck ?

The iGetLCDButtonState() function in the code allows you to determine which, if any, button is currently pressed.

i'm trying to build a LCD dice to play catan with, using my LCD keypad shield on top of my arduino, the problem is that i want to be able to press select to roll the dice and then press select again to roll one die
and here's what i have so far

Select - 1
#include liquidCrystal lcd(8, 9, 4, 5, 6, 7)

int localkey = 1;
String keystring = "";

void setup() {
lcd.begin(16, 2):
lcd.clear():
lcd.setCursor(0, 0);
lcd.print(" roll to dice");

}

void loop() {
keypad.geyKey();

localkey = keypad.getkey();

if (local != SAMPLE_WAIT)
(
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("rolling dice")

if (diceOne + diceTwo == 7) {
lcd.clear();
lcd.setCursor(7, 0);
lcd.print("7");
lcd.setCursor(3, 1);
lcd.print("The pirate");

if (diceOne + diceTwo == 5, 4, 3, 2, 6 ) {
lcd.clear();
lcd.setCursor(7, 0);
lcd.print("5,4,3,2 6")
}
}
}

@tarbear123, please do not cross-post. Threads merged.

here's what i have so far

Which does not compile

One reason is this line

#include liquidCrystal lcd(8, 9, 4, 5, 6, 7)

Compare that with any of the LCD examples

Please post a link to the "LCD keypad shield" that you are using

 keypad.geyKey();

localkey = keypad.getkey();

if (local != SAMPLE_WAIT)

You appear to be trying to use a function from the Keypad library, which the shield does not use and which has not been #included, spelling it wrong, discarding the result even if it were possible for one to be returned, then testing a non existent variable for a value that has not been defined.

WOW !