#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
#include <Arduino.h>
#include <IRremote.h>

RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

#define PIN_RECEIVER 9   // Signal Pin of IR receiver
IRrecv receiver(PIN_RECEIVER);

// set the LCD address to 0x27 and for a 20 chars and 4 line display
LiquidCrystal_I2C lcd(0x27,20,4);

const int relay1Pin = 6;
const int relay2Pin = 7;
const int relay3Pin = 8;
const int button1Pin = 3;
const int button2Pin = 4;
const int button3Pin = 5;
const int button4Pin = 10;
int lastState1 = HIGH;
int lastState2 = HIGH;
int lastState3 = HIGH;

void setup() {
  Serial.begin(115200);
  pinMode( relay1Pin, OUTPUT);
  pinMode( relay2Pin, OUTPUT);
  pinMode( relay3Pin, OUTPUT);
  pinMode( button1Pin, INPUT);
  pinMode( button2Pin, INPUT);
  pinMode( button3Pin, INPUT);
  pinMode( button4Pin, INPUT);
  digitalWrite( relay1Pin, LOW);
  digitalWrite( relay2Pin, LOW);
  digitalWrite( relay3Pin, LOW);

if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    abort();
  }
 
 lcd.begin(16, 2);
  lcd.print("<press a button>");
  receiver.enableIRIn(); // Start the receiver
}
  
void loop() 
{
  
 // Checks received an IR signal
  if (receiver.decode()) {
    translateIR();
    receiver.resume();  // Receive the next value
  }
}

void lcdPrint(char* text)
{
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("button pressed:");
  lcd.setCursor(0, 1);
  lcd.print(text);
  lcd.print(" code: ");
  lcd.print(receiver.decodedIRData.command);
}

void translateIR()
{
  // Takes command based on IR code received
  switch (receiver.decodedIRData.command) {
    case 162:
      lcdPrint("POWER");
      break;
    case 226:
      lcdPrint("MENU");
      break;
    case 34:
      lcdPrint("TEST");
      break;
    case 2:
      lcdPrint("PLUS");
      break;
    case 194:
      lcdPrint("BACK");
      break;
    case 224:
      lcdPrint("PREV.");
      break;
    case 168:
      lcdPrint("PLAY");
      break;
    case 144:
      lcdPrint("NEXT");
      break;
    case 104:
      lcdPrint("num: 0");
      break;
    case 152:
      lcdPrint("MINUS");
      break;
    case 176:
      lcdPrint("key: C");
      break;
    case 48:
      lcdPrint("num: 1");
      break;
    case 24:
      lcdPrint("num: 2");
      break;
    case 122:
      lcdPrint("num: 3");
      break;
    case 16:
      lcdPrint("num: 4");
      break;
    case 56:
      lcdPrint("num: 5");
      break;
    case 90:
      lcdPrint("num: 6");
      break;
    case 66:
      lcdPrint("num: 7");
      break;
    case 74:
      lcdPrint("num: 8");
      break;
    case 82:
      lcdPrint("num: 9");
      break;
    default:
      lcd.clear();
      lcd.print(receiver.decodedIRData.command);
      lcd.print(" other button");
  }
}
GND5VSDASCLSQWRTCDS1307+