Introduction: Arduino Uno R3 1602 LCD Safe Lock With Joystick Control

I just got this copy arduino Uno kit and it's so awesome I had to make my first instructable to go with!

Here I will show the basic layout using PS2 analog stick to control the LCD screen, and the code to go with it.

Video 2 (coming soon!) will include instructions to program the MAX7219 to run the final code so I don't have to use the entire arduino chip for this project, then hook up a 12v step motor, couple circuit breakers, and encase it all in thick sheet steel to make a working electronic safe!

Step 1: Setup the Hardware

We setup the hardware according to diagram above. The joystick X-axis movement is wired to analog pin A0 and Y-axis to A1.

Note: the switch pin on the joystick. Must be connected to a digital OUTPUT pinmode, if not it doesn't give accurate readings. Best to just use pin 13...

The resistors going from 5v and GND to the green wire (LCD pin 3) replace the potentiometer. I used 1k ohms from 5V and 330 ohms going down to GND, but you may need to use different resistors or a pot depending on the LCD.
The third resistor from 5V to the backlight is 10k ohms.

Step 2: Write the Code

You can download Arduino IDE from arduino.cc
It is based on C/Java.
Here is the code I used:

/* 1602 LCD 4 digit passcode controlled by joystick
*/

//define variable to store digits selected & position of cursor
int cursorPos;
int dig[4];

//include LCD library
#include < LiquidCrystal.h >

//initiate lcd pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

//main setup function runs once
void setup() {

//setup lcd display
lcd.begin(16, 2);
lcd.print("Enter password:");
lcd.setCursor(6,1);
lcd.print("0000");
lcd.cursor();
lcd.noBlink();
lcd.setCursor(6,1);

//set values of variables
cursorPos = 0;
for (int i = 0; i <=3; i++){
dig[i]=0;
}

//blink LED on board
digitalWrite(13, LOW);
delay(500);
digitalWrite(13, HIGH);

}

//loop code runs continuously while Arduino has power
void loop() {
if (digitalRead(13) == HIGH) {
while (digitalRead(13) == HIGH){ //anything in this loop will run until the joystick is pressed in
int sensorX = analogRead(A0); //get readings for x,y-axis movement
int sensorY = analogRead(A1);
float angleX = sensorX * (180.0 / 1023.0); //convert value to angle
float angleY = sensorY * (180.0 / 1023.0);
if (angleX > 100) {
moveRight();
delay(500);
} else if ( angleX < 70) {
moveLeft();
delay(500);
} else {
if (angleY > 105){
decrease(cursorPos);
delay(250);
} else if(angleY < 75) {
increase(cursorPos);
delay(250);
}
}
}
}

//this runs once the button is presses
checkCode();
delay(2000);
setup();
}

void moveRight(){
if (cursorPos == 3){
lcd.setCursor(6, 1);
cursorPos = 0;
} else {
int a = cursorPos + 7;
lcd.setCursor(a, 1);
cursorPos = cursorPos + 1;
}
}

void moveLeft(){
if (cursorPos == 0){
lcd.setCursor(9, 1);
cursorPos = 3;
} else {
int b = cursorPos + 5;
lcd.setCursor(b, 1);
cursorPos = cursorPos - 1;
}
}

void increase(int cursorPos){
if (dig[cursorPos] == 9){
dig[cursorPos] = 0;
} else {
dig[cursorPos] = dig[cursorPos] + 1;
}
int c = cursorPos + 6;
lcd.setCursor(c, 1);
lcd.print(dig[cursorPos]);
lcd.setCursor(c, 1);
}

void decrease(int cursorPos){
if (dig[cursorPos] == 0){
dig[cursorPos] = 9;
} else {
dig[cursorPos] = dig[cursorPos] - 1;
}
int c = cursorPos + 6;
lcd.setCursor(c, 1);
lcd.print(dig[cursorPos]);
lcd.setCursor(c, 1);
}
//change this part for your own passcode

//I've picked 1234....
void checkCode(){
if (dig[0] == 1 && dig[1] == 2 && dig[2] == 3 && dig[3] == 4){ //Change '1', '2', '3', '4' on this line!!!!
lcd.setCursor(4,1);
lcd.print("Correct!");
} else {
lcd.setCursor(5,1);
lcd.print("Wrong!");
}
}

Step 3: Get Creative!!!

Have fun, and please comment below!!