#include <Keypad.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define RED_PIN 10
#define GREEN_PIN 11
#define BLUE_PIN 12
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
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);
struct RGB {
uint8_t red;
uint8_t green;
uint8_t blue;
};
RGB colors[16] = {
{255, 0, 0}, // Rojo
{0, 255, 0}, // Verde
{0, 0, 255}, // Azul
{255, 255, 255}, // blanco
{250, 100, 0 }, //naranja
{128, 0, 255}, //morado
{128, 128, 128},//gris
{102, 0, 0}, //marron
{255,0,255}, //magenta
{128, 0, 0},//vino*
{192, 192, 192},//plata*
{255, 255, 0},//amarillo
{255, 128, 0},//naranja
{0, 255, 255},//cyan*
{0, 128, 128},//vomito*
{255, 153, 153}//piel
};
void setup() {
Serial.begin(9600);
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
// Inicializar la pantalla OLED
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("Error al iniciar la pantalla OLED"));
for(;;);
}
// Limpiar la pantalla
display.clearDisplay();
display.display();
}
void setColor(RGB color) {
analogWrite(RED_PIN, color.red);
analogWrite(GREEN_PIN, color.green);
analogWrite(BLUE_PIN, color.blue);
}
void loop() {
char key = keypad.getKey();
if (key) {
// Mostrar la tecla presionada en la pantalla OLED
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("Tecla presionada:");
display.setTextSize(2);
display.setCursor(0, 16);
display.println(key);
// Obtener el índice de la tecla presionada
int index = -1;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (keys[i][j] == key) {
index = i * COLS + j;
}
}
}
if (index >= 0) {
RGB color = colors[index];
setColor(color);
// Mostrar los valores RGB en la pantalla OLED
display.setTextSize(1);
display.setCursor(0, 40);
display.print("RGB: ");
display.print(color.red);
display.print(", ");
display.print(color.green);
display.print(", ");
display.print(color.blue);
}
display.display();
}
}