Advertisement
Hanneman

Arduino Moodlight revision 2

Jun 11th, 2014
933
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ada 35.50 KB | None | 0 0
  1.     /**
  2.  
  3.     * @file     moodlight.ino
  4.  
  5.     * @author     Hannes Beem
  6.  
  7.     * @date  09-06-2014
  8.  
  9.     *
  10.    
  11.     * @par License info
  12.  
  13.     *
  14.    
  15.     * Copyright (C) 2014  Hannes Beem
  16.    
  17.     *
  18.    
  19.     * This program is free software: you can redistribute it and/or modify
  20.    
  21.     * it under the terms of the GNU General Public License as published by
  22.    
  23.     * the Free Software Foundation, either version 3 of the License, or
  24.    
  25.     * (at your option) any later version.
  26.    
  27.     *
  28.    
  29.     * This program is distributed in the hope that it will be useful,
  30.    
  31.     * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32.    
  33.     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  34.    
  35.     * GNU General Public License for more details.
  36.    
  37.     *
  38.    
  39.     * You should have received a copy of the GNU General Public License
  40.    
  41.     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  42.    
  43.     *
  44.    
  45.     * Program : Mood Light  Copyright (C) 2014  Hannes Beem
  46.    
  47.     * This is free software, and you are welcome to redistribute it.
  48.    
  49.     *
  50.  
  51.     * Thanks to Alexander Brevig for his work on the MenuBackend library.  http://www.arduino.cc/playground/uploads/Profiles/MenuBackend_1-4.zip
  52.  
  53.     * Thanks to rafbuff for his work on the most accurate encoder code found on the Arduino.cc website. http://playground.arduino.cc/Main/RotaryEncoders
  54.  
  55.     * Thanks to transcendtient for his work on the LiquidCrystal library found on the Arduino.cc website. http://playground.arduino.cc/Main/LiquidCrystal
  56.  
  57.     * Thanks to Nico Verduin, shooter, resu from the arduinoforum.nl forum.
  58.  
  59.     * Thanks to PaulS from the Arduino.cc forum.
  60.  
  61.     * Thanks to Michael McRoberts Author of  Beginning Arduino pdf.
  62.    
  63.     ! IMPORTANT: to use the menubackend library by Alexander Brevig download it at http://www.arduino.cc/playground/uploads/Profiles/MenuBackend_1-4.zip and add the next code at line 195
  64.        void toRoot() {
  65.           setCurrent( &getRoot() );
  66.        }
  67.     */
  68.     #include <SPI.h> // Necessary for use with the 74HC595 chip
  69.     #include <MenuBackend.h> // MenuBackend library
  70.     #include <LiquidCrystal.h>  // LCD Display Library for use with 74HC595 chip
  71.     // initialize the library with the number of the sspin
  72.     //(or the latch pin of the 74HC595)
  73.     LiquidCrystal lcd(10);
  74.     //
  75.     // Encoder Definitions
  76.     //
  77.     #define encoderPinA     2   // right
  78.     #define encoderPinB     3   // left
  79.     #define encoderButton   4    // another two pins
  80.     //
  81.     // PIN definitions LEDS
  82.     //
  83.     #define REDLEDPIN       5
  84.     #define GREENLEDPIN     6
  85.     #define BLUELEDPIN      9
  86.     //
  87.     // FSM States
  88.     //
  89.     #define presetleds         0
  90.     #define blinking           1
  91.     #define randommode         2
  92.     #define blinkadjust        3
  93.     #define whiteledstate      4
  94.     #define redledstate        5
  95.     #define greenledstate      6
  96.     #define blueledstate       7
  97.     #define purpleledstate     8
  98.     #define yellowledstate     9
  99.     #define coolblueledstate  10
  100.     #define mintgreenledstate 11
  101.     //
  102.     // Preset colors
  103.     //
  104.     #define whitecolor    12              
  105.     #define purple1color  13                
  106.     #define purple2color  14                  
  107.     #define purple3color  15
  108.     #define blue1color    16              
  109.     #define blue2color    17                  
  110.     #define blue3color    18      
  111.     #define yellow1color  19                  
  112.     #define yellow2color  20          
  113.     #define orange1color  21                  
  114.     #define orange2color  22  
  115.     #define green1color   23
  116.     #define green2color   24
  117.     #define green3color   25
  118.     //
  119.     // Encoder Variables
  120.     //
  121.     volatile unsigned int encoderPos = 30000;   // a counter for the dial
  122.     unsigned int lastReportedPos = 1;           // change management
  123.     static boolean rotating=false;              // debounce management
  124.     int reading;                                // For reading the buttonstate
  125.     int lastButtonState;                        // the previous reading from the input pin
  126.     long lastDebounceTime = 0;                  // the last time the output pin was toggled
  127.     long debounceDelayButton = 50;              // the debounce time; increase if the output flickers
  128.     int buttonState;                            // the current reading from the input pin
  129.     #define holdTime 1000                       // ms hold period: how long to wait for press+hold event
  130.     int buttonVal = 0;                          // value read from button
  131.     int buttonLast = 0;                         // buffered value of the button's previous state
  132.     long btnDnTime;                             // time the button was pressed down
  133.     long btnUpTime;                             // time the button was released
  134.     boolean ignoreUp = false;                   // whether to ignore the button release because the click+hold was triggered
  135.     // interrupt service routine vars
  136.     boolean A_set = false;              
  137.     boolean B_set = false;
  138.     //
  139.     // Variables FSM States
  140.     //
  141.     int moodmodes;                               // Switch variable for the Modes
  142.     int ledstatecolors;                         // Switch variable for the Stroboscope colors
  143.     int presetcolors;                           // Switch variable for the Presetcolors
  144.     //
  145.     // Variables for preset and Strobo
  146.     //
  147.     int ledState = LOW;                         // Set ledstate for Stroboscope to off
  148.     int x;                                      // Variable used in Randommode
  149.     // How much time should the light stay on between delay times (in Micro Seconds)?
  150.     /* Big number = more blur, more perceived brightness
  151.     * Small number = less blur, less perceived brightness */
  152.     long on_time = 500;
  153.     // How much should we multiply the incoming value by?
  154.     /* Big number = less precision, more range
  155.     * Small number = more precision, less range */
  156.     long analog_value_multiplier = 1; // 15 Seems to work well for fast fans =)
  157.     // What should the minimum delay be (in Micro Seconds)?
  158.     // This sets the bottom delay range of the strobe, as a delay of 0 doesn't actually flash =P
  159.     // The strobe starts with this is the "fastest" mode, and goes slower from there, adding to the delay
  160.     long minimum_delay = 5; // 500 (half a millisecond) is a great superfast start
  161.     // Initialize the number to hold our strobe delay. Isn't used till we get to the main loop
  162.     long strobe_delay = 1000;
  163.     //
  164.     // Strobe & Random mode variables
  165.     //
  166.     float RGB1[3];
  167.     float RGB2[3];
  168.     float INC[3];
  169.     float RGB[3];
  170.     int changespeed;   // variable to store the value of the ambient light
  171.     int red, green, blue;
  172.     int brightness = 0;                          // How bright the LED is
  173.     int flashamount = 30;                        // Start value for the blink speed of the Stroboscope
  174.     unsigned long delayCtr    = 0L;              // Used to determin when to do something
  175.     //
  176.     //timers
  177.     //
  178.     unsigned long currentMillis = millis();      // Used to determin when to do something
  179.     long previousMillis = 0;                     // Used to determin when to do something
  180.     //
  181.     // Menu setup
  182.     //
  183.         //this controls the menu backend and the event generation
  184.     MenuBackend menu = MenuBackend(menuUsed,menuChanged);
  185.     //beneath is list of menu items needed to build the menu
  186.  
  187.     MenuItem presetmode          =  MenuItem("Preset Mode     ");
  188.                 MenuItem white       =  MenuItem("1. White        ");
  189.                 MenuItem purple1     =  MenuItem("2. Dark Purple  ");
  190.             MenuItem purple2     =  MenuItem("3. Medium Purple");
  191.                 MenuItem purple3     =  MenuItem("4. Light Purple ");
  192.             MenuItem blue1       =  MenuItem("5. Dark Blue    ");
  193.         MenuItem blue2       =  MenuItem("6. Medium Blue  ");
  194.                 MenuItem blue3       =  MenuItem("7. Light Blue   ");
  195.                 MenuItem yellow1     =  MenuItem("8. Dark Yellow  ");
  196.                 MenuItem yellow2     =  MenuItem("9. Yellow       ");
  197.                 MenuItem orange1     =  MenuItem("10. Dark Orange ");
  198.                 MenuItem orange2     =  MenuItem("11. Orange      ");
  199.                 MenuItem green1      =  MenuItem("12. Bright Green");
  200.                 MenuItem green2      =  MenuItem("13. Medium Green");
  201.                 MenuItem green3      =  MenuItem("14. Light Green ");
  202.     MenuItem strobomode          =  MenuItem("Stroboscope Mode");
  203.                 MenuItem strobespeed =  MenuItem("Adjust Speed?   ");
  204.                     MenuItem flashadjust30   =  MenuItem("Fastest Speed  ");
  205.                     MenuItem flashadjust40   =  MenuItem("S             IF");
  206.                     MenuItem flashadjust50   =  MenuItem("S            I F");
  207.                     MenuItem flashadjust60   =  MenuItem("S           I  F");
  208.                     MenuItem flashadjust70   =  MenuItem("S          I   F");
  209.                     MenuItem flashadjust80   =  MenuItem("S         I    F");
  210.                     MenuItem flashadjust90   =  MenuItem("S        I     F");
  211.                     MenuItem flashadjust100  =  MenuItem("S       I      F");
  212.                     MenuItem flashadjust120  =  MenuItem("S      I       F");
  213.                     MenuItem flashadjust140  =  MenuItem("S     I        F");
  214.                     MenuItem flashadjust160  =  MenuItem("S    I         F");
  215.                     MenuItem flashadjust180  =  MenuItem("S   I          F");
  216.                     MenuItem flashadjust200  =  MenuItem("S  I           F");
  217.                     MenuItem flashadjust220  =  MenuItem("S I            F");
  218.                     MenuItem flashadjust240  =  MenuItem("SI             F");
  219.                     MenuItem flashadjust260  =  MenuItem("Slowest Speed   ");
  220.                     MenuItem strobowhite     =        MenuItem("White           ");
  221.                     MenuItem strobored       =        MenuItem("Red             ");
  222.                     MenuItem strobogreen     =        MenuItem("Green           ");
  223.                     MenuItem stroboblue      =        MenuItem("Blue            ");
  224.                     MenuItem strobopurple    =        MenuItem("Purple          ");
  225.                     MenuItem stroboyellow    =        MenuItem("Yellow          ");
  226.                 MenuItem strobocoolblue  =        MenuItem("Cool Blue       ");
  227.         MenuItem _randommode                 =  MenuItem("Random Mode     ");
  228.                 MenuItem randomspeed         =  MenuItem("Change Speed?   ");
  229.                     MenuItem randomadjust30  =      MenuItem("Fastest Speed   ");
  230.                     MenuItem randomadjust40  =      MenuItem("S             IF");
  231.                     MenuItem randomadjust50  =      MenuItem("S            I F");
  232.                     MenuItem randomadjust60  =      MenuItem("S           I  F");
  233.                     MenuItem randomadjust70  =      MenuItem("S          I   F");
  234.                     MenuItem randomadjust80  =      MenuItem("S         I    F");
  235.                     MenuItem randomadjust90  =      MenuItem("S        I     F");
  236.                     MenuItem randomadjust100 =      MenuItem("S       I      F");
  237.                     MenuItem randomadjust120 =      MenuItem("S      I       F");
  238.                     MenuItem randomadjust140 =      MenuItem("S     I        F");
  239.                     MenuItem randomadjust160 =      MenuItem("S    I         F");
  240.                     MenuItem randomadjust180 =      MenuItem("S   I          F");
  241.                     MenuItem randomadjust200 =      MenuItem("S  I           F");
  242.                     MenuItem randomadjust220 =      MenuItem("S I            F");
  243.                     MenuItem randomadjust240 =      MenuItem("SI             F");
  244.                     MenuItem randomadjust260 =      MenuItem("Slowest Speed   ");
  245.  
  246.     void setup()
  247.     {
  248.     //
  249.     // Encoder Setup
  250.     //
  251.     pinMode(encoderPinA, INPUT);
  252.     pinMode(encoderPinB, INPUT);
  253.     pinMode(encoderButton, INPUT);
  254.     // turn on pullup resistors
  255.     digitalWrite(encoderPinA, HIGH);
  256.     digitalWrite(encoderPinB, HIGH);
  257.     digitalWrite(encoderButton, HIGH);
  258.     // encoder pin on interrupt 0 (pin 2)
  259.     attachInterrupt(0, doEncoderA, CHANGE);
  260.     // encoder pin on interrupt 1 (pin 3)
  261.     attachInterrupt(1, doEncoderB, CHANGE);
  262.     //
  263.     // LED Setup
  264.     //
  265.     pinMode(REDLEDPIN, OUTPUT);
  266.     pinMode(GREENLEDPIN, OUTPUT);
  267.     pinMode(BLUELEDPIN, OUTPUT);
  268.     //
  269.     // LEDS OFF at the start
  270.     //
  271.     analogWrite(REDLEDPIN,   0);
  272.     analogWrite(GREENLEDPIN, 255);
  273.     analogWrite(BLUELEDPIN,  255);
  274.     //
  275.     // LCD Menu Setup
  276.     //
  277.     lcd.begin(16, 2);
  278.     lcd.setCursor(0,0);
  279.     lcd.print("Mood Light");
  280.     delay(2000);
  281.     lcd.setCursor(0,1);
  282.     lcd.print("Turn Encoder    ");
  283.     }  
  284.    
  285.     void loop()
  286.     {
  287.     moodlightModes();            // FSM moodlight Modes loop
  288.     menuSetup();                 // Menu loop
  289.     readButtons();               // Read buttons and encoder turns status loop
  290.     //
  291.     // Encoder
  292.     //
  293.     rotating = true;             // reset the debouncer
  294.     if (lastReportedPos != encoderPos) {
  295.     lastReportedPos = encoderPos;
  296.       }
  297.     if (digitalRead(encoderButton) == LOW )  {
  298.     encoderPos = 30000;
  299.       }
  300.     }
  301.     // Interrupt on A changing state
  302.     void doEncoderA(){
  303.     // debounce
  304.     if ( rotating ) delay (1);  // wait a little until the bouncing is done
  305.     // Test transition, did things really change?
  306.       if( digitalRead(encoderPinA) != A_set ) {  // debounce once more
  307.         A_set = !A_set;
  308.     // adjust counter + if A leads B
  309.     if ( A_set && !B_set )
  310.       encoderPos += 5;              // Adds 5 to 30000, Menu responds bij moving up
  311.         rotating = false;  // no more debouncing until loop() hits again
  312.       }
  313.     }
  314.     // Interrupt on B changing state, same as A above
  315.     void doEncoderB(){
  316.       if ( rotating ) delay (1);
  317.         if( digitalRead(encoderPinB) != B_set ) {
  318.           B_set = !B_set;
  319.     //  adjust counter - 1 if B leads A
  320.     if( B_set && !A_set )
  321.       encoderPos -= 5;            // decrements 5 from 30000, Menu responds bij moving up
  322.         rotating = false;
  323.       }
  324.     }
  325.      
  326.     void menuSetup()
  327.     {
  328.  
  329.     //add the file menu to the menu root
  330.     menu.getRoot().add(presetmode); // 1st is presetmode
  331.  
  332.           // Main Menu setup
  333.                   presetmode.addAfter(strobomode);
  334.                   strobomode.addAfter(_randommode);   // I HAD to use an underscore _ before randommode or I would get an Error message  
  335.                   _randommode.addAfter(presetmode);
  336.                  
  337.                   //setup the presetmode menu item                  
  338.                   presetmode.addRight(white);                      
  339.                         green3.addAfter(white);
  340.                         green2.addAfter(green3);
  341.             green1.addAfter(green2);
  342.                         orange2.addAfter(green1);
  343.                         orange1.addAfter(orange2);
  344.                         yellow2.addAfter(orange1);
  345.                         yellow1.addAfter(yellow2);
  346.                         blue3.addAfter(yellow1);
  347.                         blue2.addAfter(blue3);
  348.                         blue1.addAfter(blue2);
  349.                         purple3.addAfter(blue1);
  350.                         purple2.addAfter(purple3);
  351.                         purple1.addAfter(purple2);
  352.                         white.addAfter(purple1);
  353.                         //we want a left movement to presetmode from anywhere
  354.                         green3.addLeft(presetmode);
  355.                         green2.addLeft(presetmode);
  356.                         green1.addLeft(presetmode);
  357.                         orange2.addLeft(presetmode);
  358.                         orange1.addLeft(presetmode);  
  359.                         yellow2.addLeft(presetmode);  
  360.                         yellow1.addLeft(presetmode);
  361.                         blue3.addLeft(presetmode);
  362.                         blue2.addLeft(presetmode);
  363.                         blue1.addLeft(presetmode);            
  364.                         purple3.addLeft(presetmode);
  365.                         purple2.addLeft(presetmode);
  366.             purple1.addLeft(presetmode);
  367.                         white.addLeft(presetmode);    
  368.                 strobomode.addRight(strobespeed);
  369.                        strobespeed.addRight(flashadjust30);
  370.                           flashadjust40.addAfter(flashadjust30);
  371.                           flashadjust50.addAfter(flashadjust40);
  372.                           flashadjust60.addAfter(flashadjust50);
  373.                           flashadjust70.addAfter(flashadjust60);
  374.                           flashadjust80.addAfter(flashadjust70);
  375.                           flashadjust90.addAfter(flashadjust80);
  376.                           flashadjust100.addAfter(flashadjust90);
  377.                           flashadjust120.addAfter(flashadjust100);
  378.                           flashadjust140.addAfter(flashadjust120);
  379.                           flashadjust160.addAfter(flashadjust140);
  380.                           flashadjust180.addAfter(flashadjust160);
  381.                           flashadjust200.addAfter(flashadjust180);
  382.                           flashadjust220.addAfter(flashadjust200);
  383.                           flashadjust240.addAfter(flashadjust220);
  384.                           flashadjust260.addAfter(flashadjust240);
  385.                         strobespeed.addAfter(strobowhite);  
  386.                         strobowhite.addAfter(strobored);
  387.                         strobored.addAfter(strobogreen);
  388.                         strobogreen.addAfter(stroboblue);
  389.                         stroboblue.addAfter(strobopurple);
  390.                         strobopurple.addAfter(stroboyellow);
  391.                         stroboyellow.addAfter(strobocoolblue);
  392.                         strobocoolblue.addAfter(strobespeed);
  393.                         // we want a left movement to strobomode from anywhere
  394.                             strobocoolblue.addLeft(strobomode);
  395.                             stroboyellow.addLeft(strobomode);
  396.                             strobopurple.addLeft(strobomode);
  397.                             stroboblue.addLeft(strobomode);
  398.                             strobogreen.addLeft(strobomode);
  399.                             strobored.addLeft(strobomode);
  400.                             strobowhite.addLeft(strobomode);
  401.                         flashadjust260.addLeft(strobespeed);
  402.                         flashadjust240.addLeft(strobespeed);
  403.                         flashadjust220.addLeft(strobespeed);
  404.                         flashadjust200.addLeft(strobespeed);
  405.                         flashadjust180.addLeft(strobespeed);
  406.                         flashadjust160.addLeft(strobespeed);
  407.                         flashadjust140.addLeft(strobespeed);
  408.                         flashadjust120.addLeft(strobespeed);
  409.                         flashadjust100.addLeft(strobespeed);
  410.                         flashadjust90.addLeft(strobespeed);
  411.                         flashadjust80.addLeft(strobespeed);
  412.                         flashadjust70.addLeft(strobespeed);
  413.                         flashadjust60.addLeft(strobespeed);
  414.                         flashadjust50.addLeft(strobespeed);
  415.                         flashadjust40.addLeft(strobespeed);
  416.                         flashadjust30.addLeft(strobespeed);
  417.                         strobespeed.addLeft(strobomode);            
  418.                 _randommode.addRight(randomspeed);
  419.                     randomspeed.addRight(randomadjust30);
  420.                           randomadjust260.addAfter(randomadjust240);
  421.                           randomadjust240.addAfter(randomadjust220);
  422.                           randomadjust220.addAfter(randomadjust200);
  423.                           randomadjust200.addAfter(randomadjust180);
  424.                           randomadjust180.addAfter(randomadjust160);
  425.                           randomadjust160.addAfter(randomadjust140);
  426.                           randomadjust140.addAfter(randomadjust120);
  427.                           randomadjust120.addAfter(randomadjust100);
  428.                           randomadjust100.addAfter(randomadjust90);
  429.                           randomadjust90.addAfter(randomadjust80);
  430.                           randomadjust80.addAfter(randomadjust70);
  431.                           randomadjust70.addAfter(randomadjust60);
  432.                           randomadjust60.addAfter(randomadjust50);
  433.                           randomadjust50.addAfter(randomadjust40);
  434.                           randomadjust40.addAfter(randomadjust30);                          
  435.                         //we want a left movement to randommode from anywhere
  436.                         randomadjust260.addLeft(_randommode);
  437.                         randomadjust240.addLeft(_randommode);
  438.                         randomadjust220.addLeft(_randommode);
  439.                         randomadjust200.addLeft(_randommode);
  440.                         randomadjust180.addLeft(_randommode);
  441.                         randomadjust160.addLeft(_randommode);
  442.                         randomadjust140.addLeft(_randommode);
  443.                         randomadjust120.addLeft(_randommode);
  444.                         randomadjust100.addLeft(_randommode);
  445.                         randomadjust90.addLeft(_randommode);
  446.                         randomadjust80.addLeft(_randommode);
  447.                         randomadjust70.addLeft(_randommode);
  448.                         randomadjust60.addLeft(_randommode);
  449.                         randomadjust50.addLeft(_randommode);
  450.                         randomadjust40.addLeft(_randommode);
  451.                         randomadjust30.addLeft(_randommode);                              
  452.     }
  453.    
  454.     void menuChanged(MenuChangeEvent changed)
  455.     {
  456.        lcd.setCursor(0,0);
  457.        printLine(0, "Menu change     ");
  458.        lcd.setCursor(0,1);
  459.        lcd.print(changed.from.getName());
  460.        lcd.print(" ");
  461.        lcd.setCursor(0,1);
  462.        lcd.print(changed.to.getName());
  463.        
  464.        MenuItem newMenuItem=changed.to; //get the destination menu
  465.           if(newMenuItem.getName()==menu.getRoot()){
  466.              
  467.           }else if(newMenuItem.getName()=="Preset Mode     "){
  468.           }else if(newMenuItem.getName()=="Stroboscope Mode"){    
  469.           }else if(newMenuItem.getName()=="Random Mode     "){    
  470.             }else if(newMenuItem.getName()=="Fastest Speed   "){
  471.               flashamount = 30;
  472.             }else if(newMenuItem.getName()=="S             IF"){
  473.               flashamount = 40;
  474.             }else if(newMenuItem.getName()=="S            I F"){
  475.               flashamount = 50;  
  476.             }else if(newMenuItem.getName()=="S           I  F"){
  477.               flashamount = 60;
  478.             }else if(newMenuItem.getName()=="S          I   F"){
  479.               flashamount = 70;
  480.             }else if(newMenuItem.getName()=="S         I    F"){
  481.               flashamount = 80;
  482.             }else if(newMenuItem.getName()=="S        I     F"){
  483.               flashamount = 100;
  484.             }else if(newMenuItem.getName()=="S       I      F"){
  485.               flashamount = 120;
  486.             }else if(newMenuItem.getName()=="S      I       F"){
  487.               flashamount = 130;  
  488.             }else if(newMenuItem.getName()=="S     I        F"){
  489.               flashamount = 140;  
  490.             }else if(newMenuItem.getName()=="S    I         F"){
  491.               flashamount = 160;  
  492.             }else if(newMenuItem.getName()=="S   I          F"){
  493.               flashamount = 180;  
  494.             }else if(newMenuItem.getName()=="S  I           F"){
  495.               flashamount = 200;  
  496.             }else if(newMenuItem.getName()=="S I            F"){
  497.               flashamount = 220;
  498.             }else if(newMenuItem.getName()=="SI             F"){
  499.               flashamount = 240;  
  500.             }else if(newMenuItem.getName()=="Slowest Speed   "){
  501.               flashamount = 260;  
  502.               }else if(newMenuItem.getName()=="White           "){
  503.                 ledstatecolors = whiteledstate;
  504.               }else if(newMenuItem.getName()=="Red             "){
  505.                 ledstatecolors = redledstate;
  506.               }else if(newMenuItem.getName()=="Green           "){
  507.                 ledstatecolors = greenledstate;
  508.               }else if(newMenuItem.getName()=="Blue            "){
  509.                 ledstatecolors = blueledstate;
  510.               }else if(newMenuItem.getName()=="Purple          "){
  511.                 ledstatecolors = purpleledstate;
  512.               }else if(newMenuItem.getName()=="Yellow          "){
  513.                 ledstatecolors = yellowledstate;
  514.               }else if(newMenuItem.getName()=="Cool Blue       "){
  515.                 ledstatecolors = coolblueledstate;
  516.                 }else if(newMenuItem.getName()=="1. White        "){
  517.                   presetcolors = whitecolor;
  518.                 }else if(newMenuItem.getName()=="2. Dark Purple  "){
  519.                   presetcolors = purple1color;
  520.                 }else if(newMenuItem.getName()=="3. Medium Purple"){
  521.                   presetcolors = purple2color;
  522.                 }else if(newMenuItem.getName()=="4. Light Purple "){
  523.                   presetcolors = purple3color;
  524.                 }else if(newMenuItem.getName()=="5. Dark Blue    "){
  525.                   presetcolors = blue1color;
  526.                 }else if(newMenuItem.getName()=="6. Medium Blue  "){
  527.                   presetcolors = blue2color;
  528.                 }else if(newMenuItem.getName()=="7. Light Blue   "){
  529.                   presetcolors = blue3color;
  530.                 }else if(newMenuItem.getName()=="8. Dark Yellow  "){
  531.                   presetcolors = yellow1color;
  532.                 }else if(newMenuItem.getName()=="9. Yellow       "){
  533.                   presetcolors = yellow2color;
  534.                 }else if(newMenuItem.getName()=="10. Dark Orange "){
  535.                   presetcolors = orange1color;
  536.                 }else if(newMenuItem.getName()=="11. Orange      "){
  537.                   presetcolors = orange2color;
  538.                 }else if(newMenuItem.getName()=="12. Bright Green"){
  539.                   presetcolors = green1color;
  540.                 }else if(newMenuItem.getName()=="13. Medium Green"){
  541.                   presetcolors = green2color;
  542.                 }else if(newMenuItem.getName()=="14. Light Green "){
  543.                   presetcolors = green3color;
  544.                   }
  545.     }
  546.    
  547.     void menuUsed(MenuUseEvent used)
  548.     {
  549.          lcd.setCursor(0,0);
  550.          lcd.print("Menu use        ");
  551.          lcd.setCursor(0,1);
  552.          lcd.print(used.item.getName());
  553.          //
  554.          // Mode to choose when buttons is held
  555.          //  
  556.          MenuItem choosenMenuItem=used.item;
  557.            if(choosenMenuItem.getName()==menu.getRoot()){
  558.            
  559.              }else if(choosenMenuItem.getName()=="Preset Mode     "){
  560.              
  561.                moodmodes = presetleds;
  562.              
  563.              }else if(choosenMenuItem.getName()=="Stroboscope Mode"){
  564.      
  565.                moodmodes = blinking;
  566.              
  567.              }else if(choosenMenuItem.getName()=="Random Mode     "){
  568.              
  569.                moodmodes = randommode;
  570.              }
  571.     }
  572.    
  573.     void  readButtons(){             //Read buttons and encoder turns status
  574.    
  575.         if (encoderPos <= 29995 ) {
  576.          
  577.             menu.moveDown();
  578.             encoderPos=30000;
  579.            
  580.         } else if (encoderPos >= 30005 ) {
  581.            
  582.             menu.moveUp();
  583.             encoderPos=30000;
  584.            
  585.                 } else {
  586.                   encoderPos=30000;
  587.                 }
  588.    
  589.     // Read the state of the button
  590.     buttonVal = digitalRead(encoderButton);
  591.    
  592.     // Test for button pressed and store the down time
  593.     if (buttonVal == LOW && buttonLast == HIGH && (millis() - btnUpTime) > long(debounceDelayButton))
  594.     {
  595.     btnDnTime = millis();
  596.     }
  597.    
  598.     // Test for button release and store the up time
  599.     if (buttonVal == HIGH && buttonLast == LOW && (millis() - btnDnTime) > long(debounceDelayButton))
  600.     {
  601.     if (ignoreUp == false) {
  602.      
  603.       menu.moveLeft();
  604.    
  605.     }else { ignoreUp = false;
  606.     btnUpTime = millis();
  607.     }
  608.     }
  609.    
  610.     // Test for button held down for longer than the hold time
  611.     if (buttonVal == LOW && (millis() - btnDnTime) > long(holdTime))
  612.     {
  613.      
  614.       menu.use();
  615.       menu.moveRight();
  616.      
  617.     ignoreUp = true;
  618.     btnDnTime = millis();
  619.     }
  620.    
  621.     buttonLast = buttonVal;
  622.     }
  623.    
  624.     void printLine(uint8_t regel, char * text) {
  625.       lcd.setCursor(0, regel);
  626.       lcd.print("                ");
  627.       lcd.setCursor(0, regel);
  628.       lcd.print(text);
  629.     }
  630.    
  631.     //
  632.     // FSM MoodlightModes
  633.     //
  634.     void  moodlightModes() {
  635.            
  636.       switch(moodmodes) {
  637.        
  638.          case presetleds:
  639.          
  640.                  analogWrite(REDLEDPIN,   0);
  641.                  analogWrite(GREENLEDPIN, 0);
  642.                  analogWrite(BLUELEDPIN,  0);
  643.            
  644.              switch(presetcolors) {
  645.                
  646.                  case whitecolor:
  647.                  analogWrite(REDLEDPIN,   255);
  648.                  analogWrite(GREENLEDPIN, 255);
  649.                  analogWrite(BLUELEDPIN,  255);
  650.                  break;
  651.                  case purple1color:
  652.                  analogWrite(REDLEDPIN,   255);
  653.                  analogWrite(GREENLEDPIN, 0);
  654.                  analogWrite(BLUELEDPIN,  255);
  655.                  break;
  656.                  case purple2color:
  657.                  analogWrite(REDLEDPIN,   255);
  658.                  analogWrite(GREENLEDPIN, 25);
  659.                  analogWrite(BLUELEDPIN,  255);
  660.                  case purple3color:
  661.                  analogWrite(REDLEDPIN,   255);
  662.                  analogWrite(GREENLEDPIN, 100);
  663.                  analogWrite(BLUELEDPIN,  255);
  664.                  case blue1color:
  665.                  analogWrite(REDLEDPIN,   0);
  666.                  analogWrite(GREENLEDPIN, 0);
  667.                  analogWrite(BLUELEDPIN,  255);
  668.                  break;
  669.                  case blue2color:
  670.                  analogWrite(REDLEDPIN,   0);
  671.                  analogWrite(GREENLEDPIN, 50);
  672.                  analogWrite(BLUELEDPIN,  255);
  673.                  case blue3color:
  674.                  analogWrite(REDLEDPIN,   255);
  675.                  analogWrite(GREENLEDPIN, 100);
  676.                  analogWrite(BLUELEDPIN,  255);
  677.                  case yellow1color:
  678.                  analogWrite(REDLEDPIN,   255);
  679.                  analogWrite(GREENLEDPIN, 255);
  680.                  analogWrite(BLUELEDPIN,  0);
  681.                  break;
  682.                  case yellow2color:
  683.                  analogWrite(REDLEDPIN,   255);
  684.                  analogWrite(GREENLEDPIN, 100);
  685.                  analogWrite(BLUELEDPIN,  0);
  686.                  break;
  687.                  case orange1color:
  688.                  analogWrite(REDLEDPIN,   255);
  689.                  analogWrite(GREENLEDPIN, 50);
  690.                  analogWrite(BLUELEDPIN,  0);
  691.                  break;
  692.                  case orange2color:
  693.                  analogWrite(REDLEDPIN,   255);
  694.                  analogWrite(GREENLEDPIN, 25);
  695.                  analogWrite(BLUELEDPIN,  0);
  696.                  break;
  697.                  case green1color:
  698.                  analogWrite(REDLEDPIN,   150);
  699.                  analogWrite(GREENLEDPIN, 255);
  700.                  analogWrite(BLUELEDPIN,  10);
  701.                  break;
  702.                  case green2color:
  703.                  analogWrite(REDLEDPIN,   100);
  704.                  analogWrite(GREENLEDPIN, 255);
  705.                  analogWrite(BLUELEDPIN,  10);
  706.                  break;
  707.                  case green3color:
  708.                  analogWrite(REDLEDPIN,   25);
  709.                  analogWrite(GREENLEDPIN, 255);
  710.                  analogWrite(BLUELEDPIN,  0);
  711.                  break;
  712.             }
  713.      
  714.         break;
  715.    
  716.         case blinking:
  717.        
  718.             switch(ledstatecolors) {
  719.              
  720.               case whiteledstate:
  721.                   if (millis() > delayCtr) {
  722.                   analogWrite(REDLEDPIN, ledState);
  723.                   analogWrite(GREENLEDPIN, ledState);
  724.                   analogWrite(BLUELEDPIN, ledState);  
  725.                   }
  726.                   break;
  727.               case redledstate:
  728.                   if (millis() > delayCtr) {
  729.                   analogWrite(REDLEDPIN, ledState);
  730.                   analogWrite(GREENLEDPIN, LOW);
  731.                   analogWrite(BLUELEDPIN, LOW);  
  732.                   }
  733.                   break;
  734.               case greenledstate:
  735.                   if (millis() > delayCtr) {
  736.                   analogWrite(REDLEDPIN, LOW);
  737.                   analogWrite(GREENLEDPIN, ledState);
  738.                   analogWrite(BLUELEDPIN, LOW);  
  739.                   }
  740.                   break;
  741.               case blueledstate:
  742.                   if (millis() > delayCtr) {
  743.                   analogWrite(REDLEDPIN, LOW);
  744.                   analogWrite(GREENLEDPIN, LOW);
  745.                   analogWrite(BLUELEDPIN, ledState);  
  746.                   }
  747.                   break;
  748.               case purpleledstate:
  749.                   if (millis() > delayCtr) {
  750.                   analogWrite(REDLEDPIN, ledState);
  751.                   analogWrite(GREENLEDPIN, LOW);
  752.                   analogWrite(BLUELEDPIN, ledState);  
  753.                   }
  754.                   break;
  755.               case yellowledstate:
  756.                   if (millis() > delayCtr) {
  757.                   analogWrite(REDLEDPIN, ledState);
  758.                   analogWrite(GREENLEDPIN, ledState);
  759.                   analogWrite(BLUELEDPIN, LOW);  
  760.                   }
  761.                   break;
  762.               case coolblueledstate:
  763.                   if (millis() > delayCtr) {
  764.                   analogWrite(REDLEDPIN, LOW);
  765.                   analogWrite(GREENLEDPIN, ledState);
  766.                   analogWrite(BLUELEDPIN, ledState);  
  767.                   }
  768.                   break;        
  769.             }
  770.            
  771.         flashamount = constrain(flashamount, 30, 260);
  772.                  
  773.         strobe_delay = minimum_delay + flashamount * analog_value_multiplier;
  774.        
  775.         currentMillis = millis();
  776.        
  777.         if(currentMillis - previousMillis > strobe_delay) {
  778.         // save the last time you blinked the LED
  779.         previousMillis = currentMillis;
  780.            // if the LED is off turn it on and vice-versa:
  781.         if (ledState == LOW)
  782.         brightness = 255;
  783.          ledState = brightness;
  784.          delayMicroseconds(on_time);
  785.         }
  786.         else
  787.         {
  788.           ledState = LOW;
  789.       }
  790.  
  791.     if (strobe_delay > 10000){
  792.    
  793.         delay((strobe_delay - on_time) / 1000);
  794.    
  795.     }else{
  796.    
  797.       delayMicroseconds(strobe_delay - on_time);
  798.     }
  799.    
  800.     break;
  801.        
  802.       case randommode:
  803.        
  804.         randomSeed(analogRead(6));
  805.         if (millis() > flashamount) {
  806.         currentMillis = millis();
  807.         if(currentMillis - previousMillis > (flashamount/3)) {
  808.         for (int x=0; x<3; x++) {
  809.         INC[x] = (RGB1[x] - RGB2[x]) / 256; }
  810.         for (int x=0; x<256; x++) {
  811.         red = int(RGB1[0]);
  812.         green = int(RGB1[1]);
  813.         blue = int(RGB1[2]);
  814.         analogWrite (REDLEDPIN, red);
  815.         analogWrite (GREENLEDPIN, green);
  816.         analogWrite (BLUELEDPIN, blue);
  817.         if (millis() > (flashamount)){
  818.         if(currentMillis - previousMillis > (flashamount/6)) {
  819.         RGB1[0] -= INC[0];
  820.         RGB1[1] -= INC[1];
  821.         RGB1[2] -= INC[2];
  822.         previousMillis = currentMillis;
  823.             }
  824.           }
  825.         }
  826.         for (int x=0; x<3; x++) {
  827.         RGB2[x] = random(556)-300;
  828.         RGB2[x] = constrain(RGB2[x], 0, 255);
  829.             }
  830.           }
  831.         }
  832.        
  833.  
  834.     break;
  835.    
  836.       }
  837.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement