////////////////////////////////////////////////////////////////////////////////
// Name:       RO-049                                                         //
//             Hello Wink Mega                                                //
// Platform:   Arduino Mega 2560                                              //
// Created by: HARB rboek2@gmail.com June 2016 GPL copyrights                 //
// http://robotigs.com/robotigs/includes/parts_header.php?idpart=97           //
// This program will wink 3 LED`s with PWM                                    //
////////////////////////////////////////////////////////////////////////////////

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

// Define precompiler settings PINS --------------------------------------------
#define ledRed      44   //3 Color LED, to which PWM pin this Color is connected
#define ledGre      46   //3 Color LED, to which PWM pin this Color is connected
#define ledBlu      45   //3 Color LED, to which PWM pin this Color is connected

// Define user settings --------------------------------------------------------
bool     ledBinVal = LOW;     //You can chose HIGH-on or LOW-off for LED_BUILTIN

uint8_t  Red = 0;           //Brightness of this Color, set by PWM 0=min 255=max
uint8_t  Gre = 0;           //Brightness of this Color, set by PWM 0=min 255=max
uint8_t  Blu = 0;           //Brightness of this Color, set by PWM 0=min 255=max

int Patience = 7;                                        //Program speed setting
//END OF PRECOMPILER OPTIONS ---------------------------------------------------

void setup() { //Setup runs once ***********************************************
  disable_jtag();              //Disable jtag to free port C, enabled by default
    
  pinMode(LED_BUILTIN, OUTPUT);  //Arduino boards contain an onboard LED_BUILTIN
  
  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
  
  toggle_ledBin();                                           //Show we are awake
}//--(end setup )---------------------------------------------------------------



void loop() { //KEEP ON RUNNING THIS LOOP FOREVER ******************************

  for (int Red = 0; Red <= 255; Red++) {       //Dim red from minimum to maximum
    analogWrite(ledRed, Red);                //Set PWM to the desired brightness
    delay(Patience);                   //Here you can train yourself in patience
  }                                                //Now keep on burning a while
  delay(Patience * 10);                //Here you can train yourself in patience
  for (int Red = 255; Red >= 0; Red--) {       //Dim red from maximum to minimum
    analogWrite(ledRed, Red);                //Set PWM to the desired brightness
    delay(Patience);                   //Here you can train yourself in patience
  } //End for (Red = 255; Red >= 0; Red--)       End dim from maximum to minimum

  for (int Gre = 0; Gre <= 255; Gre++) {     //Dim green from minimum to maximum
    analogWrite(ledGre, Gre);                //Set PWM to the desired brightness
    delay(Patience);                   //Here you can train yourself in patience
  }                                                //Now keep on burning a while
  delay(Patience * 10);                //Here you can train yourself in patience
  for (int Gre = 255; Gre >= 0; Gre--) {     //Dim green from maximum to minimum
    analogWrite(ledGre, Gre);                //Set PWM to the desired brightness
    delay(Patience);                   //Here you can train yourself in patience
  } //End for (Gre = 255; Gre >= 0; Gre--)       End dim from maximum to minimum

  for (int Blu = 0; Blu <= 255; Blu++) {      //Dim blue from minimum to maximum
    analogWrite(ledBlu, Blu);                //Set PWM to the desired brightness
    delay(Patience);                   //Here you can train yourself in patience
  }                                                //Now keep on burning a while
  delay(Patience * 10);                //Here you can train yourself in patience
  for (int Blu = 255; Blu >= 0; Blu--) {      //Dim blue from maximum to minimum
    analogWrite(ledBlu, Blu);                //Set PWM to the desired brightness
    delay(Patience);                   //Here you can train yourself in patience
  } //End for (Blu = 255; Blu >= 0; Blu--)       End dim from maximum to minimum

  toggle_ledBin();                  //Toggles the default on-board LED on or off
} //End of void loop()                       //KEEP ON RUNNING THIS LOOP FOREVER
//345678911234567892123456789312345678941234567895123456789612345678971234567898

void disable_jtag(void) { //Disable jtag to free port C, enabled by default ****
#if defined(JTD)                           //Not all AVR controller include jtag
  MCUCR |= ( 1 << JTD );                                //Write twice to disable
  MCUCR |= ( 1 << JTD );                                       //So stutter once
#endif                                            //End of conditional compiling
} //Exit jtag_disable ----------------------------------------------------------


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 ---------------------------------------------------------

////////////////////////////////////////////////////////////////////////////////
// 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                          PWM //
// 6   = 15 = PH3 OC4A             = Motorshield                          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 Red                      PWM //
// 45  = 39 = PL4 OC5B             = 3 Color led Blue                     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