#include <Keypad.h>
const uint8_t ROWS = 4;
const uint8_t COLS = 3;
char keys[ROWS][COLS] = {
  { '1', '2', '3'},
  { '4', '5', '6'},
  { '7', '8', '9'},
  { '*', '0', '#'}
};

uint8_t colPins[COLS] = { 5, 4, 3}; // Pinos conectados a C1, C2, C3, C4
uint8_t rowPins[ROWS] = { 9, 8, 7, 6}; // Pinos conectados a R1, R2, R3, R4

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


#define led1 12
#define led2 11
#define led3 10
#define pin1 2

const int potR = A0;
const int potG = A1;
const int potB = A2;

void setup() { 
  pinMode(led1, OUTPUT); 
  pinMode(led2, OUTPUT); 
  pinMode(led3, OUTPUT); 
  pinMode(pin1, OUTPUT);
  pinMode(potR, INPUT);
  pinMode(potG, INPUT);
  pinMode(potB, INPUT);
  Serial.begin(9600);
  }

  int readPot(int pin) {
  return map(analogRead(pin), 0, 1023, 0, 255);
}

void loop() { 

  char key = keypad.getKey();

  if (key != NO_KEY) {
    Serial.println(key);
      
      if(key=='1')
        { 
        digitalWrite(led1, HIGH); 
        digitalWrite(led2, LOW); 
        digitalWrite(led3, LOW); 
        } 
        if(key=='2')
        { 
        digitalWrite(led1, LOW); 
        digitalWrite(led2, HIGH); 
        digitalWrite(led3, LOW); 
        } 
        if(key=='3')
        { 
        digitalWrite(led1, LOW); 
        digitalWrite(led2, LOW); 
        digitalWrite(led3, HIGH); 
        } 
        if(key=='4'||key=='5'||key=='6'||key=='7'||key=='9')
        { 
        digitalWrite(led1, LOW); 
        digitalWrite(led2, LOW); 
        digitalWrite(led3, LOW); 
        } 
        if(key=='0'){
          delay(10);
          digitalWrite(pin1, !digitalRead(pin1));
          Serial.print("pin ");
          Serial.print(pin1);
          Serial.print("= ");
          Serial.println(digitalRead(pin1));
        }
        if(key=='*'|key=='#')
        { 
        digitalWrite(led1, LOW); 
        digitalWrite(led2, LOW); 
        digitalWrite(led3, HIGH);
        delay((1000));
        digitalWrite(led1, HIGH); 
        digitalWrite(led2, HIGH); 
        digitalWrite(led3, LOW);
        delay(1500);
        digitalWrite(led1, HIGH); 
        digitalWrite(led2, HIGH); 
        digitalWrite(led3, HIGH);
        }

        if(key=='8'){
        analogWrite(led1, readPot(potR));
        analogWrite(led2, readPot(potG));
        analogWrite(led3, readPot(potB));
        }
    }
}