// Learn about the ESP32 WiFi simulation in
// https://docs.wokwi.com/guides/esp32-wifi


/*
 * Date:    31/05/2023
 * Title:   LCD 16x2 ESP32 BIGNUMBERS
 * Author:  Rubén Lozano
 * MOD Edit:    RELL9030
 * References:  https://steemit.com/utopian-io/@lapilipinas/arduino-big-digits-0-99-with-i2c-16x2-lcd
 *              
*/

/******************************************************
 *    LIBRERIAS
 ******************************************************/

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

/******************************************************
 *    VARIABLES & DEFINES
 ******************************************************/
#define pot 34
#define I2C_address 0x27 // 0x27 , 0x3F , 0x20 , 0x38
// set the LCD number of columns and rows
int lcdColumns = 16;
int lcdRows = 2;
LiquidCrystal_I2C lcd(I2C_address, lcdColumns, lcdRows); 

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int up_buttonState = 0;         // current state of the up button
int up_lastButtonState = 0;     // previous state of the up button
int down_buttonState = 0;         // current state of the up button
int down_lastButtonState = 0;     // previous state of the up button

bool bPress = false;

byte LT[8] = { B00111, B01111, B11111, B11111, B11111, B11111, B11111, B11111 };
byte UB[8] ={ B11111, B11111, B11111, B00000, B00000, B00000, B00000, B00000 };
byte RT[8] ={ B11100, B11110, B11111, B11111, B11111, B11111, B11111, B11111 };
byte LL[8] ={ B11111, B11111, B11111, B11111, B11111, B11111, B01111, B00111 };
byte LB[8] ={ B00000, B00000, B00000, B00000, B00000, B11111, B11111, B11111 };
byte LR[8] ={ B11111, B11111, B11111, B11111, B11111, B11111, B11110, B11100 };
byte MB[8] ={ B11111, B11111, B11111, B00000, B00000, B00000, B11111, B11111 };
byte block[8] ={ B11111, B11111, B11111, B11111, B11111, B11111, B11111, B11111 };

/******************************************************
 *    SETUP
 ******************************************************/
void setup()
{
Wire.begin();
Serial.begin(115200);
lcd.begin(lcdColumns,lcdRows);                      // initialize the lcd
  lcd.createChar(0,LT);
  lcd.createChar(1,UB);
  lcd.createChar(2,RT);
  lcd.createChar(3,LL);
  lcd.createChar(4,LB);
  lcd.createChar(5,LR);
  lcd.createChar(6,MB);
  lcd.createChar(7,block);
  // Print a message to the LCD.
  lcd.backlight();
  int temp = 10;
  lcd.clear();
  printDigits(0,0);
  printDigits(1,4);
  printDigits(2,8);
  printDigits(3,12);
  lcd.clear();
}

/******************************************************
 *    LOOP
 ******************************************************/
void loop()
{
   float lectura = analogRead(pot)*3.3/4090; //lectura del pot
   printNumber(lectura,0);
   lcd.setCursor(6, 1);
   lcd.print(".");
   int lectura2=lectura*100;
   Serial.print(lectura);
   Serial.print("\t");
   Serial.print(lectura2);
   byte lastDigit =lectura2%100;
   Serial.print("\t");
   Serial.println(lastDigit);
   printNumber(lastDigit,7);
   lcd.setCursor(14, 1);
   lcd.print("V");
   delay(100);
}

/******************************************************
 *    FUNCIONES
 ******************************************************/
void printNumber(int val,int col){  //imprime el numero actualizado
     printDigits(val/10,col);
     printDigits(val%10,col+3);
}

void custom0(int x){
  lcd.setCursor(x,0);
  lcd.write((byte)0); 
  lcd.write(1); 
  lcd.write(2);
  lcd.setCursor(x, 1);
  lcd.write(3); 
  lcd.write(4); 
  lcd.write(5);
}
void custom1(int x){
  lcd.setCursor(x,0);
  lcd.write(1);
  lcd.write(2);
  lcd.print(" ");
  lcd.setCursor(x,1);
  lcd.write(4);
  lcd.write(7);
  lcd.write(4);
}
void custom2(int x){
  lcd.setCursor(x,0);
  lcd.write(6);
  lcd.write(6);
  lcd.write(2);
  lcd.setCursor(x, 1);
  lcd.write(3);
  lcd.write(4);
  lcd.write(4);
}
void custom3(int x){
  lcd.setCursor(x,0);
  lcd.write(6);
  lcd.write(6);
  lcd.write(2);
  lcd.setCursor(x, 1);
  lcd.write(4);
  lcd.write(4);
  lcd.write(5);
}
void custom4(int x){
  lcd.setCursor(x,0);
  lcd.write(3);
  lcd.write(4);
  lcd.write(7);
  lcd.setCursor(x, 1);
  lcd.print(" ");
  lcd.print(" ");
  lcd.write(7);
}
void custom5(int x){
  lcd.setCursor(x,0);
  lcd.write(3);
  lcd.write(6);
  lcd.write(6);
  lcd.setCursor(x, 1);
  lcd.write(4);
  lcd.write(4);
  lcd.write(5);
}
void custom6(int x){
  lcd.setCursor(x,0);
  lcd.write((byte)0);
  lcd.write(6);
  lcd.write(6);
  lcd.setCursor(x, 1);
  lcd.write(3);
  lcd.write(4);
  lcd.write(5);
}
void custom7(int x){
  lcd.setCursor(x,0);
  lcd.write(1);
  lcd.write(1);
  lcd.write(2);
  lcd.setCursor(x, 1);
  lcd.print(" ");
  lcd.print(" ");
  lcd.write(7);
}
void custom8(int x){
  lcd.setCursor(x,0);
  lcd.write((byte)0);
  lcd.write(6);
  lcd.write(2);
  lcd.setCursor(x, 1);
  lcd.write(3);
  lcd.write(4);
  lcd.write(5);
}
void custom9(int x){
  lcd.setCursor(x,0);
  lcd.write((byte)0);
  lcd.write(6);
  lcd.write(2);
  lcd.setCursor(x, 1);
  lcd.print(" ");
  lcd.print(" ");
  lcd.write(7);
}
void printDigits(int digits, int x){
  // utility function for digital clock display: prints preceding colon and leading 0
  switch (digits) {
  case 0: 
    custom0(x);
    break;
  case 1: 
    custom1(x);
    break;
  case 2: 
    custom2(x);
    break;
  case 3: 
    custom3(x);
    break;
  case 4: 
    custom4(x);
    break;
  case 5: 
    custom5(x);
    break;
  case 6: 
    custom6(x);
    break;
  case 7: 
    custom7(x);
    break;
  case 8: 
    custom8(x);
    break;
  case 9: 
    custom9(x);
    break;
  }
}