0% found this document useful (0 votes)
7 views10 pages

Lab 9 Sol

The document contains multiple Arduino sketches that utilize the LiquidCrystal_I2C library to interface with LCD displays. Each sketch demonstrates different functionalities such as displaying static messages, reading analog values, measuring distances with a sensor, and implementing a keypad-based calculator. The code snippets are structured to initialize the LCD, read inputs, and display outputs accordingly.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views10 pages

Lab 9 Sol

The document contains multiple Arduino sketches that utilize the LiquidCrystal_I2C library to interface with LCD displays. Each sketch demonstrates different functionalities such as displaying static messages, reading analog values, measuring distances with a sensor, and implementing a keypad-based calculator. The code snippets are structured to initialize the LCD, read inputs, and display outputs accordingly.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

ej.

#include <LiquidCrystal_I2C.h>

#include <Wire.h>

LiquidCrystal_I2C lcd(0x3F, 16, 2);

void setup() {

lcd.init();

lcd.backlight();

lcd.print("Hola a todos");

void loop() {

ej.2

#include <LiquidCrystal_I2C.h>

#include <Wire.h>

LiquidCrystal_I2C lcd(0x3F, 16, 2);

int val = 0;

void setup() {

lcd.init();

lcd.backlight();

void loop() {

val = analogRead(A0);

lcd.clear();

lcd.print("Valor: ");

lcd.print(val);

delay(500);

}
ej.3

#include <LiquidCrystal_I2C.h>

#include <Wire.h>

#include <HCSR04.h>

HCSR04 hc (12, 11);

LiquidCrystal_I2C lcd(0x3F, 16, 2);

void setup() {

Serial.begin(9600);

lcd.init();

lcd.backlight();

void loop() {

lcd.clear();

lcd.setCursor(0, 0);

Serial.println(hc.dist());

lcd.print(hc.dist());

lcd.print("cm");

delay(500);

}
ej.4

#include <Keypad.h>

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F,16,2);

const byte ROWS = 4;

const byte COLS = 4;

char keys [ROWS] [COLS] = {

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

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

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

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

};

byte rowPins [ROWS] = {9,8,7,6};

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

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

void setup() {

Serial.begin(9600);

lcd.init();

lcd.backlight();

void loop() {

char key = keypad.getKey();

if (key != NO_KEY) {

Serial.println(key);
lcd.clear();

lcd.setCursor(0, 0);

lcd.print(key);

delay(500);

ej.5

#include <Keypad.h>

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F, 16, 2); // (0x3f,16,2)(0x27,16,2)ó(0x20,16,2)

const byte FIL = 4;

const byte COL = 4;

char keys[FIL][COL] = {

{'1', '2', '3', '+'}, //define la matriz del teclado

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

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

{'C', '0', 'E', '/'} };

byte filas[FIL] = {9,8,7,6};

byte columnas[COL] = {5,4,3,2};

Keypad teclado = Keypad(makeKeymap(keys), filas, columnas, FIL, COL);

char tecla;

char num[7]; //numero a ingresar max de 7 digitos

byte indice = 0;

byte pos = 0;

void borrenum() {

num[0] = 0;

num[1] = 0;

num[2] = 0;
num[3] = 0;

num[4] = 0;

num[5] = 0;

num[6] = 0;

indice = 0;

void imprimirlcd()

{ lcd.setCursor(pos, 0); //indice

lcd.print(tecla);

void cargar() {

if (indice >= 7) {

Serial.print("dms");

else {

num[indice] = tecla;

indice++;

pos++;

Serial.print(tecla);

imprimirlcd();

long num1 = 0;

long num2 = 0;

char operacion = '+';

void setup() {

lcd.init();

lcd.backlight();

lcd.clear();

lcd.setCursor(0, 0);
lcd.print("Calculadora");

delay(1000);

lcd.clear();

lcd.setCursor(0, 0); //posicion inicial

lcd.blink();

Serial.begin(9600);

void loop() {

tecla = teclado.getKey();

if (tecla) {

switch (tecla) {

case '1':

cargar();

break;

case '2':

cargar();

break;

case '3':

cargar();

break;

case '4':

cargar();

break;

case '5':

cargar();

break;

case '6':

cargar();

break;

case '7':

cargar();
break;

case '8':

cargar();

break;

case '9':

cargar();

break;

case '0':

cargar();

break;

case '+':

Serial.println();

num1 = atof(num); //las funciones atof convierte un char array, a un número entero

lcd.setCursor(indice + 1, 0);

borrenum();

operacion = '+';

pos++;

lcd.print(operacion);

break;

case '-':

Serial.println();

num1 = atof(num); //las funciones atof convierte un char array, a un número entero

lcd.setCursor(indice + 1, 0);

borrenum();

operacion = '-';

pos++;

lcd.print(operacion);

break;

case 'x':

Serial.println();

num1 = atof(num); //las funciones atof convierte un char array, a un número entero
Serial.println(num1);

lcd.setCursor(indice + 1, 0);

borrenum();

operacion = 'x';

pos++;

lcd.print(operacion);

break;

case '/':

Serial.println();

num1 = atof(num); //las funciones atof convierte un char array, a un número entero

lcd.setCursor(indice + 1, 0);

borrenum();

operacion = '/';

pos++;

break;

case 'C':

Serial.println();

borrenum();

lcd.clear();

pos = 0;

lcd.setCursor(0, 0); //posicion inicial

break;

case 'E':

num2 = atof(num);

borrenum();

Serial.println();

Serial.print(num1);

Serial.print(operacion);

Serial.print(num2);

Serial.print("=");

lcd.setCursor(0, 1);
lcd.print('=');

switch (operacion) {

case '+':

Serial.println(num1);

Serial.println(num2);

Serial.println(num1 + num2);

lcd.print(num1 + num2);

lcd.setCursor(0, 0);

pos = 0;

num1 = 0;

num2 = 0;

break;

case '-':

Serial.print(num1 - num2);

lcd.print(num1 - num2);

lcd.setCursor(0, 0);

pos = 0;

num1 = 0;

num2 = 0;

break;

case 'x':

Serial.print(num1 * num2);

lcd.print(num1 * num2);

lcd.setCursor(0, 0);

pos = 0;

num1 = 0;

num2 = 0;

break;

case '/':

if (num2 == 0) {

Serial.println("Error div/0");
}

else {

Serial.print(num1 / num2);

lcd.print(num1 / num2);

lcd.setCursor(0, 0);

pos = 0;

num1 = 0;

num2 = 0; }

break; }

break;

You might also like