Task: 1 Write A Sketch To Interface Arduino With A 3 X 4 Matrix Keypad. The Display of The Pressed Key Should Be Displayed On The LCD. Code

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Task: 1

Write a sketch to interface Arduino with a 3 x 4 Matrix Keypad. The display of the
pressed key should be displayed on the LCD.
Code:
#include <LiquidCrystal.h>

#include <Keypad.h>

LiquidCrystal lcd(12, 11, 21, 20, 19, 18);

const byte ROWS = 4;

const byte COLS = 3;

char keys[ROWS][COLS] =

{'1','2','3'},

{'4','5','6'},

{'7','8','9'},

{'*','0','#'}

};

byte rowPins[ROWS] = {2,7,6,4};

byte colPins[COLS] = {3, 1, 5};

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

void setup()

Serial3.begin(9600);

lcd.begin(16, 2);

void loop()

char key = keypad.getKey();

if (key)

{
Serial3.println(key);

lcd.print(key);

}
Circuit:

Task:2
Write a sketch that works as security keypad lock. Set any password. If the input
password matches with the set password, Green LED should glow, otherwise Red
LED will glow.
Code:
#include <Keypad.h>

char* secretCode = "1234";

int position = 0;

const byte rows = 4;

const byte cols = 3;

char keys[rows][cols] =

{
{'1','2','3'},

{'4','5','6'},

{'7','8','9'},

{'*','0','#'}

};

byte rowPins[rows] = {2, 7, 6, 4};

byte colPins[cols] = {3, 1, 5};

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);

int redPin = 12;

int greenPin = 9;

void setup()

pinMode(redPin, OUTPUT);

pinMode(greenPin, OUTPUT);

setLocked(true);

void loop()

char key = keypad.getKey();

if (key == '*' || key == '#') // Reset Buttons.

position = 0;

setLocked(true);

if (key == secretCode[position])

position ++;

if (position == 4)
{

setLocked(false);

delay(100);

void setLocked(int locked)

if (locked)

digitalWrite(redPin, HIGH);

digitalWrite(greenPin, LOW);

else

digitalWrite(redPin, LOW);

digitalWrite(greenPin, HIGH);

}
Circuit:

Exercise:
Write a sketch to control the LEDs based on their Labelled Number. (Take input
from Keypad and there are 10 LED’s in all labelled from 0 to 9)
Code:
#include <Keypad.h>

int LED0 = 8;

int LED1 = 9;

int LED2 = 10;

int LED3 = 11;

int LED4 = 12;

int LED5 = 13;

int LED6 = 14;

int LED7 = 15;

int LED8 = 16;


int LED9 = 17;

const byte rows = 4;

const byte cols = 3;

char keys[rows][cols] =

{'1','2','3'},

{'4','5','6'},

{'7','8','9'},

{'*','0','#'}

};

byte rowPins[rows] = {2, 7, 6, 4};

byte colPins[cols] = {3, 1, 5};

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);

void setup()

pinMode(LED0, OUTPUT);

pinMode(LED1, OUTPUT);

pinMode(LED2, OUTPUT);

pinMode(LED3, OUTPUT);

pinMode(LED4, OUTPUT);

pinMode(LED5, OUTPUT);

pinMode(LED6, OUTPUT);

pinMode(LED7, OUTPUT);

pinMode(LED8, OUTPUT);

pinMode(LED9, OUTPUT);

void loop()

char key = keypad.getKey();


if (key == '*' || key == '#')

digitalWrite(LED0,LOW);

digitalWrite(LED1,LOW);

digitalWrite(LED2,LOW);

digitalWrite(LED3,LOW);

digitalWrite(LED4,LOW);

digitalWrite(LED5,LOW);

digitalWrite(LED6,LOW);

digitalWrite(LED7,LOW);

digitalWrite(LED8,LOW);

digitalWrite(LED9,LOW);

else if (key == '0')

digitalWrite(LED0,HIGH);

digitalWrite(LED1,LOW);

digitalWrite(LED2,LOW);

digitalWrite(LED3,LOW);

digitalWrite(LED4,LOW);

digitalWrite(LED5,LOW);

digitalWrite(LED6,LOW);

digitalWrite(LED7,LOW);

digitalWrite(LED8,LOW);

digitalWrite(LED9,LOW);

else if (key == '1')

digitalWrite(LED0,LOW);
digitalWrite(LED1,HIGH);

digitalWrite(LED2,LOW);

digitalWrite(LED3,LOW);

digitalWrite(LED4,LOW);

digitalWrite(LED5,LOW);

digitalWrite(LED6,LOW);

digitalWrite(LED7,LOW);

digitalWrite(LED8,LOW);

digitalWrite(LED9,LOW);

else if (key == '2')

digitalWrite(LED0,LOW);

digitalWrite(LED1,LOW);

digitalWrite(LED2,HIGH);

digitalWrite(LED3,LOW);

digitalWrite(LED4,LOW);

digitalWrite(LED5,LOW);

digitalWrite(LED6,LOW);

digitalWrite(LED7,LOW);

digitalWrite(LED8,LOW);

digitalWrite(LED9,LOW);

else if (key == '3')

digitalWrite(LED0,LOW);

digitalWrite(LED1,LOW);

digitalWrite(LED2,LOW);

digitalWrite(LED3,HIGH);
digitalWrite(LED4,LOW);

digitalWrite(LED5,LOW);

digitalWrite(LED6,LOW);

digitalWrite(LED7,LOW);

digitalWrite(LED8,LOW);

digitalWrite(LED9,LOW);

else if (key == '4')

digitalWrite(LED0,LOW);

digitalWrite(LED1,LOW);

digitalWrite(LED2,LOW);

digitalWrite(LED3,LOW);

digitalWrite(LED4,HIGH);

digitalWrite(LED5,LOW);

digitalWrite(LED6,LOW);

digitalWrite(LED7,LOW);

digitalWrite(LED8,LOW);

digitalWrite(LED9,LOW);

else if (key == '5')

digitalWrite(LED0,LOW);

digitalWrite(LED1,LOW);

digitalWrite(LED2,LOW);

digitalWrite(LED3,LOW);

digitalWrite(LED4,LOW);

digitalWrite(LED5,HIGH);

digitalWrite(LED6,LOW);
digitalWrite(LED7,LOW);

digitalWrite(LED8,LOW);

digitalWrite(LED9,LOW);

else if (key == '6')

digitalWrite(LED0,LOW);

digitalWrite(LED1,LOW);

digitalWrite(LED2,LOW);

digitalWrite(LED3,LOW);

digitalWrite(LED4,LOW);

digitalWrite(LED5,LOW);

digitalWrite(LED6,HIGH);

digitalWrite(LED7,LOW);

digitalWrite(LED8,LOW);

digitalWrite(LED9,LOW);

else if (key == '7')

digitalWrite(LED0,LOW);

digitalWrite(LED1,LOW);

digitalWrite(LED2,LOW);

digitalWrite(LED3,LOW);

digitalWrite(LED4,LOW);

digitalWrite(LED5,LOW);

digitalWrite(LED6,LOW);

digitalWrite(LED7,HIGH);

digitalWrite(LED8,LOW);

digitalWrite(LED9,LOW);
}

else if (key == '8')

digitalWrite(LED0,LOW);

digitalWrite(LED1,LOW);

digitalWrite(LED2,LOW);

digitalWrite(LED3,LOW);

digitalWrite(LED4,LOW);

digitalWrite(LED5,LOW);

digitalWrite(LED6,LOW);

digitalWrite(LED7,LOW);

digitalWrite(LED8,HIGH);

digitalWrite(LED9,LOW);

else if (key == '9')

digitalWrite(LED0,LOW);

digitalWrite(LED1,LOW);

digitalWrite(LED2,LOW);

digitalWrite(LED3,LOW);

digitalWrite(LED4,LOW);

digitalWrite(LED5,LOW);

digitalWrite(LED6,LOW);

digitalWrite(LED7,LOW);

digitalWrite(LED8,LOW);

digitalWrite(LED9,HIGH);

}
Circuit:

You might also like