#include <LiquidCrystal_I2C.h> // if you don´t have I2C version of the display, use LiquidCrystal.h library instead
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
//LiquidCrystal_I2C lcd(0x3f,16,2); // set the LCD address to 0x3f for a 16 chars and 2 line display
// if you don´t know the I2C address of the display, use I2C scanner first (https://playground.arduino.cc/Main/I2cScanner/)
//int c1 = 13, c2 = 12, c3 = 11, c4 =10;
//int r1 = 9, r2 = 8, r3 = 7, r4 = 6;
int r1 = 13, r2 = 12, r3 = 11, r4 =10;
int c1 = 9, c2 = 8, c3 = 7, c4 = 6;
int del = 600; //pequeño retraso
void setup()
{
lcd.init(); // inicializar 16x2 lcd module
lcd.backlight(); // enciende la retroiluminación del módulo LCD
lcd.setCursor(2,0); // movemos el Cursor a la Fila 0 Columna 0 // COLOCAR A MEDIO
lcd.print("HOLA MUNDO"); // imprime el mensaje
lcd.setCursor(0,1); // Movemos el cursor a la Fila 1 Columna 0
//lcd.write(0x41,0x42); // PRUEBITA CON CARACTERES ASCII
lcd.write(0x41);
lcd.write(0x42);
//TAREA DE BRANDON COLOCAR 16 SÍMBOLOS EN LA PARTE DE ABAJO
delay(1000);
lcd.clear(); //limpiamos la pantalla
/* use input pull-up feature of arduino, so that input c1 to c3 remains HIGH in absence input
signal
*/
pinMode(c1, INPUT_PULLUP);
pinMode(c2, INPUT_PULLUP);
pinMode(c3, INPUT_PULLUP);
pinMode(c4, INPUT_PULLUP);
// Set r1,r2,r3,r4 pins to OUTPUT mode
pinMode(r1, OUTPUT);
pinMode(r2, OUTPUT);
pinMode(r3, OUTPUT);
pinMode(r4, OUTPUT);
}
void loop()
{
// Calling user defined function row1( ), row2( ), row3( ) and row4( ) next to each other
row1();
row2();
row3();
row4();
}
void row1() {
/* This the same condition we understand in topic 9.2 i.e. one row should be Low and others should be High to distinguish between other rows and to print row 1 data
*/
digitalWrite(r1, LOW); //COLOCAMOS LA SALIDA EN ESTADO BAJO
digitalWrite(r2, HIGH);
digitalWrite(r3, HIGH);
digitalWrite(r4, HIGH);
/* We know high signal becomes low when it gets a direct path to ground */
/* Reading state of c1 and checking if it is LOW then print ‘1’ and if c2 is LOW print ‘2’ and if c3 is LOW print ‘3’
*/
if (digitalRead(c1) == LOW) {
lcd.print("1"); delay(del);
}
else if (digitalRead(c2) == LOW) {
lcd.print("2"); delay(del);
}
else if (digitalRead(c3) == LOW) {
lcd.print("3"); delay(del);
}
else if (digitalRead(c4) == LOW) {
lcd.print("A"); delay(del);
}
//delay(del);
/*why do we not set single delay here instead three in if blocks? Because if we give delay in
if block then delay will executed only if any key has been pressed but if we give delay
here each time either key pressed or not delay will be executed which slows the
programming process.
*/
//delay(500);
}
/* Similarly as we set logic for row1 we can set logic for other rows.
Keep row LOW which we want to set logic and others to be HIGH
But this time we will print ‘4’, ‘5’, ‘6’ on column key pressed instead of ‘1’, ‘2’, ‘3’.
*/
void row2() {
digitalWrite(r1, HIGH);
digitalWrite(r2, LOW);
digitalWrite(r3, HIGH);
digitalWrite(r4, HIGH);
if (digitalRead(c1) == LOW) {
lcd.print("4"); delay(del);
}
else if (digitalRead(c2) == LOW) {
lcd.print("5"); delay(del);
}
else if (digitalRead(c3) == LOW) {
lcd.print("6"); delay(del);
}
else if (digitalRead(c4) == LOW) {
lcd.print("B"); delay(del);
}
// delay(500);
}
/* In this we will print ‘7’, ‘8’, ‘9’ on column key pressed.
*/
void row3() {
digitalWrite(r1, HIGH);
digitalWrite(r2, HIGH);
digitalWrite(r3, LOW);
digitalWrite(r4, HIGH);
if (digitalRead(c1) == LOW) {
lcd.print("7"); delay(del);
}
else if (digitalRead(c2) == LOW) {
lcd.print("8"); delay(del);
}
else if (digitalRead(c3) == LOW) {
lcd.print("9"); delay(del);
}
else if (digitalRead(c4) == LOW) {
lcd.print("C"); delay(del);
}
//delay(500);
}
/* In this we will print ‘*’, ‘0’, ‘#’ on column key pressed.
*/
void row4() {
digitalWrite(r1, HIGH);
digitalWrite(r2, HIGH);
digitalWrite(r3, HIGH);
digitalWrite(r4, LOW);
if (digitalRead(c1) == LOW) {
lcd.print("*"); delay(del);
}
else if (digitalRead(c2) == LOW) {
lcd.print("0"); delay(del);
}
else if (digitalRead(c3) == LOW) {
lcd.print("#"); delay(del);
}
else if (digitalRead(c4) == LOW) {
lcd.print("D"); delay(del);
}
// delay(500);
}
//TAREA DE BRANDON:
// CUANDO SE PRESIONE LA TECLA A = BORRAR TODA PANTALLA
// CUANDO SE PRESIONE LA TECLA B = BORRA UN CARACTER HACIA ATRÁS
// CUANDO SE PRESIONE LA TECLA C = SALTO A LA SIGUIENTE FILA
// CUANDO SE PRESIONE LA TECLA D = COLOCAR EL CURSOR AL MEDIO DE LA PANTALLA