/*******************************
****  Autor: José M. Lázaro ****
****  Fecha: 2023-09-07     ****
****  Versión: 1.00         ****
*******************************/

#include <Keypad.h>
	const uint8_t ROWS = 4;
	const uint8_t COLS = 4;
	char keys[ROWS][COLS] = {
		{ '1', '2', '3', 'A' },
		{ '4', '5', '6', 'B' },
		{ '7', '8', '9', 'C' },
		{ '.', '0', '=', 'D' }
	};
	uint8_t colPins[COLS] = { 5, 4, 3, 2 }; // Pins connected to C1, C2, C3, C4
	uint8_t rowPins[ROWS] = { 9, 8, 7, 6 }; // Pins connected to R1, R2, R3, R4

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

#include <LiquidCrystal_I2C.h>
	#define I2C_ADDR    0x27
	#define LCD_COLUMNS 20
	#define LCD_LINES   4
	LiquidCrystal_I2C lcd1(I2C_ADDR, LCD_COLUMNS, LCD_LINES);

// Variables
int x1=0;
int x2=0;
int posX=0;
int posY=0;
int posXmax=20;
int posYmax=4;
int val=0;
String cursor="_";
bool lcd=1;
	// Salidas Relé
bool O1=0;
bool O2=0;
bool O3=0;
bool O4=0;

int pO1=10;
int pO2=11;
int pO3=12;
int pO4=13;

void setup() {
	Serial.begin(115200);
	lcd1.begin (posXmax,posYmax);
	lcd1.setBacklight(HIGH);
	lcd1.cursor();	//Muestra cursor '_' 
	//lcd1.blink();	  //Pardadea posición.
  pinMode(pO1, OUTPUT);	
  pinMode(pO2, OUTPUT);	
  pinMode(pO3, OUTPUT);	
  pinMode(pO4, OUTPUT);	
}

void loop() {
  char key = keypad.getKey();

  if (key != NO_KEY) {
    Serial.println(key);
		lcd=1;
		if (key=='1') {
			if (O1==0) {
				digitalWrite(pO1,1);
				O1=1;
			}else {
				digitalWrite(pO1,0);
				O1=0;
			}
		  Serial.print("Valor O1: ");
			Serial.println(O1);
		}
		if (key=='2') {
			if (O2==0) {
				digitalWrite(pO2,1);
				O2=1;
			}else {
				digitalWrite(pO2,0);
				O2=0;
			}
		  Serial.print("Valor O2: ");
			Serial.println(O2);
		}
		if (key=='3') {
			if (O3==0) {
				digitalWrite(pO3,1);
				O3=1;
			}else {
				digitalWrite(pO3,0);
				O3=0;
			}
		  Serial.print("Valor O3: ");
			Serial.println(O3);
		}
		if (key=='4') {
			if (O4==0) {
				digitalWrite(pO4,1);
				O4=1;
			}else {
				digitalWrite(pO4,0);
				O4=0;
			}
		  Serial.print("Valor O4: ");
			Serial.println(O4);
		}
		if (key=='.') {
			lcd1.display();
			lcd=0;
		}
		if (key=='=') {
			lcd1.clear();
			digitalWrite(10,LOW);
			lcd=0;
		}
		if (key=='A') {
			lcd1.scrollDisplayLeft();
			lcd=0;
		}
		if (key=='B') {
			lcd1.scrollDisplayRight();
			lcd=0;
		}
		if (key=='C') {
			lcd1.noCursor();
			lcd=0;
		}
		if (key=='D') {
			lcd1.cursor();
			lcd=0;
		}
		if (lcd==1) {
			lcd1.print(key);
			lcd=0;
		}
	}
}

/*void salida1() {
	lcd1.clear();
	lcd1.setCursor(posX,posY);
	//	lcd1.print (posY);
	lcd1.print (cursor);
}

void teclado1() {	//  Derecha   ++
	if (digitalRead(2)==1) x1=1;
	if ((digitalRead(2)==0) & (x1==1)) {
		posX=posX+1;
		if ((posX>posXmax-1) & (posY<posYmax-1)) {
			posY=posY+1;
			posX=0;
		}
		if ((posX>posXmax-1) & (posY==posYmax-1)) {
			posY=0;
			posX=0;
		}
		salida1();
		x1=0;
	}	
}

void teclado2() {	//  Izquierda  --
	if (digitalRead(3)==1) x2=1;
	if ((digitalRead(3)==0) & (x2==1))	{
		posX=posX-1;
		if ((posX<0) & (posY==0)) {
			posY=posYmax-1;
			posX=posXmax-1;
		}
		if ((posX<0) & (posY<=posYmax)) {
			posY=posY-1;
			posX=posXmax-1;
		}
		salida1();
		x2=0;
	}
}
*/
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module