////////////////////////////////////////////////////////////////////////////////
// Official name:     KY-012                                                  //
// Hardware platform: Bolderbot Mini                                          //
// Pin connections:   Arduino Mega 2560                                       //
// Created:           March 2016                                              //
// Created by:        HARB                                                    //
// This program shows a PWM LED dimming while outputing the settings. When    //
// started up it will show the onboard LED burning and the tricolor LED will  //
// fade the red LED and show the Green and Blue LED in a fainted way. You can //
// alter the changing LED in Green or Blue by opening the Serial monitor and  //
// entering a gG or bB or rR. Also the IR remote TV control unit can be used  //
// to switch the light color. 1=red 2=Green 3=Blue. Next stwep is to integrate//
// the engines. 1=Red=Rest, 2=Green=GO, 3=Blue=Backwards. You can also enter  //
// the Serial monitor commands r, g or b. The speed encoders have also been   //
// implemented. You could use the EEPROM to remember the last settings.       //
////////////////////////////////////////////////////////////////////////////////
// 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    =                                          //
// A15 = 82 = PK7 ADC15/PCINT23    =                                          //
// 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         = Speed encoder Left                   TWI //
// C21 = 43 = PD0 SCL/INT0         = Speed encoder Right                  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 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 <AFMotor.h>  //Motors shield, Copyright Adafruit Industries LLC, 2009
//#include <TimerOne.h>    //Currently needed for reading wheel speed per second

//DEFINE VARIABLES -------------------------------------------------------------
//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
const int buzAct = 14;         //Define to which I/O pin this color is connected
int Red = 4;                //Brightness of this color, set by PWM 0=min 255=max
int Gre = 4;                //Brightness of this color, set by PWM 0=min 255=max
int Blu = 4;                //Brightness of this color, set by PWM 0=min 255=max
//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, the old way
    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
  //Or fe:     state = !state;
} //Exit toggle_led ------------------------------------------------------------


void setup() {
  pinMode(ledRed, OUTPUT);                //Make the LED connections output pins
  pinMode(ledGre, OUTPUT);                //Make the LED connections output pins
  pinMode(ledBlu, OUTPUT);                //Make the LED connections output pins
  pinMode(buzAct, OUTPUT);                //Make the LED connections output pins
  DDRB |= (1 << LED);                            //Set onboard LED-pin as output
  PORTB |= (1 << LED);                               //Initally onboard LED = on
  analogWrite(ledRed, Red);   //Set the brightness of this LED and illuminate it
  analogWrite(ledGre, Gre);   //Set the brightness of this LED and illuminate it
  analogWrite(ledBlu, Blu);   //Set the brightness of this LED and illuminate it
} //End of setup


void loop() {                                //KEEP ON RUNNING THIS LOOP FOREVER
  delay(10000);                                               //Wait milliseconds
  toggle_led();                                   //Show I am awake, Hello World
  digitalWrite(buzAct,HIGH);                                    //Turn buzzer on
  delay(5);                                                  //Wait milliseconds
  digitalWrite(buzAct,LOW);        //Turn annoying buzzer off as fast as you can
} //End of void loop()                       //KEEP ON RUNNING THIS LOOP FOREVER
//345678911234567892123456789312345678941234567895123456789612345678971234567898