////////////////////////////////////////////////////////////////////////////////
// Name:       KY-022                                                         //
//             IR remote control receiver                                     //
// Platform:   Arduino Mega 2560                                              //
// Created by: HARB rboek2@gmail.com June 2016 GPL copyrights                 //
// http://robotigs.com/robotigs/includes/parts_header.php?idpart=10           //
// The S-pin must be connected to an interrupt input.                         //
// This program show a pressed key from IR remote control on serial monitor.  //
// Do not forget to replace the original library.                             //
////////////////////////////////////////////////////////////////////////////////

// Define precompiler settings PINS --------------------------------------------
#define RECV_PIN    2          //Connection of the IR remote TV control receiver

// Define the needed header files for the precompiler, no charge if not used ---
#include <IRremote.h>       //Do never use the default by the IDE but replace it

//Set appropriate IR remote control to be used ---------------------------------
#define Keyes                 //Choose IR Remote Control by uncommenting = Keyes

// Define variables ------------------------------------------------------------
bool ledBinVal = LOW;         //You can chose HIGH-on or LOW-off for LED_BUILTIN
byte inByte = 0;                                      //For incoming serial data
bool flagActive = false;           //Has a IR TV remote control key been pressed
byte cmdEngines = 'D';       //The startup colour and the command to the engines

IRrecv irrecv(RECV_PIN);             //Why not in setup, what is the difference?
decode_results results;

#ifdef Keyes                                           //IR Remote Control Keyes
const long butRP = 4294967295UL;     //Any button is continously pressed, REPEAT
const long butUp =   16736925UL;                 //Blue up arrow button, FORWARD
const long butDn =   16754775UL;                  //Blue down arrow button, BACK
const long butLf =   16720605UL;                  //Blue left arrow button, LEFT
const long butRt =   16761405UL;                //Blue right arrow button, RIGHT
const long butOk =   16712445UL;                      //Red OK button, DISENGAGE
const long butMp =   16728765UL;            //Red * (multiply) button, DISENGAGE
const long butNr =   16732845UL;              //Red # (number) button, DISENGAGE
const long but0  =   16730805UL;                 //White 0 button, run program 0
const long but1  =   16738455UL;                 //White 1 button, run program 1
const long but2  =   16750695UL;                 //White 2 button, run program 2
const long but3  =   16756815UL;                 //White 3 button, run program 3
const long but4  =   16724175UL;                 //White 4 button, run program 4
const long but5  =   16718055UL;                 //White 5 button, run program 5
const long but6  =   16743045UL;                 //White 6 button, run program 6
const long but7  =   16716015UL;                 //White 7 button, run program 7
const long but8  =   16726215UL;                 //White 8 button, run program 8
const long but9  =   16734885UL;                 //White 9 button, run program 9
#endif //End of #ifdef Keyes                             IR Remote Control Keyes


void setup() { //Setup runs once ***********************************************
  Serial.begin(9600);   //Nothing more needed for the Serial Monitor to function
  pinMode(LED_BUILTIN, OUTPUT);  //Arduino boards contain an onboard LED_BUILTIN
  irrecv.enableIRIn();                                  // Start the IR receiver
  toggle_ledBin();                                           //Show we are awake
}//--(end setup )---------------------------------------------------------------


void loop() { //KEEP ON RUNNING THIS LOOP FOREVER ******************************  
  if (irrecv.decode(&results)) {      //Check if there is any data in the buffer
    Serial.println(results.value, HEX);                       //Show to the user
    irrecv.resume();                                    //Receive the next value
    toggle_ledBin();            //Toggles the LED_BUILTIN on-board LED on or off
  } //End of if (irrecv.decode(&results))                 Nothing more in buffer
} //End of void loop()                       //KEEP ON RUNNING THIS LOOP FOREVER
//345678911234567892123456789312345678941234567895123456789612345678971234567898


void toggle_ledBin(void) { //Toggles the LED_BUILTIN on-board LED on or off ****
  ledBinVal = !ledBinVal;                                         //Toggle value
  digitalWrite(LED_BUILTIN, ledBinVal);         //Set Arduino boards onboard LED
} //Exit toggle_ledBin ---------------------------------------------------------


void readIRreceiver(void) {           //Receives data from the IR remote control
  if (irrecv.decode(&results)) {  //If I did receive anything then lets check it
    switch (results.value) {        //So what value did I receive from TV remote
      case butRP:                 //Some button is being kept on pressed by user
        flagActive = true; //Engines will be released as soon as key is released
      break;                                    //Jump to the end of this switch
      
      case butUp:                               //User has pressed the button up
        cmdEngines = 'F';          //Set command for the motors to drive FORWARD
        flagActive = true; //Engines will be released as soon as key is released
      break;                                    //Jump to the end of this switch

      case butRt:                            //User has pressed the button right
        cmdEngines = 'R';             //Set command for the motors to turn RIGHT
        flagActive = true; //Engines will be released as soon as key is released
      break;                                    //Jump to the end of this switch

      case butLf:                             //User has pressed the button left
        cmdEngines = 'L';              //Set command for the motors to turn LEFT
       flagActive = true;  //Engines will be released as soon as key is released
      break;                                    //Jump to the end of this switch

      case butDn:                             //User has pressed the button down
        cmdEngines = 'B';          //Set command for the motors to turn BACKWARD
        flagActive = true; //Engines will be released as soon as key is released
      break;                                    //Jump to the end of this switch

      case butOk:                               //User has pressed the OK button
        cmdEngines = 'D';                             //Set command ro DISENGAGE
        flagActive = false;         //Engines will not be automatically released
      break;                                    //Jump to the end of this switch

      case but0:                                 //User has pressed the 0 button
        cmdEngines = '0';                                //Set command ro BREAK1
        flagActive = false;         //Engines will not be automatically released
      break;                                    //Jump to the end of this switch

      case but1:                                 //User has pressed the 1 button
        cmdEngines = '1';                              //Set command ro AUTORUN1
        flagActive = false;         //Engines will not be automatically released
      break;                                    //Jump to the end of this switch
    
      case butMp:            //User has pressed the Star / multiply button PANIC
        cmdEngines = 'P';                  //Set command for the motors to PANIC
        flagActive = false;         //Engines will not be automatically released
      break;                                    //Jump to the end of this switch
 
      default://If button case matched, then this default case will be performed
        cmdEngines = 'D';                  //Set command to the motors DISENGAGE
        break;                                  //Jump to the end of this switch
    } //End of switch (results.value) So what value did I receive from TV remote
    #ifdef RS232       //Only include this part if the variable has been defined
      Serial.println(results.value, DEC);   //Handy to decode the remote control
    #endif //End of      Serial not compiled if precompiler variable was not set
    irrecv.resume();                                   // Receive the next value
  } else {                   //Meaning no key at all is pressed at the TV remote
    if (flagActive == true) {    //Special situation if repeated key was pressed
      cmdEngines = 'D';         //Bring the engines to a halt by using DISENGAGE
      flagActive = false;      //Reset, so we know we already halted the engines
    } //End of (flagActive == true)Special situation if repeated key was pressed
  } //End of if (irrecv.decode(&results)     receive anything then lets check it
} //Exit of readIRreceiver              Receives data from the IR remote control


////////////////////////////////////////////////////////////////////////////////
// FUSES (can always be altered by using the STK500)                          //
// On-Chip Debug Enabled: off                            (OCDEN=0)            //
// JTAG Interface Enabled: off                           (JTAGEN=0)           //
// Preserve EEPROM mem through the Chip Erase cycle: On  (EESAVE = 0)         //
// Boot Flash section = 2048 words, Boot startaddr=$3800 (BOOTSZ=00)          //
// Boot Reset vector Enabled, default address=$0000      (BOOTSTR=0)          //
// CKOPT fuse (operation dependent of CKSEL fuses        (CKOPT=0)            //
// Brown-out detection level at VCC=2,7V;                (BODLEVEL=1)         //
// Ext. Cr/Res High Freq.; Start-up time: 16K CK + 64 ms (CKSEL=1111 SUT=11)  //
// LOCKBITS (are dangerous to change, since they cannot be reset)             //
// Mode 1: No memory lock features enabled                                    //
// Application Protect Mode 1: No lock on SPM and LPM in Application Section  //
// Boot Loader Protect Mode 1: No lock on SPM and LPM in Boot Loader Section  //
////////////////////////////////////////////////////////////////////////////////
// EEPROM MEMORY MAP:                                                         //
// Start End  Number Description                                              //
// 0000  0000      1 Never use this memory location to be AVR compatible      //
////////////////////////////////////////////////////////////////////////////////
// PIN ALLOCATIONS                                                            //
// Prog=List=Chip                                                             //
// A0  = 97 = PF0 ADC0             =                                      ADC //
// A1  = 96 = PF1 ADC1             =                                      ADC //
// A2  = 95 = PF2 ADC2             =                                      ADC //
// A3  = 94 = PF3 ADC3             =                                      ADC //
// A4  = 93 = PF4 ADC4/TMK         =                                      ADC //
// A14 = 83 = PK6 ADC14/PCINT22    = Battery monitor H=1/2 6Vdc pack      ADC //
// A15 = 82 = PK7 ADC15/PCINT23    = Battery monitor H=1/3 9Vdc battery   ADC //
// 0   =  2 = PE0 RXD0/PCINT8      = Serial monitor, also on-board LED    RX0 //
// 1   =  3 = PE1 TXD0             = Serial monitor, also on-board LED    TX0 //
// 2   =  6 = PE4 OC3B/INT4        = IR TV remote control receiver        INT //
// 3   =  7 = PE5 OC3C/INT5        = Motorshield                          INT //
// 4   =  1 = PG5 OCOB             = Motorshield                          PWM //
// 5   =  5 = PE3 OC3A/AIN1        = Motorshield M3 => Free PWM pin       PWM //
// 6   = 15 = PH3 OC4A             = Motorshield M4 => Buzzer             PWM //
// 7   = 16 = PH4 OC4B             = Motorshield                          PWM //
// 8   = 17 = PH5 OC4C             = Motorshield                          PWM //
// 9   = 18 = PH6 OC2B             = Motorshield                          PWM //
// 10  = 23 = PB4 OC2A/PCINT4      = Motorshield                          PWM //
// 11  = 24 = PB5 OC1A/PCINT5      = Motorshield                          PWM //
// 12  = 25 = PB6 OC1B/PCINT6      = Motorshield                          PWM //
// 13  = 26 = PB7 OCOA/OC1C/PCINT7 = On board user LED, on=high off=low   PWM //
// 18  = 46 = PD2 TXD1/INT3        = Speed encoder Right                  TX1 //
// 19  = 45 = PD2 RXD1/INT2        = Speed encoder Left                   RX1 //
// 20  = 44 = PD1 SDA/INT1         = SDA Yellow SRF10                     TWI //
// 21  = 43 = PD0 SCL/INT0         = SCL White  SRF10                     TWI //
// 44  = 40 = PL5 OC5C             = 3 Color led Blue                     PWM //
// 45  = 39 = PL4 OC5B             = 3 Color led Red                      PWM //
// 46  = 38 = PL3 OC5A             = 3 Color led Green                    PWM //
// 50  = 22 = PB3 MISO/PCINT3      =                                      SPI //
// 51  = 21 = PB2 MOSI/PCINT2      =                                      SPI //
// 52  = 20 = PB1 SCK/PCINT1       =                                      SPI //
// 53  = 19 = PB1 SS/PCINT0        =                                      SPI //
////////////////////////////////////////////////////////////////////////////////
//345678911234567892123456789312345678941234567895123456789612345678971234567898