////////////////////////////////////////////////////////////////////////////////
// Official name:     Bolderbot mini IRremote Single run Test 01              //
// Hardware platform: Bolderbot Mini                                          //
// Pin connections:   Arduino Mega 2560                                       //
// Created:           March 2016                                              //
// Created by:        HARB                                                    //
// This is the simplest way of testing your IR receiver, as long as you know  //
// how to replace your IRremote.h                                             //
////////////////////////////////////////////////////////////////////////////////
// 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                                                            //
// A14 = 83 = PK6 ADC14/PCINT22    = Speed encoder Left                       //
// A15 = 82 = PK7 ADC15/PCINT23    = Speed encoder Right                      //
// C00 =  2 = PE0 RXD0/PCINT8      = Serial monitor, also on-board LED    RX0 //
// C01 =  3 = PE1 TXD0             = Serial monitor, also on-board LED    TX0 //
// C18 = 46 = PD2 TXD1/INT3        =                                      TX1 //
// C19 = 45 = PD2 RXD1/INT2        =  IR TV remote control receiver       RX1 //
// C20 = 44 = PD1 SDA/INT1         =                                      TWI //
// C21 = 43 = PD0 SCL/INT0         =                                      TWI //
// D13 = 26 = PB7 OCOA/OC1C/PCINT7 = On board user LED, on=high off=low       //
// D44 = 40 = PL5 OC5C/PWM         = 3 color led Blue                         //
// D45 = 39 = PL4 OC5B/PWM         = 3 color led Red                          //
// D46 = 38 = PL3 OC5A/PWM         = 3 color led Green                        //
// D50 = 22 = PB3 MISO/PCINT3      =                                      SPI //
// D51 = 21 = PB2 MOSI/PCINT2      =                                      SPI //
// D52 = 20 = PB1 SCK/PCINT1       =                                      SPI //
// D53 = 19 = PB1 SS/PCINT0        =                                      SPI //
////////////////////////////////////////////////////////////////////////////////

// SET PRECOMPILER OPTIONS *****************************************************

// Initialse conditional compiling, uncomment to include, comment to exclude ---
// #define RS232 1                 //Include RS232 sections to output debug info
//  #ifdef RS232       //Only include this part if the variable has been defined

// Define precompiler variables ------------------------------------------------
#define    LED               PB7         //Arduino boards contain an onboard LED
// #define F_CPU        16000000                //Speed of the connected crystal
// #define UART0_BAUD      57600                               //UART0 baud rate
// #define TWI_SLA_CPMS03   0xC0                    //The address of the compass
// #define MAX_ITER          200                  //TWI preventing infinite loop
// #define DD_MISO          DDB6                      //Master In, Slave out SPI
// #define DD_MOSI          DDB5                      //Master out, Slave In SPI
// #define DD_SCK           DDB7                              //Serial ClocK SPI
// #define DDR_SPI          DDRB           //Set register data direction for SPI

///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
// #include <avr/io.h>                      //AVR device-specific IO definitions
// #include <util/delay.h>     //Convenience functions for busy-wait delay loops
// #include <stdio.h>                                   //Standard IO facilities
// #include <util/twi.h>                                //TWI handling functions

//DEFINE VARIABLES -------------------------------------------------------------
int RECV_PIN = 19;             //Connection of the IR remote TV control receiver
//3 COLOR LED breakout, common ground,      connect these pins preferably to PWM
//const int ledRed = 44;       //Define to which PWM pin this color is connected
//const int ledGre = 45;       //Define to which PWM pin this color is connected
//const int ledBlu = 46;       //Define to which PWM pin this color is connected

//END OF PRECOMPILER OPTIONS ---------------------------------------------------


void toggle_led(void) { //Toggles the default on-board LED on or off ***********
  if bit_is_clear(PORTB, LED)                           //Test if the LED is off
    PORTB |= (1 << LED);                           //If LED=off then turn LED on
  else                                                 //Else the LED must be on
    PORTB &= ~(1 << LED);                                //Then turn the LED off
} //Exit toggle_led ------------------------------------------------------------


IRrecv irrecv(RECV_PIN);
decode_results results;


void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
}

void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume(); // Receive the next value
  }
  toggle_led();                   //Toggles the default on-board LED on or off
}