Pages: [1]   Go Down
Author Topic: Using a 3x4 matrix keypad with I2C LCD  (Read 958 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello, I am working on a project and i am having problems coding to do what i want. I have got the LCD working and i have got the keypad working so when i hit the key "1" and "#" some LED's light up and it displays text on the LCD, and goes back to home when it is done. The issue I am having is that i don't want to have to hit the "#" key so the function doesn't run. I would like to be able to code it so when I press the "1" button it shows on the LCD "Turn on Red LED?" and then you can hit the "#" key to run the function. I would like to do this function just as a double check so i don't run a different function. Is this at all possible?
Thanks in advance
Gaven
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 613
Posts: 49270
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is this at all possible?
Yes.

Quote
Thanks in advance
You're welcome.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you would you know how to code this i have most of the code typed up i just need it to print something on the LCD when i hit the number i have in my loop to readKeypad() and the fuction readKeypad() is as follows

void readKeypad()
{
  char key = keypad.getKey();
  if (key != NO_KEY)
  {
    switch(key)
    {
    case '*':
      z=0;
      break;
    case '#':
      delay(100); // for extra debounce
      checkPIN();
      break;
    default:
      attemptC[z]=key;
      z++;
    }
  }
}

I would like to somehow put in that function if i type in a 1 and a 2 into the keypad the LCD prints "turn on red LED?" and i would have to hit the "#" button to turn on the red led function.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 626
Posts: 34118
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Read how to use this forum post at the start of this section.
Then post all your code correctly, then we will have the information needed to help you.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Alright i have read how to post and i will give it a shot. I'm sure there is a better way to code it but i found this the simplest way, any suggestions welcome.
Thank You
Code:
#include "Keypad.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x20,16,2);  // set the LCD address to 0x20 for a 16 chars and 2 line display

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] =
{
  {
    '1','2','3'    }
  ,
  {
    '4','5','6'    }
  ,
  {
    '7','8','9'    }
  ,
  {
    '*','0','#'    }
};
byte rowPins[ROWS] = {
  5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {
  8, 7, 6}; //connect to the column pinouts of the keypad

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

char RED[2]={
  '1','2'}; // our secret (!) number
char YELLOW[2]={
  '2','3'}; //YELLOW now
char BLUE[2]={
  '4','2'};
char attemptC[2]={
  0,0}; // used for comparison
char GREEN[2]={
  '1','3'};
int z=0;
int ledRed = 12;
int ledYellow = 11;
int ledGreen = 10;
int ledOrange = 13;
void setup()
{
  lcd.init();                      // initialize the lcd
  lcd.backlight();  //turns backlight on
  lcd.print("Pick Your Color");
  pinMode(ledRed, OUTPUT);
  pinMode(ledYellow, OUTPUT);
  pinMode(ledGreen, OUTPUT);
  pinMode(ledOrange, OUTPUT);
  incorrectPIN();
}
void optionOne(){
  lcd.clear();
  lcd.print("Super Epic!");
  digitalWrite(ledRed, LOW);
  digitalWrite(ledGreen, HIGH);
  delay(2000);
  incorrectPIN();
}

void GREENS(){
  lcd.clear();
  lcd.print("EPIC!");
  digitalWrite(ledRed, LOW);
  digitalWrite(ledOrange, HIGH);
  delay(2000);
  digitalWrite(ledOrange, LOW);
  incorrectPIN();
}

void correctPIN() // do this if correct PIN entered
{
  lcd.clear();
  lcd.print("Hip hip Hooray!");
  digitalWrite(ledRed, LOW);
  digitalWrite(ledYellow, HIGH);
  delay(3000);
  incorrectPIN();
}

void incorrectPIN() // do this if incorrect PIN entered
{
  lcd.clear();
  lcd.print("Pick Your Color");
  digitalWrite(ledGreen, LOW);
  digitalWrite(ledRed, HIGH);
  digitalWrite(ledYellow, LOW);
}

void optionTwo(){
  lcd.clear();
  lcd.print("It Works");
  digitalWrite(ledRed, LOW);
  for(int x=0; x<10; x++){
    digitalWrite(ledYellow, HIGH);
    delay(250);
    digitalWrite(ledYellow, LOW);
    delay(250);
  }
  incorrectPIN();
}

void checkPIN()
{
  if (attemptC[0]==RED[0]&&
  attemptC[1]==RED[1])
  {
    correctPIN();
  }

  else
  {
    incorrectPIN();
  }

  if (attemptC[0]==YELLOW[0]&&
  attemptC[1]==YELLOW[1])
  {
    optionOne();
  }

  else
  {
    incorrectPIN();
  }
 
    if (attemptC[0]==BLUE[0]&&
    attemptC[1]==BLUE[1])
  {
    optionTwo();
  }

  else
  {
    incorrectPIN();
  }
 
   if (attemptC[0]==GREEN[0]&&
   attemptC[1]==GREEN[1])
  {
    GREENS();
  }

  else
  {
    incorrectPIN();
  }
  for (int zz=0; zz<6; zz++) // wipe attempt
  {
    attemptC[zz]=0;
  }
}

void readKeypad()
{
  char key = keypad.getKey();
  if (key != NO_KEY)
  {
    switch(key)
    {
    case '*':
      z=0;
      break;
    case '#':
      delay(100); // for extra debounce
      checkPIN();
      break;
    default:
      attemptC[z]=key;
      z++;
    }
  }
}

void loop()
{
  readKeypad();
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 613
Posts: 49270
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
char RED[2]={
  '1','2'}; // our secret (!) number

void checkPIN()
{
  if (attemptC[0]==RED[0]&&
  attemptC[1]==RED[1])
  {
    correctPIN();
  }

  else
  {
    incorrectPIN();
  }

  if (attemptC[0]==YELLOW[0]&&
  attemptC[1]==YELLOW[1])
Two letter codes are not exactly secure. Think how ugly this is going to look when you start using 8 letter codes.

You could properly define RED:
char RED[] = "12345678";

Then, properly store the key in attemptC:
Code:
    default:
      attemptC[z] = key;
      z++;
      attemptC[z] = '\0';

Then, use strcmp() to do the comparison(s):
Code:
if(strcmp(RED, attemptC) == 0)
  // red match
else if(strcmp(YELLOW, attemptC) == 0)
  // yellow match
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Alright that would make sense but how would i setup for attemptC such as
Quote
char attemptC[8]={
  0,0,0,0,0,0,0,0};
and also how i would i display it on the LCD when i enter in the code such as if i typed "12345678" into the keypad it displays "Turn Red On?" before i actually press enter?
Thank You
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have figured out everything I needed to know thank you very much your idea has worked perfectly and made my code not so messy! Thanks Again.
Logged

Pages: [1]   Go Up
Jump to: