This post will include our code for the project. All the codes listed below are provided with full commenting making it very easy for someone to recreate our project. Along with the code, we have also provided the Fritzing diagram so anyone will have full instructions on how to wire all the hardware components as well as a clear understanding of the software.
Here is the code below:
// project.ino
#include <LiquidCrystal.h>
#include <string.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);//initialize the library with the number of the interface pins
//Select each interface pin for each button
const int button_up= 02;
const int button_down= 03;
const int button_left= 04;
const int button_right=05;
const int button_done= 06;
const int button_print=0;
//——————————————
//variables to read the button states
int state_up=0;
int state_down=0;
int state_left=0;
int state_right=0;
int state_done=0;
int state_print=0;
//———————————————
//string will all the letters from the alphabet
char alphabet[27]= {‘_’, ‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘H’, ‘I’, ‘J’, ‘K’, ‘L’, ‘M’, ‘N’, ‘O’, ‘P’, ‘Q’, ‘R’, ‘S’, ‘T’, ‘U’, ‘V’, ‘X’, ‘W’, ‘Y’, ‘Z’};
//matrix that will store all the names
char name[20][17];
char password[]=”MASTER”;
int i;//counter for loops
int j=0;//count the order of each inserted name
int pos=0;//determines the position of the cursor in the name
int alpha=0;//runs the array alphabet
int RET=1;
void setup() {
Serial.begin(9600);
//define all the buttons as inputs
pinMode(button_up, INPUT);
pinMode(button_down, INPUT);
pinMode(button_left, INPUT);
pinMode(button_right, INPUT);
pinMode(button_done, INPUT);
pinMode(button_print, INPUT);
//———————————
lcd.begin(20, 4);//starts the lcd display, 20 rows 4 lines
for(int k=0; k<20; k++)//initiate all the names with ‘_’
for(int l=0; l<16; l++)
name[k][l]=’_’;
}
void loop() {
//read the state of each button
state_up=digitalRead(button_up);
state_down=digitalRead(button_down);
state_left=digitalRead(button_left);
state_right=digitalRead(button_right);
state_done=digitalRead(button_done);
state_print=digitalRead(button_print);
lcd.setCursor(0, 0);
lcd.print(“ENTER YOUR NAME:”);
//i=0;
//lcd.setCursor(i, 1);
//lcd.blink();
for(i=0; i<16; i++)//will update the lcd screen with the typed character
{
lcd.setCursor(i, 2);
lcd.print(name[j][i]);
}
if(state_up==HIGH)//when up is pressed, runs from a-z in a ascendent order
{
if(alpha==26)//if you are in the ‘Z’ character, goes back to the beggining
alpha=0;
else
alpha++;
name[j][pos]=alphabet[alpha];//insert the chosen character in the respective position
}
if(state_down==HIGH)//when down is pressed, does the same as above, but in a descendent order
{
if(alpha==0)
alpha=25;
else
alpha–;
name[j][pos]=alphabet[alpha];
}
if(state_left==HIGH)//move between characters in the display
{
if(pos==0)//if you are in the first character and press left, goes to the last character
pos=15;
else
pos–;
alpha=give_back(j, pos);
}
if(state_right==HIGH)//the same as above
{
if(pos==15)
pos=0;
else
pos++;
alpha=give_back(j, pos);
}
if(state_done==HIGH)//when done is pressed, save the name, and open the next position in the matrix for another name
{
pos++;
name[j][pos]=”;
RET=strcmp(name[j], password);
Serial.println(RET);
if(RET==0)
{
press_print();
}
else
{
RET=0;
pos=0;
j++;
alpha=0;
}
}
/*if(state_print==HIGH)
{
press_print();
}*/
lcd.setCursor(pos, 2);//blink the cursor in the position that can be changed
lcd.blink();
delay(150);
//lcd.clear();
//lcd.blink();
}
int give_back(int j, int pos)//returns the respective ‘number’ of the character when the cursor moves back to a position
{
int c=0;
while(name[j][pos]!=alphabet[c])
{
c++;
}
return (c);
}
void press_print()//print the list of names in the serial monitor
{
lcd.setCursor(0,3);
lcd.print(“Printing on Serial”);
delay(1000);
lcd.clear();
for(int w=0; w<20; w++)
{for(int y=0; y<16; y++)
Serial.print(name[w][y]);
Serial.println(“”);
}
j=0;
pos=0;
for(int k=0; k<20; k++)//RESET THE MATRIX AFTER PRINTING
for(int l=0; l<16; l++)
name[k][l]=’_’;
alpha=0;
//delay();
}