La Bougie (blinkm via rotary encoder)

VERSION 2.5 PART 2

// Programming code of the functions we use, are below:

// ROTARY BUTTON PRESSED - START
int rotary_button_pressed() {
start_from_powerdown:
  if(rotary.pressed()) {
  Serial.print("\n\n     Rotary Pressed.\n");
  if (my_power == false) { intro(); goto letsbegin;}

keypress_time = millis();     // resetting the keypress_time to current program running time
holding_button:
if ( (millis() - keypress_time) >= my_poweroff_timer ) {  powerdown() ; }
delay(50); // wait a bit so the button is not repeated extremely fast
if (my_power == false) { goto start_from_powerdown; }
if(rotary.pressed()) { goto holding_button; }

    Serial.print("     Current program running time = ");
    Serial.print(millis());
    Serial.print(" msec\n");
      Serial.print("     Time that you pressed the button down = ");
      Serial.print(millis() - keypress_time);
      Serial.print(" msec\n");
// Counting the time the button is being held - STOP

if ( (millis() - keypress_time) <= my_option_timer ) {
   option_switch() ; }                                    // switching between options within the menu if you press between the defined time

if ( (millis() - keypress_time) >= my_option_timer 
  && (millis() - keypress_time) <= my_menu_timer ) {
   menu_switch() ; }                                     // switching between menu's if you press between the defined time

letsbegin:                                      // The intro will send you here at the first time.
if (my_menu == 1)  {
if (my_previous_menu != my_menu) {              // if the previous menu wasn't this menu, reset the option back to 1
    my_option = 1 ;                             // if the previous menu wasn't this menu, reset the option back to 1
    Serial.print("Menu 1\n");
    BlinkM_setFadeSpeed(blinkm_addr, 25);      // fading speed between colours
    my_information(my_menu) ;                   // Informing the user we are at menu 1
    }

if (my_option == 1) { 
    Serial.print("Now controlling brightness\n"); 
    rotary.position(my_brightness); // Giving the rotary the current brightness level back so we can controll it.
    status_update();
    }

if (my_option == 2) { 
    Serial.print("Now controlling hue\n"); 
    rotary.position(my_hue); // Giving the rotary the current Hue level back so we can controll it.
    status_update();
    }
if (my_option == 3) { 
    Serial.print("Now controlling Saturation\n"); 
    rotary.position(my_saturation); // Giving the rotary the current Saturation level back so we can controll it.
    status_update();
    }
    
my_previous_menu = my_menu; // Giving the parameter the new menu nr.
 }


else if (my_menu == 2) {
if (my_previous_menu != my_menu) {
  my_option = 1 ;
  Serial.print("Menu 2\n");
my_information(my_menu); // Informing the user we are at menu 2
  }
if (my_option == 1) { 
    Serial.print("Now going random with brightness controll\n"); 
    rotary.position(my_brightness);               // Giving the rotary the current brightness level back so we can controll it.  
    status_update();
    }

if (my_option == 2) { 
    Serial.print("Now running a default blinkm script\n"); 
    BlinkM_setFadeSpeed(blinkm_addr, 250);                    // For HUE testing purposes I want to change colours fast.
    BlinkM_setTimeAdj(blinkm_addr, 0);                          // Set playspeed to normal
    BlinkM_playScript(blinkm_addr, 12, 0, 0);      // Play pre-programmed script from BlinkM looping forever
    Wire.send('p');                                               // What's this? maybe to synchronize the play?
    status_update();
    }

if (my_option == 3) { 
    Serial.print("Now running a default blinkm script\n"); 
    BlinkM_setFadeSpeed(blinkm_addr, 250);                    // For HUE testing purposes I want to change colours fast.
    BlinkM_setTimeAdj(blinkm_addr, 0);                          // Set playspeed to normal
    BlinkM_playScript(blinkm_addr, 13, 0, 0);      // Play pre-programmed script from BlinkM looping forever
    Wire.send('p');                                               // What's this? maybe to synchronize the play?
    status_update();
    }
    
if (my_option == 4) { 
    Serial.print("Now running a default blinkm script\n"); 
    BlinkM_setFadeSpeed(blinkm_addr, 250);                    // For HUE testing purposes I want to change colours fast.
    BlinkM_setTimeAdj(blinkm_addr, 0);                          // Set playspeed to normal
    BlinkM_playScript(blinkm_addr, 14, 0, 0);      // Play pre-programmed script from BlinkM looping forever
    Wire.send('p');                                               // What's this? maybe to synchronize the play?
    status_update();
    }
    
if (my_option == 5) { 
    Serial.print("Now running a default blinkm script\n"); 
    BlinkM_setFadeSpeed(blinkm_addr, 250);                    // For HUE testing purposes I want to change colours fast.
    BlinkM_setTimeAdj(blinkm_addr, 0);                          // Set playspeed to normal
    BlinkM_playScript(blinkm_addr, 10, 0, 0);      // Play pre-programmed script from BlinkM looping forever
    Wire.send('p');                                               // What's this? maybe to synchronize the play?
    status_update();
    }


my_previous_menu = my_menu;
  }
 }
}
// ROTARY BUTTON PRESSED - STOP


// ACTIONS TO DO WHEN POWER IS ON - START
int actions2do() {
  if (my_power != false) {
 
if (my_menu == 1 ) {
    if (my_option == 1) {  
    my_brightness = rotary.position();                     // The Rotary gives brightness the new value
    hue_val = my_hue;                                       // send the (changed) value to the transmitted value
    sat_val = my_saturation;                                // send the (changed) value to the transmitted value
    bri_val = my_brightness;                               // send the (changed) value to the transmitted value

    BlinkM_fadeToHSB( blinkm_addr, hue_val, sat_val, bri_val );
    }
    if (my_option == 2) {
    my_hue = rotary.position();                           // The Rotary gives hue the new value 
    hue_val = my_hue;                                       // send the (changed) value to the transmitted value
    sat_val = my_saturation;                                // send the (changed) value to the transmitted value
    bri_val = my_brightness;                               // send the (changed) value to the transmitted value
    BlinkM_fadeToHSB( blinkm_addr, hue_val, sat_val, bri_val );
    }
    if (my_option == 3) {
    my_saturation = rotary.position();                  // The Rotary gives saturation the new value 
    hue_val = my_hue;                                       // send the (changed) value to the transmitted value
    sat_val = my_saturation;                                // send the (changed) value to the transmitted value
    bri_val = my_brightness;                               // send the (changed) value to the transmitted value
    BlinkM_fadeToHSB( blinkm_addr, hue_val, sat_val, bri_val );
    }
}


if (my_menu == 2 ) {

   if (my_option == 1) {  
    BlinkM_setFadeSpeed(blinkm_addr, 1);         // Fadespeed to slowest setting possible
    my_brightness = rotary.position();                         // This gives the brightness the new value    
    when_to_randomize_counter++;
    if (when_to_randomize_counter == when_to_randomize) {
    my_hue_random = random(255);
    my_saturation_random = random (255);
    when_to_randomize_counter = 0;
    bri_val = my_brightness;                // read the new brightness variable for transmitting to blinkm
    hue_val = my_hue_random;              // read the new hue variable for transmitting to blinkm
    sat_val = my_saturation_random;       // read the new saturation variable for transmitting to blinkm
    status_update();
    BlinkM_fadeToHSB( blinkm_addr, my_hue_random, my_saturation_random, bri_val );
    // BlinkM_fadeToRandomRGB(blinkm_addr, my_hue_random,my_hue_random, my_hue_random); // Only for testing purposes, because MaxM's don't show the correct HUE colour
     }
    }

 }
} 
}
// ACTIONS TO DO WHEN POWER IS ON - STOP

// CHANGE OPTION - START
int option_switch() {
  BlinkM_stopScript(blinkm_addr);       // Stop playing a predifined blinkm program if a previous menu/option had this running
  if (my_option == menu[my_menu] )      // comment this
        my_option = 1;     // if you are at the last menu, go back to the initial menu number
        else
increase_option_number:
        my_option ++;      // increase the menu number if it's not allready at max.
  Serial.print("Changing to option nr: ");
  Serial.print(my_option);
  Serial.print("\n");  
}
// CHANGE OPTION - STOP


// CHANGE MENU - START
int menu_switch(){
  BlinkM_stopScript(blinkm_addr);       // Stop playing a predifined blinkm program if a previous menu/option had this running
  if (my_menu == 2)      // I only defined 2 menu's, you can add more by increasing this my_menu number)
        my_menu = 1;     // if you are at the last menu, go back to the initial menu number
        else
        my_menu ++;      // increase the menu number if it's not allready at max.
  Serial.print("Changing to menu nr: ");
  Serial.print(my_menu);
  Serial.print("\n");  
}
// CHANGE MENU - STOP