

//
// NeoPixel glas in lood raan of verborgen klok.
//
// Jan5412
//
//
// arduino-1.8.2
//
// versie 1.0 dd 20 februari 2023            // eerste opzet

#include "FastLED.h"                         // Bibliotheek FastLED op versie 3.3.3
#include <DS3231.h>
#include <Wire.h> 

#include <EEPROM.h>                          // Bibliotheek EEPROM-master op versie 2.0
#include <IRremote.h>                        // Bibliotheek Arduino-IRremote-master op versie 2.0.1

#define FASTLED_USE_PROGMEM 1
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
#define NUM_LEDS 141

#define DATA_PIN 4                           // data pin for NeoPixels
#define IR_INTERRUPT  0                      // interrupt number associated with pin 2
#define MosFet_PIN  9                        // output pin to mosfet power for NeoPixels
#define IR_LED  10                           // led die aangaat bij ontvangen van interrupt signaal en uitgaat als IR bediening mogelijk is


CRGB leds[NUM_LEDS];
//CRGB leds_new_time[NUM_LEDS];
CRGB leds_patroon[NUM_LEDS];

CHSV colorStart;                                  // starting color
CHSV colorTarget;                                 // target color
CHSV colorCurrent1 = colorStart;
CHSV colorCurrent2 = colorTarget;


byte BRIGHTNESS = 150;
byte x = 0;
int kleur = 2;


DS3231 clock;
RTCDateTime dt;

bool oldState = HIGH;  // voor switch
// boolean state; // Nodig voor 1 Hz puls

int uren = 0 ;
int minuten = 0 ;

int newTime ;  //opslag van nieuwe seconde
int oldTime ;  //opslag van vorige seconde


byte selectedEffect = 1;                          // case   nummer van effect in main loop 1 t/m 9

bool seconde_led_mult_flag = false;
byte seconde_led_mult = 130;
byte seconde_led = 120;

byte minuten_led =   43;    // variabele voor minuten te tellen
byte minuten_led_prev =   0;    // voorlaatste minuut
byte uren_led    =   17;    // variabele voor uren te tellen
byte uren_led_prev = 0;
byte uur         =   0;

int tijd = 0; //tijdelijk om aantal seconden te tellen


bool clock_actief = true; //false; 
bool clock_actief_color_aan_uit = true;
bool flower_random_patroon = true;

bool update_now= true;



static const byte patroon[2][NUM_LEDS] PROGMEM {                        
                        
                        {132, 232, 232, 132, 232, 232, 132, 232, 232, 132, 232, 232, 132, 232, 232, 132, 232, 232,
                         132, 232, 232, 132, 232, 232, 132, 232, 232, 132, 232, 232, 132, 232, 232, 132, 232, 232,
                                                  
                         192, 036, 192, 036, 192, 036, 192, 036, 192, 036, 192, 036, 192, 036, 192, 036, 192, 036, 192, 036, 192, 036, 192, 036,
                         
                         224, 064, 224, 064, 224, 064, 224, 064, 224, 064, 224, 064, 224, 064, 224, 064, 224, 064, 224, 064, 224, 064, 224, 064,
                         
                         232, 077, 232, 077, 232, 077, 232, 077, 232, 077, 232, 077, 

                         000, 100, 160, 000, 100, 160, 000, 100, 160, 000, 100, 160, 000, 100, 160, 000, 100, 160, 000, 100, 160, 000, 100, 160,
                         
                         000, 132, 000, 132, 000, 132, 000, 132, 000, 132, 000, 132,

                         192, 064, 192, 064, 192, 064, 192, 064,

                         000, },



                        {064, 224, 064, 224, 064, 224, 064, 224, 064, 224, 064, 224, 064, 224, 064, 224, 064, 224, 
                         064, 224, 064, 224, 064, 224, 064, 224, 064, 224, 064, 224, 064, 224, 064, 224, 064, 224, 



                                                  

                         192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 

                         047, 151, 047, 151, 047, 151, 047, 151, 047, 151, 047, 151, 047, 151, 047, 151, 047, 151, 047, 151, 047, 151, 047, 151,

                         
                         232, 077, 232, 077, 232, 077, 232, 077, 232, 077, 232, 077, 


                         047, 151, 047, 151, 047, 151, 047, 151, 047, 151, 047, 151, 047, 151, 047, 151, 047, 151, 047, 151, 047, 151, 047, 151, 


                         

                         027, 057, 027, 057, 027, 057, 027, 057, 027, 057, 027, 057, 


                         057, 027, 057, 027, 057, 027, 057, 027, 

                         000, },
                         };



 

static const byte s_leds[NUM_LEDS] PROGMEM {
    84,116,102,52,54,62,2,82,31,88,108,137,121,37,9,99,76,134,29,50,114,103,30,61,26,64,81,3,
    71,33,91,86,94,68,90,97,23,39,27,132,32,45,20,0,12,73,115,85,55,75,44,15,34,93,48,100,128,38,
    60,130,13,51,140,53,124,4,117,41,21,89,56,109,43,24,8,131,127,11,57,49,14,25,122,10,28,70,
    120,22,6,123,79,112,40,59,47,42,74,19,5,36,92,106,118,87,16,129,72,1,111,69,58,98,35,
    63,138,107,46,104,65,119,83,18,125,66,139,77,110,7,67,133,126,17,78,95,136,96,135,80,113,105,101

            };
    




// const byte minuten_volgorde[]                                                                                                  
static const byte minuten_volgorde[] PROGMEM =
                              {84, 60,  96,  97, 61, 
                               85, 62,  98,  99, 63, 
                               86, 64, 100, 101, 65, 
                               87, 66, 102, 103, 67, 
                               88, 68, 104, 105, 69, 
                               89, 70, 106, 107, 71, 
                               90, 72, 108, 109, 73, 
                               91, 74, 110, 111, 75, 
                               92, 76, 112, 113, 77, 
                               93, 78, 114, 115, 79, 
                               94, 80, 116, 117, 81, 
                               95, 82, 118, 119, 83  };

// const byte uren_volgorde   []
static const byte uren_volgorde[] PROGMEM =
                              { 0,  1, 36,37,  2, 
                                3,  4, 38,39,  5, 
                                6,  7, 40,41,  8, 
                                9, 10, 42,43, 11, 
                               12, 13, 44,45, 14, 
                               15, 16, 46,47, 17, 
                               18, 19, 48,49, 20, 
                               21, 22, 50,51, 23, 
                               24, 25, 52,53, 26, 
                               27, 28, 54,55, 29, 
                               30, 31, 56,57, 32, 
                               33, 34, 58,59, 35  };



byte last_animations[141];                         // array van 141 waarden

byte colors[] {0, 194, 251, 1, 41, 236, 118, 95, 55, 183, 190, 141, 79, 96, 121, 149, 96, 85, 204, 32, 132, 171, 108, 248, 169, 96, 66, 163, 255, 113, 183, 244};        

byte free_colors[4][32];

static const byte fix_colors[17][32] PROGMEM { 

{3, 166, 161, 68, 151, 149, 71, 32, 218, 192, 45, 157, 165, 159, 57, 232, 118, 206, 60, 40, 169, 204, 251, 31, 23, 55, 154, 107, 255, 227, 7, 240},

{3, 166, 161, 68, 23, 55, 154, 107, 118, 206, 60, 40, 151, 149, 71, 32, 165, 159, 57, 232, 169, 204, 251, 31, 218, 192, 45, 157, 255, 227, 7, 240},

{71, 238, 229, 137, 115, 134, 193, 171, 185, 11, 135, 107, 219, 234, 128, 123, 233, 242, 141, 56, 233, 26, 69, 82, 54, 7, 101, 244, 255, 51, 100, 56},

{79, 252, 244, 155, 143, 154, 209, 189, 196, 31, 148, 121, 237, 254, 139, 140, 245, 248, 164, 70, 245, 54, 79, 99, 78, 19, 114, 3, 255, 74, 123, 73},

{89, 20, 13, 162, 164, 170, 222, 208, 213, 55, 167, 141, 252, 9, 159, 159, 13, 11, 176, 91, 3, 65, 95, 119, 103, 36, 128, 10, 255, 89, 150, 94},

{207, 176, 191, 53, 49, 49, 112, 91, 97, 185, 67, 26, 171, 173, 47, 47, 141, 156, 75, 11, 171, 246, 232, 40, 255, 230, 241, 165, 255, 245, 11, 223},

{25, 207, 4, 29, 28, 254, 147, 101, 62, 199, 202, 159, 90, 121, 132, 165, 119, 100, 222, 60, 153, 176, 119, 12, 163, 207, 214, 164, 255, 198, 213, 157},

{218, 122, 236, 8, 250, 220, 115, 79, 39, 158, 136, 123, 61, 58, 111, 111, 105, 66, 187, 17, 151, 111, 71, 234, 78, 167, 153, 120, 255, 158, 157, 103},

{231, 140, 250, 33, 13, 230, 133, 91, 52, 183, 151, 143, 81, 65, 125, 123, 112, 82, 206, 22, 164, 119, 97, 0, 98, 182, 177, 141, 255, 167, 164, 112},

{237, 134, 208, 16, 231, 185, 146, 63, 37, 169, 198, 99, 33, 65, 97, 122, 67, 62, 166, 0, 134, 131, 86, 208, 97, 145, 153, 137, 255, 150, 189, 105},

{104, 243, 67, 130, 98, 56, 1, 178, 152, 8, 64, 174, 149, 182, 179, 200, 165, 172, 1, 126, 220, 239, 171, 31, 221, 218, 15, 226, 255, 11, 24, 221},

{14, 187, 246, 51, 246, 231, 171, 91, 79, 165, 41, 54, 60, 108, 74, 121, 38, 82, 199, 54, 86, 109, 104, 237, 135, 103, 183, 149, 255, 145, 188, 137},

{46, 177, 236, 55, 18, 225, 118, 119, 111, 195, 113, 14, 48, 145, 110, 122, 26, 93, 190, 26, 69, 166, 135, 19, 139, 169, 203, 160, 255, 172, 187, 208},

{55, 201, 12, 86, 47, 6, 160, 151, 132, 220, 144, 26, 62, 171, 129, 151, 53, 120, 221, 60, 89, 182, 154, 35, 174, 187, 230, 183, 255, 196, 221, 216},

{202, 92, 178, 5, 213, 142, 72, 11, 1, 127, 80, 157, 231, 76, 29, 20, 240, 244, 126, 216, 220, 68, 56, 165, 83, 98, 95, 60, 255, 78, 101, 99},

{195, 8, 4, 21, 43, 26, 27, 226, 28, 9, 3, 4, 199, 35, 7, 47, 67, 4, 34, 7, 232, 236, 195, 196, 229, 50, 20, 26, 255, 42, 5, 19},

{96, 249, 87, 149, 109, 49, 228, 176, 159, 31, 10, 44, 144, 248, 232, 200, 158, 162, 52, 127, 126, 221, 189, 36, 9, 12, 8, 231, 255, 3, 11, 233},

};






//-------------------------------------------------
//    Define your own buttons here for SAMSUNG code
//-------------------------------------------------

#define button_r    0x898943BC        // button >  => 
#define button_fw   0x898941BE        // button /\ => 
#define button_l    0x898944BB        // button <  => 
#define button_rv   0x898942BD        // button \/ => 
#define button_ok   0x898940BF        // button ok => 

#define button_1    0x898951AE        // button 1 => Triangles
#define button_2    0x898952AD        // button 2 => ColorWavesWithPalettes
#define button_3    0x898953AC        // button 3 => smiley
#define button_4    0x898954AB        // button 4 => ChangePalettePeriodically
#define button_5    0x898955AA        // button 5 => full_rainbow_color
#define button_6    0x898956A9        // button 6 => chaos
#define button_7    0x898957A8        // button 7 => Fire2012WithPalette
#define button_8    0x898958A7        // button 8 => FixBeeld
#define button_9    0x898959A6        // button 9 => juggle
#define button_0    0x898950AF        // button 0 => algemeen instellingen
//#define button_11   0x898941BE      // button 11 => 

//#define herhaal_c   0xFFFFFFFF      // geen buttom maar herhaal code
  
//--------------------------------------------
//    End define your own buttons
//--------------------------------------------

volatile bool IRcontrol = false;   // for switch

const int IR_Pin = 2;
//const int Radar_Pin = 3;

/*-----( Declare objects )-----*/
IRrecv irrecv(IR_Pin);             // create instance of 'irrecv'
decode_results results;            // create instance of 'decode_results'
unsigned long lastCode;            // stores last code received


// Forward declarations of an array of cpt-city gradient palettes, and 
// a count of how many there are.  The actual color palette definitions
// are at the bottom of this file.
extern const TProgmemRGBGradientPalettePtr gGradientPalettes[];
extern const uint8_t gGradientPaletteCount;                              // Count of how many cpt-city gradients are defined



CRGBPalette16 gCurrentPalette( CRGB::Black);                            // CRGB::Black = CRGB(0,0,0)
//CRGBPalette16 gTargetPalette( gGradientPalettes[0] );

TBlendType    currentBlending;


//bool flower_flag = false; //true;
//byte selected_beeld_effect = 2;

//--------------------------------
//  *** begin void setup ***
//--------------------------------

void setup(){
  
  Serial.begin(9600);
  
  delay(3000); // 3 second delay for recovery

// **************************************************************
// ********** tbv DS3231 Initialize begin ***********************
// **************************************************************

  // Initialize DS3231
  Serial.println("Initialize DS3231");
  clock.begin();

  delay(1000); // 1 second delay for recovery

  //          Set sketch compiling time
  // clock.setDateTime(__DATE__, __TIME__);   // Deze regel zet de computertijd naar de DS3231, de tijd loop dan 6 sec achter
                                              // Na update nogmaals uitvoeren omdat anders bij openen van de seriele monitor de tijd weer wordt aangepast naar de laast ingelezen tijd

  //           Manual (YYYY, MM, DD, HH, II, SS
  //   clock.setDateTime(2021, 12, 12, 23, 44, 15);    // Tijd handmatig zetten naar de DS3231, plus 10 sec om juiste tijd te krijgen

  // Enable 1 Hz puls

  //  clock.enable32kHz(false);         // disable 32kHz
  //  clock.setOutput(DS3231_1HZ);      // Select output as rate to 1Hz
  //  clock.enableOutput(true);         // Start 1 Hz puls
  //  clock.enableOutput(false);        // Start 1 Hz puls

  dt = clock.getDateTime();


  oldTime= dt.second  ;  //opslag van vorige seconde

  // For leading zero look to DS3231_dateformat example
///*
  Serial.print("De tijd is :  ");
  Serial.print(dt.year);   Serial.print("-");     // zonder line return
  Serial.print(dt.month);  Serial.print("-");
  Serial.print(dt.day);    Serial.print(" ");
  Serial.print(dt.hour);   Serial.print(":");
  Serial.print(dt.minute); Serial.print(":");
  Serial.print(dt.second); Serial.println("");    // met line return
//*/

// **************************************************************
// ********** tbv DS3231 Initialize begin ***********************
// **************************************************************


//  tell FastLED about the LED strip configuration -- een output voor alle leds
  FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalSMD5050); //.setCorrection(TypicalLEDStrip); 

  FastLED.setBrightness(BRIGHTNESS);
//  set_max_power_in_volts_and_milliamps(5, 1000); // FastLED Power management set at 5V, 1000 mA.  
  FastLED.setMaxPowerInVoltsAndMilliamps(5,1000); // FastLED Power management set at 5V, 1000 mA.  


  pinMode(MosFet_PIN, OUTPUT);                    // configure MosFet_PIN as an output.


  digitalWrite(MosFet_PIN, HIGH);                 // turn MosFet on what set the power to the NeoPixels on.


  randomSeed (analogRead(0));
  delay(50);
  random16_add_entropy(random());           // om random8 van FastLED meer random te maken

  new_color();                              // geeft nieuwe kleur voor colorTarget en colorStart

  FastLED.clear();                          // fill_solid( leds, NUM_LEDS, CRGB::Black);

  delay(10);

  showStrip(1);




  attachInterrupt (digitalPinToInterrupt (IR_Pin), Interrupt_Handler, LOW);              //set up ISR for receiving IR signal  CHANGE - LOW - RISING
//  attachInterrupt (digitalPinToInterrupt (Radar_Pin), Interrupt_RadarSensor, RISING);    //set up ISR for receiving RadarSensor signal  CHANGE - LOW - RISING
  irrecv.enableIRIn();                                                                   // Start the receiver


newTime= dt.second  ;     //opslag van nieuwe seconde

  Serial.println(F("Setup done.  "));




               make_random_tabel();

               change_flower();




}

//--------------------------------------------
//  *** einde void setup ***
//--------------------------------------------

//  Serial.print("xxxxx  =   ");
//  Serial.println(yyyyy, DEC);

//--------------------------------------------





//----------------------------------------
//  *** begin make_random_tabel nummer ***
//----------------------------------------

void make_random_tabel(){      // tabel random maken


          if (flower_random_patroon) { 

               for (int x = 0; x < 141; x++) {                   // vul het array met 0 t/m 140

                    last_animations[x] = x;

               }
          } else {

               for (int x = 0; x < 150; x++) {                   // hussel het array dmv random array nummers door elkaar

                      byte nummer_1 = random8(0,141);            // random getal nr 1                
                      byte nummer_2 = random8(0,141);            // random getal nr 2                       

                      byte getal_1 = last_animations[nummer_1];
                      byte getal_2 = last_animations[nummer_2];

                      last_animations[nummer_1] = getal_2;
                      last_animations[nummer_2] = getal_1;
               }
          }



        


}

//----------------------------------------
//  *** einde make_random_tabel nummer ***
//----------------------------------------




//-----------------------------
//  *** begin change_flower ***
//-----------------------------

void change_flower(){      // change_flower random

byte a = random8(0,18);

      for (byte i=0; i<32; i++) {
        
             colors[i] = (pgm_read_byte_near( &(fix_colors[a][i])));
      }
          Serial.print(" change_flower  =    "); Serial.println(a);
}

//-----------------------------
//  *** einde change_flower ***
//-----------------------------




//---------------------------------------
//  *** begin void Interrupt_Handler ***
//---------------------------------------

void Interrupt_Handler() {


           IRcontrol = true ;

           detachInterrupt(digitalPinToInterrupt(IR_Pin));  // stop interrupts


   }

//---------------------------------------
//  *** einde void Interrupt_Handler ***
//---------------------------------------




//----------------------------------
//  *** begin control_IR_signal ***
//----------------------------------

void control_IR_signal()
{


// test IR afstandsbediening

          if (IRcontrol) {                                                // have we received an IR signal?            
             
             translateIR();                                               // signaal ontvangen
         }

}

//----------------------------------
//  *** einde control_IR_signal ***
//----------------------------------



//---------------------------------
//  *** begin void translateIR ***
//---------------------------------
                 
void translateIR()     {

    irrecv.enableIRIn();                                                    // Start the receiver
    delay(200);

    results.value = 0xFFFFFFFF;

    

    byte k = 0 ;

                    do  {

                          k = k + 1 ;  

                          irrecv.resume();                                  // receive the next value
                          delay(350);
                          if (irrecv.decode(&results))                      // have we received an IR signal?

                          delay(50);

  Serial.print(F("  code value control   "));   Serial.println(results.value, HEX);

                          if(results.value == button_ok) {                  // als OK dan door anders terug omdat het een onbedoelde code is

                          FastLED.clear();
                          fill_solid( & (leds[132]), 9, colorStart);
                          
                          showStrip(5);                                     // update the display

                          reciveIR();                                       // kiezen welk effect

                          FastLED.clear();

                          update_time();

                          showStrip(5);                                     // update the display

 Serial.print(F(" uit translateIR met selectedEffect =  ")); Serial.println(selectedEffect);




                          k = 20;                                           // end      

                          }
                          




                  }  while (k < 5 );

      update_now= true;                                                         // tevens minuten en uren updaten
      
      IRcontrol = false ;

      EIFR = 0b00000011;

      attachInterrupt (digitalPinToInterrupt (IR_Pin), Interrupt_Handler, LOW); //set up ISR for receiving IR signal

Serial.println(F(" translateIR Done  "));
 
}

//---------------------------------
//  *** einde void translateIR ***
//---------------------------------



//------------------------------
//  *** begin void reciveIR ***
//------------------------------

void reciveIR()  {                                                // takes action based on IR code received

byte selectedEffect_tmp = selectedEffect;                         // om geen effect verandering te krijgen na keuze 0 wat de algemene instellingen is

    irrecv.enableIRIn();                                          // Start the receiver
    delay(200);
    results.value = 0xFFFFFFFF;



 Serial.print(F(" In reciveIR void gekomen  ")); Serial.println(selectedEffect);

bool verlaat_instel = false ;                                     // om uit de while loop te gaan

 
 while ( verlaat_instel == false) {   

                            irrecv.resume();                      // receive the next value
                            delay(350);
                            if (irrecv.decode(&results)) {        // have we received an IR signal?
                            delay(50);
                            
  Serial.print(F("  code case button   "));   Serial.println(results.value, HEX);

  switch(results.value) {


  case button_ok:  Serial.print(" OK => return "); Serial.println(results.value, HEX);

                     IRcontrol = false ;
                     verlaat_instel = true;                   
                     delay(100);
                     break; // einde oefening


  case button_0:  Serial.print(F(" Knop 0 is go to general settings ")); Serial.println(results.value, HEX);
                  
                    IRcontrol = false ;
                    selectedEffect = 0;
                          fill_solid( & (leds[0]),  1, CRGB::Blue);
                          on_off_00(clock_actief, 0);
                          on_off_00(clock_actief_color_aan_uit, 3); // 1 uur van de clock is 3 plaatsen verder dan 0 cq 12 uur
                          on_off_00(flower_random_patroon, 6);      // 2 uur van de clock is 6 plaatsen verder dan 0 cq 12 uur
                          showStrip(5);

                    value_control();
                    selectedEffect = selectedEffect_tmp;                 
                    verlaat_instel = true;
//                    beep(1500);
                    delay(100);                                    
                    break;    
  

  case button_1:  Serial.print(" Knop 1 is case 1 "); Serial.println(results.value, HEX);

                    IRcontrol = false ;
                    selectedEffect = 1;
                          fill_solid( & (leds[3]),  1, CRGB::Blue);
                          showStrip(5);
                    value_control();
                    verlaat_instel = true;
//                    beep(1500);
                    delay(100);                 
                    break;    


  case button_2:  Serial.print(" Knop 2 is case 2 ");
                    IRcontrol = false ;
                    selectedEffect = 2;
//                    value_control();
                    verlaat_instel = true;
//                    beep(1500);
                    delay(100);                   
                    break;    

  case button_3:  Serial.print(" Knop 3 is case 3 ");
                    IRcontrol = false ;
                    selectedEffect = 3;
//                    value_control();
                    verlaat_instel = true;
//                    beep(1500);
                    delay(100);                   
                    break;    

  case button_4: // Serial.print(" Knop 4 is case 4 ");
                    IRcontrol = false ;
                    selectedEffect = 4;
 //                   value_control();
                    verlaat_instel = true;
//                    beep(1500);
                    delay(100);                   
                    break;    


    }
   }
  }
 }

//------------------------------
//  *** einde void reciveIR ***
//------------------------------




//-----------------------------------
//  *** begin void value_control ***
//-----------------------------------

void value_control()  { 


byte selectedEffect_tmp = selectedEffect;                         // om geen effect verandering te krijgen na keuze 0 wat de algemene instellingen is 
Serial.print(selectedEffect);  Serial.println(F("  In value_control void gekomen  "));  
//byte selectedEffect_tmp = selectedEffect;                         // om geen effect verandering te krijgen na keuze 0 wat de algemene instellingen is    
    irrecv.enableIRIn();                                          // Start the receiver
    delay(200);
    results.value = 0xFFFFFFFF;

//    beep(1250);

//    weergeven_instellingen();


bool verlaat_instel = false ;                                     // om uit de while loop te gaan
 
 while ( verlaat_instel == false) {

                            irrecv.resume();                      // receive the next value
                            delay(350);
                            if (irrecv.decode(&results)) {        // have we received an IR signal?
                            delay(50);
                          
  Serial.print(F("  code value control   "));   Serial.println(results.value, HEX);



  if (selectedEffect == 0) {

  switch(results.value) {

           case button_ok:  Serial.print(F(" button ok results clock_actief = ")); Serial.println(results.value, HEX);
                          verlaat_instel = true;
                          update_time();
                          delay(100);
                          break; // einde oefening


           case button_1:  Serial.print(F(" button 1 results clock_actief = ")); Serial.println(results.value, HEX);


                             clock_actief = !clock_actief;
                             on_off_00(clock_actief, 0);


//Serial.print(F(" clock_actief = "));        Serial.println(clock_actief);                      

                          showStrip(5);
                          delay(100);   
                          break; 


           case button_2:  Serial.print(F(" button 2 results clock_actief_color_aan_uit = ")); Serial.println(results.value, HEX);

                           clock_actief_color_aan_uit =!clock_actief_color_aan_uit;
                           on_off_00(clock_actief_color_aan_uit, 3);                      

                      
//Serial.print(F(" clock_actief_color_aan_uit = "));        Serial.println(clock_actief_color_aan_uit);

                          showStrip(5);
                          delay(100);   
                          break; 




           case button_3: // Serial.println(F(" button 3 results = "));           // Serial.println(results.value, HEX);
                      

                          flower_random_patroon =!flower_random_patroon;
                          on_off_00(flower_random_patroon, 6);
                          make_random_tabel();                              
                          showStrip(5);
                          delay(100);   
                          break;



           case button_fw:  Serial.print(F(" button ffw results = "));      Serial.println(results.value, HEX);
                          make_random_tabel();
                          selectedEffect = 1;
                          update_time();
                          showStrip(5);
                          selectedEffect = 0;
                          delay(500); // even wat meer tijd nodig 
                          break;



           case button_rv:  Serial.print(F(" button reverse results = "));        Serial.println(results.value, HEX);
                          change_flower();
                          selectedEffect = 1;
                          update_time();
                          showStrip(5);
                          selectedEffect = 0;
                          delay(500); // even wat meer tijd nodig
                          break;





//***********



   }}



  if (selectedEffect == 1) {


                          fill_solid( & (leds[3]),  1, CRGB::Blue);
                          showStrip(5);
                          delay(100);


  switch(results.value) {


           case button_ok: // Serial.println(F(" button ok results = "));         // Serial.println(results.value, HEX);
                          verlaat_instel = true;
                          update_time();
                          delay(100);
                          break; // einde oefening


           case button_l:  Serial.println(F(" button left results = "));         Serial.println(results.value, HEX);
                          flower_color(16);
//                          fill_solid( & (leds[3]),  1,     CRGB::Blue);
                          update_display();
                          delay(100);
                          break;    


      
       }
     }
     

     
     
     }}}  // einde value control


//--------------------------
//  *** begin on_off_00 ***
//--------------------------

void on_off_00(bool aan_uit, byte nr) { 


                          fill_solid( & (leds[3 + nr]),  1,     CRGB::Blue);

                 if (aan_uit == true ) {
                          fill_solid( & (leds[ 85 + nr/3]),  1, CRGB::Green);
                 }
                 else {
                          fill_solid( & (leds[ 85 + nr/3]),  1, CRGB::Red  );

                 }
                          showStrip(1);
//                          delay(100);  
}

//--------------------------
//  *** einde on_off_00 ***
//--------------------------







//DEFINE_GRADIENT_PALETTE( example_gp ) {
DEFINE_GRADIENT_PALETTE( colors_gp ) {



23, 27, 210, 133,
38, 23, 223, 175,
66, 120, 168, 170,
113, 103, 202, 64,
163, 26, 87, 213,
164, 205, 62, 236,
253, 71, 183, 188,
255, 126, 153, 140};



CRGBPalette16 myPal = colors_gp;  





//--------------------------------
//  *** begin void update time ***
//--------------------------------

void update_time() {


          FastLED.clear();                                   // fill_solid( leds, NUM_LEDS, CRGB::Black);

          dt = clock.getDateTime();                          // nieuwe tijd ophalen

          newTime= dt.second  ;                              // seconde

           if (newTime != oldTime) {  
  
                   if ((newTime == 0) || (update_now)) { 
                        minuten =  dt.minute  ;              // minuten

                        uren =  dt.hour  ;                   // uren
                          if(uren >= 12) {
                            uren -= 12;
                          }
                          
                          if(uren == 12) {
                            uren = 0;

                          }

//  Serial.print(uren);  Serial.print(" : "); Serial.println(minuten);

                        update_now = false;                  // eenmalige update van tijd na aanpassingen via IR
                   
                   }

             oldTime = newTime;
                 
             update_display();
            
           }
}

//--------------------------------
//  *** einde void update time ***
//--------------------------------





//-------------------------
//  *** begin void loop ***
//-------------------------

void loop() {



   if(selectedEffect>9) { 
    selectedEffect=1;
 }

          if (IRcontrol) {                                                // have we received an IR signal?            
             
             translateIR();                                               // signaal ontvangen
         }





    update_time();
}

//--------------------------
//  *** einde void loop ***
//--------------------------
//* memcpy (leds_patroon, leds, sizeof(leds));
//* memcpy (leds, leds_patroon, sizeof(leds));










//-----------------------------------
//  *** begin void update_display ***
//-----------------------------------

void update_display() {


// BRIGHTNESS = 255; //BRIGHTNESS + 25;



byte seconde_led_mult_aan_uit = 0;
byte mod_minuten = 0;
byte rest_seconde = 0;
byte seconde_leds_aan_uit = 0;

byte color_black = 0;




myPal.loadDynamicGradientPalette(colors);

  for (byte i=0; i<NUM_LEDS; i++) {


  switch(selectedEffect) {


                      case 1  : { //Serial.println(F(" 1 "));
                                  uint8_t paletteIndex = map(i,0,NUM_LEDS-1,0,240);                            // index is now based on pixel number
//                                  leds[pgm_read_byte_near( &(s_leds[i]) )] = ColorFromPalette( myPal, paletteIndex, BRIGHTNESS, LINEARBLEND);    // LINEARBLEND  NOBLEND
                                  leds [last_animations[i]] = ColorFromPalette( myPal, paletteIndex, BRIGHTNESS, LINEARBLEND);    // LINEARBLEND  NOBLEND                                  
                                  break;
                                  
                                }
//***
                  
                      case 2  : { //Serial.println(F(" 2 "));
//                                  leds[i] = ColorFromPalette( myPal, (x + sin8(i*16)), BRIGHTNESS, LINEARBLEND);
                                  leds[pgm_read_byte_near( &(s_leds[i]) )] = ColorFromPalette( myPal, (x + sin8(i*16)), BRIGHTNESS, LINEARBLEND);
//                                  leds [last_animations[i]] = ColorFromPalette( myPal, (x + sin8(i*16)), BRIGHTNESS, LINEARBLEND);
                                  break;
//                         leds[pgm_read_byte_near( &(s_leds[i]) )] = CRGB::Black ;   
                                }              


                      case 3  : { //Serial.println(F(" 3 "));
                                  CHSV colorNew = CHSV (pgm_read_byte_near( &(patroon[1][i]) ) + x,255,255); // kleur
                                  leds[i] = colorNew;
                                  break;
                                }              





                       }  // einde switch


        if(selectedEffect == 4) {

                          FastLED.clear();
               for (byte i=0; i<34; i = i +3) {

                                  CHSV colorNew = CHSV(x, 255, 232); // kleur
                                  leds[i] = colorNew;
               }
                         

          
        }


                       
}  // einde for



        if(clock_actief) {                                                      // als clock zichtbaar moet zijn de leds bewerken

              rest_seconde = newTime / 12;                                      // newTime is het aantal seconde
              seconde_leds_aan_uit = seconde_led + newTime - rest_seconde * 12; // Blue or Black


            if (rest_seconde > 0) {
    
                  seconde_led_mult_aan_uit = seconde_led_mult + rest_seconde * 2;   // Yellow or Black
            }


              mod_minuten = minuten % 5;



                    if(clock_actief_color_aan_uit) {                                     // true = color and false = black
        
                          color_black = BRIGHTNESS; // 128; //255;
        
                    } else {
        
                          color_black = 0;
        
                    }


                     leds[seconde_leds_aan_uit]                                                     = CHSV(160,255,color_black);  // Blue

            if (rest_seconde > 0) {
    
                         leds[seconde_led_mult_aan_uit]                                             = CHSV( 54,255,color_black);  // Yellow
                         leds[seconde_led_mult_aan_uit + 1]                                         = CHSV( 54,255,color_black);  // Yellow

            }

 
            if(mod_minuten != 0) {
                
                         leds [(pgm_read_byte_near( &(minuten_volgorde[minuten-1])))]                = CHSV( 92,255,color_black);  // GreenYellow

                 
            }

                     leds [(pgm_read_byte_near( &(minuten_volgorde[minuten])))]                      = CHSV( 96,255,color_black);  // Green
                   
                     leds [(pgm_read_byte_near( &(uren_volgorde[((uren * 5) + (minuten / 12))])))]   = CHSV( 25,255,color_black);  // Oranje
                     leds [(pgm_read_byte_near( &(uren_volgorde[uren * 5])))]                        = CHSV(  0,255,color_black);  // Red;


            }



          x++;                      // voor kleur langzaam te veranderen nog andere naam geven en aan uit zetten en naar select 3
          
             showStrip(1);

} // end

//-----------------------------------
//  *** einde void update_display ***
//-----------------------------------






//----------------------------------
//  *** begin void patroon_color ***
//----------------------------------

void patroon_color(byte patroon_nr) {

      for (int t = 0; t < (NUM_LEDS); t++) {

         CHSV colorNew = CHSV (pgm_read_byte_near( &(patroon[patroon_nr][t]) ) + x,255,255); // kleur en helderheid  // BRIGHTNESS
//         CHSV colorNew = CHSV(random8 (255),255,255); //random8 (255)); // kleur
//           CHSV colorNew = CHSV(t + kleur,255,255); // kleur         
         leds[t] = colorNew;
      
      }

}

//----------------------------------
//  *** einde void patroon_color ***
//----------------------------------



//---------------------------------
//  *** begin void flower_color ***
//---------------------------------

void flower_color(byte max_random) {

//** test

      for (int t = 0; t < 32; t++) {        
      
        colors[t] = colors[t] + random8 ( 1, max_random);  //        colors  [t] = random8 (255);        


        if(t == 28) {

            colors  [t] = 255;
          
        }
  
      Serial.print(" , "); Serial.print(colors[t]);
        
      }



//      Serial.print(", "); Serial.print(255);

      Serial.println("},");

//            attachInterrupt (digitalPinToInterrupt (IR_Pin), Interrupt_Handler, LOW);              //set up ISR for receiving IR signal  CHANGE - LOW - RISING        
          
 }

//---------------------------------
//  *** einde void flower_color ***
//---------------------------------














//--------------------------
//  *** begin showStrip ***
//--------------------------

void showStrip(byte frame_time) { // FastLED

//   show_at_max_brightness_for_power();                         // Power managed display of LED's.   
   FastLED.show();
   FastLED.delay(5 * frame_time);   
}

//--------------------------
//  *** einde showStrip ***
//--------------------------



//--------------------------
//  *** begin new_color ***
//--------------------------

void new_color() {

byte xy = 255;
byte yx = 255;

//  if (color_soft) {
//      xy = random8(96)+159;
//      yx = random8(96)+159;
//  }

  do {

      colorStart  = CHSV(random8(), xy, 255);         // new start   -, -, 255 random maken om iets donkerder kleur te krijgen, maar is niet echt mooier
      colorTarget = CHSV(random8(), yx, 255);         // new target   CHSV(random8(),255,255) voor FastLed random of CHSV(random8(),random8(64)+192,255);
          
  }   while (((abs (colorStart.h - colorTarget.h)) < 32) || ((abs (colorStart.h - colorTarget.h)) > 223));  // kleur verschil minimaal 32
}      

//--------------------------
//  *** einde new_color ***
//--------------------------












