#include <Wire.h> /*~ Librería I2C ~*/
#include <LiquidCrystal_I2C.h> /*~ Librería LCD ~*/
byte mainPlayer [ ] = {B01110, B01010, B01110, B11111, B00100, B00100, B01010, B10001};
LiquidCrystal_I2C lcd ( 0x27, 16, 2 ); /*~ Instancia de la clase para el manejo de la pantalla ( Dirección I2C, cantidad de columnas, cantidad de filas ) ~*/
#define VRX_PIN 36 // ESP32 pin GPIO36 (ADC0) connected to VRX pin
#define VRY_PIN 39 // ESP32 pin GPIO39 (ADC0) connected to VRY pin
#define MAX_VERT 4095
#define MAX_HORI 4095
int valueX = 0; // to store the X-axis value
int valueY = 0; // to store the Y-axis value
int row = 0;
int col = 0;
// Bandera para el intro
bool isIntroFinished = false;
void setup ( void ) {
Serial.begin(115200);
lcd.init ( );
lcd.backlight ( );
lcd.createChar(0, mainPlayer);
//lcd.createChar(1, arrowRight);
pinMode(VRX_PIN,INPUT);
pinMode(VRY_PIN,INPUT);
lcd.setCursor(row, col);
lcd.write(0);
}
void loop ( void ) {
if(!isIntroFinished){
introGame();
}
// read X and Y analog values
valueX = analogRead(VRX_PIN);
valueY = analogRead(VRY_PIN);
Serial.println(valueX);
Serial.println(valueY);
if(valueX == MAX_HORI){
right();
}
if(valueX == 0){
left();
}
if(valueY == MAX_VERT){
up();
}
if(valueY == 0){
down();
}
lcd.setCursor ( row, col ); /*~ ( columnas, filas) Ubicamos el cursor en la primera posición(columna:0) de la primera línea(fila:0) ~*/
lcd.write ( 0 ); /*~ Mostramos nuestro primer icono o caracter ~*/
delay(150);
lcd.clear ( );
}
void left(){
if(row == 15){ // Si llega al límite
row = 15;
} else{
row = row + 1;
}
}
void right(){
if(row == 0){
row = 0;
} else{
row = row - 1;
}
}
void up(){
col = 0;
}
void down(){
col = 1;
}
void introGame(){
// Variables locales para la intro
const byte characterFull = 0xFF;
const byte characterEmpty = 0x20;
const char* autores[] = {"=== Autores ===", "Alonso Flores", "Joel Garcia", "Victor Martinez", "Eric Puente"};
const char* title = "Catch the";
const char* nameGame = "Diamonds";
byte diamondTopLeft[] = { B00000, B00000, B00111, B01000, B10100, B10010, B10001, B01000 };
byte diamondTop[] = { B00000, B00000, B11111, B10001, B01010, B00100, B01010, B10001 };
byte diamondTopRight[] = { B00000, B00000, B11100, B00010, B00101, B01001, B10001, B00010 };
byte diamondBottomLeft[] = { B00101, B00010, B00001, B00000, B00000, B00000, B00000, B00000 };
byte diamondBottom[] = { B10001, B01010, B00100, B10001, B01110, B00000, B00000, B00000 };
byte diamondBottomRight[] = { B10100, B01000, B10000, B00000, B00000, B00000, B00000, B00000 };
// Crea los caracteres del diamante del 3 al 6
lcd.createChar(2, diamondTopLeft);
lcd.createChar(3, diamondTop);
lcd.createChar(4, diamondTopRight);
lcd.createChar(5, diamondBottomLeft);
lcd.createChar(6, diamondBottom);
lcd.createChar(7, diamondBottomRight);
lcd.clear();
// "Feria" de caracteres
lcd.write(characterFull);
for(int i=0; i<17; i++){
lcd.setCursor(i+1,0);
lcd.write(characterFull);
lcd.setCursor(i,1);
lcd.write(characterFull);
delay(100);
}
for(int i = 15; i>0; i--){
lcd.setCursor(i-1,0);
lcd.write(characterEmpty);
lcd.setCursor(i,1);
lcd.write(characterEmpty);
delay(100);
}
delay(500);
lcd.clear();
// Dibuja el diamante
lcd.setCursor(6, 0);
for(int i = 2; i<=4; i++) lcd.write(byte(i));
lcd.setCursor(6, 1);
for(int i = 5; i<=7; i++) lcd.write(byte(i));
delay(2000);
lcd.clear();
// Titulo desplazándose
lcd.print(title);
for(int i = 0; i < 7; i++){
// Mostrar el título desplazándose
lcd.scrollDisplayRight();
delay(200);
}
lcd.setCursor(8, 1);
lcd.print(nameGame);
for(int i = 0; i < 7; i++){
lcd.scrollDisplayLeft();
delay(200);
}
delay(2000);
lcd.clear();
// Mostrar autores
int numAutores = sizeof(autores) / sizeof(autores[0]);
for(int i=0; i < numAutores; i++){
lcd.setCursor(0,0);
lcd.print(autores[i]);
if(i+1<sizeof(autores)){
lcd.setCursor(0,1);
lcd.print(autores[i+1]);
}
delay(2000);
lcd.clear();
}
isIntroFinished = true;
}
El brillo de la pantalla puede modularse
con el potenciometro de la parte trasera.