Advertisement
Hanneman

Arduino Mood Light English

Jun 9th, 2014
974
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ada 36.98 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.     int red, green, blue;
  170.     int brightness = 0;                          // How bright the LED is
  171.     int flashamount = 30;                        // Start value for the blink speed of the Stroboscope
  172.     unsigned long delayCtr    = 0L;              // Used to determin when to do something
  173.     //
  174.     //timers
  175.     //
  176.     unsigned long currentMillis = millis();      // Used to determin when to do something
  177.     long previousMillis = 0;                     // Used to determin when to do something
  178.     //
  179.     // Menu setup
  180.     //
  181.         //this controls the menu backend and the event generation
  182.     MenuBackend menu = MenuBackend(menuUsed,menuChanged);
  183.     //beneath is list of menu items needed to build the menu
  184.  
  185.     MenuItem presetmode          =  MenuItem("Preset Mode     ");
  186.                 MenuItem white       =  MenuItem("1. White        ");
  187.                 MenuItem purple1     =  MenuItem("2. Dark Purple  ");
  188.             MenuItem purple2     =  MenuItem("3. Medium Purple");
  189.                 MenuItem purple3     =  MenuItem("4. Light Purple ");
  190.             MenuItem blue1       =  MenuItem("5. Dark Blue    ");
  191.         MenuItem blue2       =  MenuItem("6. Medium Blue  ");
  192.                 MenuItem blue3       =  MenuItem("7. Light Blue   ");
  193.                 MenuItem yellow1     =  MenuItem("8. Dark Yellow  ");
  194.                 MenuItem yellow2     =  MenuItem("9. Yellow       ");
  195.                 MenuItem orange1     =  MenuItem("10. Dark Orange ");
  196.                 MenuItem orange2     =  MenuItem("11. Orange      ");
  197.                 MenuItem green1      =  MenuItem("12. Bright Green");
  198.                 MenuItem green2      =  MenuItem("13. Medium Green");
  199.                 MenuItem green3      =  MenuItem("14. Light Green ");
  200.     MenuItem strobomode          =  MenuItem("Stroboscope Mode");
  201.                 MenuItem strobespeed =  MenuItem("Adjust Speed?   ");
  202.                     MenuItem flashadjust30   =  MenuItem("Fastest Speed  ");
  203.                     MenuItem flashadjust40   =  MenuItem("S             IF");
  204.                     MenuItem flashadjust50   =  MenuItem("S            I F");
  205.                     MenuItem flashadjust60   =  MenuItem("S           I  F");
  206.                     MenuItem flashadjust70   =  MenuItem("S          I   F");
  207.                     MenuItem flashadjust80   =  MenuItem("S         I    F");
  208.                     MenuItem flashadjust90   =  MenuItem("S        I     F");
  209.                     MenuItem flashadjust100  =  MenuItem("S       I      F");
  210.                     MenuItem flashadjust120  =  MenuItem("S      I       F");
  211.                     MenuItem flashadjust140  =  MenuItem("S     I        F");
  212.                     MenuItem flashadjust160  =  MenuItem("S    I         F");
  213.                     MenuItem flashadjust180  =  MenuItem("S   I          F");
  214.                     MenuItem flashadjust200  =  MenuItem("S  I           F");
  215.                     MenuItem flashadjust220  =  MenuItem("S I            F");
  216.                     MenuItem flashadjust240  =  MenuItem("SI             F");
  217.                     MenuItem flashadjust260  =  MenuItem("Slowest Speed   ");
  218.                     MenuItem strobowhite     =        MenuItem("White           ");
  219.                     MenuItem strobored       =        MenuItem("Red             ");
  220.                     MenuItem strobogreen     =        MenuItem("Green           ");
  221.                     MenuItem stroboblue      =        MenuItem("Blue            ");
  222.                     MenuItem strobopurple    =        MenuItem("Purple          ");
  223.                     MenuItem stroboyellow    =        MenuItem("Yellow          ");
  224.                 MenuItem strobocoolblue  =        MenuItem("Cool Blue       ");
  225.         MenuItem _randommode                 =  MenuItem("Random Mode     ");
  226.                 MenuItem randomspeed         =  MenuItem("Change Speed?   ");
  227.                     MenuItem randomadjust30  =      MenuItem("Fastest Speed   ");
  228.                     MenuItem randomadjust40  =      MenuItem("S             IF");
  229.                     MenuItem randomadjust50  =      MenuItem("S            I F");
  230.                     MenuItem randomadjust60  =      MenuItem("S           I  F");
  231.                     MenuItem randomadjust70  =      MenuItem("S          I   F");
  232.                     MenuItem randomadjust80  =      MenuItem("S         I    F");
  233.                     MenuItem randomadjust90  =      MenuItem("S        I     F");
  234.                     MenuItem randomadjust100 =      MenuItem("S       I      F");
  235.                     MenuItem randomadjust120 =      MenuItem("S      I       F");
  236.                     MenuItem randomadjust140 =      MenuItem("S     I        F");
  237.                     MenuItem randomadjust160 =      MenuItem("S    I         F");
  238.                     MenuItem randomadjust180 =      MenuItem("S   I          F");
  239.                     MenuItem randomadjust200 =      MenuItem("S  I           F");
  240.                     MenuItem randomadjust220 =      MenuItem("S I            F");
  241.                     MenuItem randomadjust240 =      MenuItem("SI             F");
  242.                     MenuItem randomadjust260 =      MenuItem("Slowest Speed   ");
  243.  
  244.     void setup()
  245.     {
  246.     //
  247.     // Encoder Setup
  248.     //
  249.     pinMode(encoderPinA, INPUT);
  250.     pinMode(encoderPinB, INPUT);
  251.     pinMode(encoderButton, INPUT);
  252.     // turn on pullup resistors
  253.     digitalWrite(encoderPinA, HIGH);
  254.     digitalWrite(encoderPinB, HIGH);
  255.     digitalWrite(encoderButton, HIGH);
  256.     // encoder pin on interrupt 0 (pin 2)
  257.     attachInterrupt(0, doEncoderA, CHANGE);
  258.     // encoder pin on interrupt 1 (pin 3)
  259.     attachInterrupt(1, doEncoderB, CHANGE);
  260.     //
  261.     // LED Setup
  262.     //
  263.     pinMode(REDLEDPIN, OUTPUT);
  264.     pinMode(GREENLEDPIN, OUTPUT);
  265.     pinMode(BLUELEDPIN, OUTPUT);
  266.     //
  267.     // LEDS OFF at the start
  268.     //
  269.     analogWrite(REDLEDPIN,   0);
  270.     analogWrite(GREENLEDPIN, 255);
  271.     analogWrite(BLUELEDPIN,  255);
  272.     //
  273.     // LCD Menu Setup
  274.     //
  275.     lcd.begin(16, 2);
  276.     lcd.setCursor(0,0);
  277.     lcd.print("Mood Light");
  278.     delay(2000);
  279.     lcd.setCursor(0,1);
  280.     lcd.print("Turn Encoder    ");
  281.     }  
  282.    
  283.     void loop()
  284.     {
  285.     moodlightModes();            // FSM moodlight Modes loop
  286.     menuSetup();                 // Menu loop
  287.     readButtons();               // Read buttons and encoder turns status loop
  288.     //
  289.     // Encoder
  290.     //
  291.     rotating = true;             // reset the debouncer
  292.     if (lastReportedPos != encoderPos) {
  293.     lastReportedPos = encoderPos;
  294.       }
  295.     if (digitalRead(encoderButton) == LOW )  {
  296.     encoderPos = 30000;
  297.       }
  298.     }
  299.     // Interrupt on A changing state
  300.     void doEncoderA(){
  301.     // debounce
  302.     if ( rotating ) delay (1);  // wait a little until the bouncing is done
  303.     // Test transition, did things really change?
  304.       if( digitalRead(encoderPinA) != A_set ) {  // debounce once more
  305.         A_set = !A_set;
  306.     // adjust counter + if A leads B
  307.     if ( A_set && !B_set )
  308.       encoderPos += 5;              // Adds 5 to 30000, Menu responds bij moving up
  309.         rotating = false;  // no more debouncing until loop() hits again
  310.       }
  311.     }
  312.     // Interrupt on B changing state, same as A above
  313.     void doEncoderB(){
  314.       if ( rotating ) delay (1);
  315.         if( digitalRead(encoderPinB) != B_set ) {
  316.           B_set = !B_set;
  317.     //  adjust counter - 1 if B leads A
  318.     if( B_set && !A_set )
  319.       encoderPos -= 5;            // decrements 5 from 30000, Menu responds bij moving up
  320.         rotating = false;
  321.       }
  322.     }
  323.      
  324.     void menuSetup()
  325.     {
  326.  
  327.     //add the file menu to the menu root
  328.     menu.getRoot().add(presetmode);
  329.         //setup the presetmode menu item
  330.                   presetmode.addRight(white);
  331.                 //we want looping both up and down
  332.             white.addBefore(green3);
  333.                         white.addAfter(purple1);
  334.                         purple1.addBefore(white);
  335.             purple1.addAfter(purple2);
  336.                         purple2.addBefore(purple1);
  337.             purple2.addAfter(purple3);
  338.                         purple3.addBefore(purple2);
  339.                         purple3.addAfter(blue1);
  340.                         blue1.addBefore(purple3);
  341.                         blue1.addAfter(blue2);
  342.                         blue2.addBefore(blue1);
  343.                         blue2.addAfter(blue3);
  344.                         blue3.addBefore(blue2);
  345.                         blue3.addAfter(yellow1);
  346.                         yellow1.addBefore(blue3);
  347.                         yellow1.addAfter(yellow2);
  348.                         yellow2.addBefore(yellow1);
  349.                         yellow2.addAfter(orange1);
  350.                         orange1.addBefore(yellow2);
  351.                         orange1.addAfter(orange2);
  352.                         orange2.addBefore(orange1);
  353.                         orange2.addAfter(green1);
  354.                         green1.addBefore(orange2);
  355.                         green1.addAfter(green2);
  356.                         green2.addBefore(green1);
  357.                         green2.addAfter(green3);
  358.                         green3.addBefore(green2);
  359.                         green3.addAfter(white);
  360.                         //we want a left movement to pin to presetmode from anywhere
  361.             white.addLeft(presetmode);
  362.             purple1.addLeft(presetmode);
  363.                         purple2.addLeft(presetmode);
  364.                         purple3.addLeft(presetmode);                
  365.                         blue1.addLeft(presetmode);                      
  366.                         blue2.addLeft(presetmode);                    
  367.                         blue3.addLeft(presetmode);                      
  368.                         yellow1.addLeft(presetmode);                        
  369.                         yellow2.addLeft(presetmode);                      
  370.                         orange1.addLeft(presetmode);                  
  371.                         orange2.addLeft(presetmode);                      
  372.                         green1.addLeft(presetmode);                  
  373.                         green2.addLeft(presetmode);
  374.                         green3.addLeft(presetmode);          
  375.         presetmode.addBefore(_randommode);  // I HAD to use an underscore _ before randommode or I would get an
  376.                 presetmode.addAfter(strobomode);    // Error message
  377.                     strobomode.addRight(strobespeed);
  378.                     strobespeed.addBefore(strobocoolblue);
  379.                         strobespeed.addAfter(strobowhite);
  380.                         strobespeed.addRight(flashadjust30);
  381.                           flashadjust40.addAfter(flashadjust30);
  382.                           flashadjust50.addAfter(flashadjust40);
  383.                           flashadjust60.addAfter(flashadjust50);
  384.                           flashadjust70.addAfter(flashadjust60);
  385.                           flashadjust80.addAfter(flashadjust70);
  386.                           flashadjust90.addAfter(flashadjust80);
  387.                           flashadjust100.addAfter(flashadjust90);
  388.                           flashadjust120.addAfter(flashadjust100);
  389.                           flashadjust140.addAfter(flashadjust120);
  390.                           flashadjust160.addAfter(flashadjust140);
  391.                           flashadjust180.addAfter(flashadjust160);
  392.                           flashadjust200.addAfter(flashadjust180);
  393.                           flashadjust220.addAfter(flashadjust200);
  394.                           flashadjust240.addAfter(flashadjust220);
  395.                           flashadjust260.addAfter(flashadjust240);  
  396.                         strobowhite.addBefore(strobespeed);
  397.                         strobowhite.addAfter(strobored);
  398.                         strobored.addBefore(strobespeed);
  399.                         strobored.addAfter(strobogreen);
  400.                         strobogreen.addBefore(strobored);
  401.                         strobogreen.addAfter(stroboblue);
  402.                         stroboblue.addBefore(strobogreen);
  403.                         stroboblue.addAfter(strobopurple);
  404.                         strobopurple.addBefore(stroboblue);
  405.                         strobopurple.addAfter(stroboyellow);
  406.                         stroboyellow.addBefore(strobopurple);
  407.                         stroboyellow.addAfter(strobocoolblue);
  408.                         strobocoolblue.addBefore(stroboyellow);
  409.                         strobocoolblue.addAfter(strobespeed);
  410.                         //we want a left movement to pin to presetmode from anywhere
  411.                         strobespeed.addLeft(strobomode);
  412.                         strobowhite.addLeft(strobomode);
  413.                         strobored.addLeft(strobomode);
  414.                         strobogreen.addLeft(strobomode);
  415.                         stroboblue.addLeft(strobomode);
  416.                         strobopurple.addLeft(strobomode);
  417.                         stroboyellow.addLeft(strobomode);
  418.                         strobocoolblue.addLeft(strobomode);
  419.                         flashadjust30.addLeft(strobespeed);
  420.                         flashadjust40.addLeft(strobespeed);
  421.                         flashadjust50.addLeft(strobespeed);
  422.                         flashadjust60.addLeft(strobespeed);
  423.                         flashadjust70.addLeft(strobespeed);
  424.                         flashadjust80.addLeft(strobespeed);
  425.                         flashadjust90.addLeft(strobespeed);
  426.                         flashadjust100.addLeft(strobespeed);
  427.                         flashadjust120.addLeft(strobespeed);
  428.                         flashadjust140.addLeft(strobespeed);
  429.                         flashadjust160.addLeft(strobespeed);
  430.                         flashadjust180.addLeft(strobespeed);
  431.                         flashadjust200.addLeft(strobespeed);
  432.                         flashadjust220.addLeft(strobespeed);
  433.                         flashadjust240.addLeft(strobespeed);
  434.                         flashadjust260.addLeft(strobespeed);
  435.                 strobomode.addBefore(presetmode);
  436.                 strobomode.addAfter(_randommode);
  437.                 _randommode.addRight(randomspeed);
  438.                       //we want a left movement to pin to presetmode from randomspeed
  439.                       randomspeed.addLeft(_randommode);
  440.                       randomspeed.addRight(randomadjust30);
  441.                           randomadjust40.addAfter(randomadjust30);
  442.                           randomadjust50.addAfter(randomadjust40);
  443.                           randomadjust60.addAfter(randomadjust50);
  444.                           randomadjust70.addAfter(randomadjust60);
  445.                           randomadjust80.addAfter(randomadjust70);
  446.                           randomadjust90.addAfter(randomadjust80);
  447.                           randomadjust100.addAfter(randomadjust90);
  448.                           randomadjust120.addAfter(randomadjust100);
  449.                           randomadjust140.addAfter(randomadjust120);
  450.                           randomadjust160.addAfter(randomadjust140);
  451.                           randomadjust180.addAfter(randomadjust160);
  452.                           randomadjust200.addAfter(randomadjust180);
  453.                           randomadjust220.addAfter(randomadjust200);
  454.                           randomadjust240.addAfter(randomadjust220);
  455.                           randomadjust260.addAfter(randomadjust240);
  456.                         //we want a left movement to pin to presetmode from anywhere
  457.                         randomadjust30.addLeft(_randommode);
  458.                         randomadjust40.addLeft(_randommode);
  459.                         randomadjust50.addLeft(_randommode);
  460.                         randomadjust60.addLeft(_randommode);
  461.                         randomadjust70.addLeft(_randommode);
  462.                         randomadjust80.addLeft(_randommode);
  463.                         randomadjust90.addLeft(_randommode);
  464.                         randomadjust100.addLeft(_randommode);
  465.                         randomadjust120.addLeft(_randommode);
  466.                         randomadjust140.addLeft(_randommode);
  467.                         randomadjust160.addLeft(_randommode);
  468.                         randomadjust180.addLeft(_randommode);
  469.                         randomadjust200.addLeft(_randommode);
  470.                         randomadjust220.addLeft(_randommode);
  471.                         randomadjust240.addLeft(_randommode);
  472.                         randomadjust260.addLeft(_randommode);
  473.                 _randommode.addBefore(strobomode);
  474.                 _randommode.addAfter(presetmode);                                            
  475.     }
  476.    
  477.     void menuChanged(MenuChangeEvent changed)
  478.     {
  479.        lcd.setCursor(0,0);
  480.        printLine(0, "Menu change     ");
  481.        lcd.setCursor(0,1);
  482.        lcd.print(changed.from.getName());
  483.        lcd.print(" ");
  484.        lcd.setCursor(0,1);
  485.        lcd.print(changed.to.getName());
  486.        
  487.        MenuItem newMenuItem=changed.to; //get the destination menu
  488.           if(newMenuItem.getName()==menu.getRoot()){
  489.              
  490.           }else if(newMenuItem.getName()=="Preset Mode     "){
  491.           }else if(newMenuItem.getName()=="Stroboscope Mode"){    
  492.           }else if(newMenuItem.getName()=="Random Mode     "){    
  493.             }else if(newMenuItem.getName()=="Fastest Speed   "){
  494.               flashamount = 30;
  495.             }else if(newMenuItem.getName()=="S             IF"){
  496.               flashamount = 40;
  497.             }else if(newMenuItem.getName()=="S            I F"){
  498.               flashamount = 50;  
  499.             }else if(newMenuItem.getName()=="S           I  F"){
  500.               flashamount = 60;
  501.             }else if(newMenuItem.getName()=="S          I   F"){
  502.               flashamount = 70;
  503.             }else if(newMenuItem.getName()=="S         I    F"){
  504.               flashamount = 80;
  505.             }else if(newMenuItem.getName()=="S        I     F"){
  506.               flashamount = 100;
  507.             }else if(newMenuItem.getName()=="S       I      F"){
  508.               flashamount = 120;
  509.             }else if(newMenuItem.getName()=="S      I       F"){
  510.               flashamount = 130;  
  511.             }else if(newMenuItem.getName()=="S     I        F"){
  512.               flashamount = 140;  
  513.             }else if(newMenuItem.getName()=="S    I         F"){
  514.               flashamount = 160;  
  515.             }else if(newMenuItem.getName()=="S   I          F"){
  516.               flashamount = 180;  
  517.             }else if(newMenuItem.getName()=="S  I           F"){
  518.               flashamount = 200;  
  519.             }else if(newMenuItem.getName()=="S I            F"){
  520.               flashamount = 220;
  521.             }else if(newMenuItem.getName()=="SI             F"){
  522.               flashamount = 240;  
  523.             }else if(newMenuItem.getName()=="Slowest Speed   "){
  524.               flashamount = 260;  
  525.               }else if(newMenuItem.getName()=="White           "){
  526.                 ledstatecolors = whiteledstate;
  527.               }else if(newMenuItem.getName()=="Red             "){
  528.                 ledstatecolors = redledstate;
  529.               }else if(newMenuItem.getName()=="Green           "){
  530.                 ledstatecolors = greenledstate;
  531.               }else if(newMenuItem.getName()=="Blue            "){
  532.                 ledstatecolors = blueledstate;
  533.               }else if(newMenuItem.getName()=="Purple          "){
  534.                 ledstatecolors = purpleledstate;
  535.               }else if(newMenuItem.getName()=="Yellow          "){
  536.                 ledstatecolors = yellowledstate;
  537.               }else if(newMenuItem.getName()=="Cool Blue       "){
  538.                 ledstatecolors = coolblueledstate;
  539.                 }else if(newMenuItem.getName()=="1. White        "){
  540.                   presetcolors = whitecolor;
  541.                 }else if(newMenuItem.getName()=="2. Dark Purple  "){
  542.                   presetcolors = purple1color;
  543.                 }else if(newMenuItem.getName()=="3. Medium Purple"){
  544.                   presetcolors = purple2color;
  545.                 }else if(newMenuItem.getName()=="4. Light Purple "){
  546.                   presetcolors = purple3color;
  547.                 }else if(newMenuItem.getName()=="5. Dark Blue    "){
  548.                   presetcolors = blue1color;
  549.                 }else if(newMenuItem.getName()=="6. Medium Blue  "){
  550.                   presetcolors = blue2color;
  551.                 }else if(newMenuItem.getName()=="7. Light Blue   "){
  552.                   presetcolors = blue3color;
  553.                 }else if(newMenuItem.getName()=="8. Dark Yellow  "){
  554.                   presetcolors = yellow1color;
  555.                 }else if(newMenuItem.getName()=="9. Yellow       "){
  556.                   presetcolors = yellow2color;
  557.                 }else if(newMenuItem.getName()=="10. Dark Orange "){
  558.                   presetcolors = orange1color;
  559.                 }else if(newMenuItem.getName()=="11. Orange      "){
  560.                   presetcolors = orange2color;
  561.                 }else if(newMenuItem.getName()=="12. Bright Green"){
  562.                   presetcolors = green1color;
  563.                 }else if(newMenuItem.getName()=="13. Medium Green"){
  564.                   presetcolors = green2color;
  565.                 }else if(newMenuItem.getName()=="14. Light Green "){
  566.                   presetcolors = green3color;
  567.                   }
  568.     }
  569.    
  570.     void menuUsed(MenuUseEvent used)
  571.     {
  572.          lcd.setCursor(0,0);
  573.          lcd.print("Menu use        ");
  574.          lcd.setCursor(0,1);
  575.          lcd.print(used.item.getName());
  576.          //
  577.          // Mode to choose when buttons is held
  578.          //  
  579.          MenuItem choosenMenuItem=used.item;
  580.            if(choosenMenuItem.getName()==menu.getRoot()){
  581.            
  582.              }else if(choosenMenuItem.getName()=="Preset Mode     "){
  583.              
  584.                moodmodes = presetleds;
  585.              
  586.              }else if(choosenMenuItem.getName()=="Stroboscope Mode"){
  587.      
  588.                moodmodes = blinking;
  589.              
  590.              }else if(choosenMenuItem.getName()=="Random Mode     "){
  591.              
  592.                moodmodes = randommode;
  593.              }
  594.     }
  595.    
  596.     void  readButtons(){             //Read buttons and encoder turns status
  597.    
  598.         if (encoderPos <= 29995 ) {
  599.          
  600.             menu.moveDown();
  601.             encoderPos=30000;
  602.            
  603.         } else if (encoderPos >= 30005 ) {
  604.            
  605.             menu.moveUp();
  606.             encoderPos=30000;
  607.            
  608.                 } else {
  609.                   encoderPos=30000;
  610.                 }
  611.    
  612.     // Read the state of the button
  613.     buttonVal = digitalRead(encoderButton);
  614.    
  615.     // Test for button pressed and store the down time
  616.     if (buttonVal == LOW && buttonLast == HIGH && (millis() - btnUpTime) > long(debounceDelayButton))
  617.     {
  618.     btnDnTime = millis();
  619.     }
  620.    
  621.     // Test for button release and store the up time
  622.     if (buttonVal == HIGH && buttonLast == LOW && (millis() - btnDnTime) > long(debounceDelayButton))
  623.     {
  624.     if (ignoreUp == false) {
  625.      
  626.       menu.moveLeft();
  627.    
  628.     }else { ignoreUp = false;
  629.     btnUpTime = millis();
  630.     }
  631.     }
  632.    
  633.     // Test for button held down for longer than the hold time
  634.     if (buttonVal == LOW && (millis() - btnDnTime) > long(holdTime))
  635.     {
  636.      
  637.       menu.use();
  638.       menu.moveRight();
  639.      
  640.     ignoreUp = true;
  641.     btnDnTime = millis();
  642.     }
  643.    
  644.     buttonLast = buttonVal;
  645.     }
  646.    
  647.     void printLine(uint8_t regel, char * text) {
  648.       lcd.setCursor(0, regel);
  649.       lcd.print("                ");
  650.       lcd.setCursor(0, regel);
  651.       lcd.print(text);
  652.     }
  653.    
  654.     //
  655.     // FSM MoodlightModes
  656.     //
  657.     void  moodlightModes() {
  658.            
  659.       switch(moodmodes) {
  660.        
  661.          case presetleds:
  662.          
  663.                  analogWrite(REDLEDPIN,   0);
  664.                  analogWrite(GREENLEDPIN, 0);
  665.                  analogWrite(BLUELEDPIN,  0);
  666.            
  667.              switch(presetcolors) {
  668.                
  669.                  case whitecolor:
  670.                  analogWrite(REDLEDPIN,   255);
  671.                  analogWrite(GREENLEDPIN, 255);
  672.                  analogWrite(BLUELEDPIN,  255);
  673.                  break;
  674.                  case purple1color:
  675.                  analogWrite(REDLEDPIN,   255);
  676.                  analogWrite(GREENLEDPIN, 0);
  677.                  analogWrite(BLUELEDPIN,  255);
  678.                  break;
  679.                  case purple2color:
  680.                  analogWrite(REDLEDPIN,   255);
  681.                  analogWrite(GREENLEDPIN, 25);
  682.                  analogWrite(BLUELEDPIN,  255);
  683.                  case purple3color:
  684.                  analogWrite(REDLEDPIN,   255);
  685.                  analogWrite(GREENLEDPIN, 100);
  686.                  analogWrite(BLUELEDPIN,  255);
  687.                  case blue1color:
  688.                  analogWrite(REDLEDPIN,   0);
  689.                  analogWrite(GREENLEDPIN, 0);
  690.                  analogWrite(BLUELEDPIN,  255);
  691.                  break;
  692.                  case blue2color:
  693.                  analogWrite(REDLEDPIN,   0);
  694.                  analogWrite(GREENLEDPIN, 50);
  695.                  analogWrite(BLUELEDPIN,  255);
  696.                  case blue3color:
  697.                  analogWrite(REDLEDPIN,   255);
  698.                  analogWrite(GREENLEDPIN, 100);
  699.                  analogWrite(BLUELEDPIN,  255);
  700.                  case yellow1color:
  701.                  analogWrite(REDLEDPIN,   255);
  702.                  analogWrite(GREENLEDPIN, 255);
  703.                  analogWrite(BLUELEDPIN,  0);
  704.                  break;
  705.                  case yellow2color:
  706.                  analogWrite(REDLEDPIN,   255);
  707.                  analogWrite(GREENLEDPIN, 100);
  708.                  analogWrite(BLUELEDPIN,  0);
  709.                  break;
  710.                  case orange1color:
  711.                  analogWrite(REDLEDPIN,   255);
  712.                  analogWrite(GREENLEDPIN, 50);
  713.                  analogWrite(BLUELEDPIN,  0);
  714.                  break;
  715.                  case orange2color:
  716.                  analogWrite(REDLEDPIN,   255);
  717.                  analogWrite(GREENLEDPIN, 25);
  718.                  analogWrite(BLUELEDPIN,  0);
  719.                  break;
  720.                  case green1color:
  721.                  analogWrite(REDLEDPIN,   150);
  722.                  analogWrite(GREENLEDPIN, 255);
  723.                  analogWrite(BLUELEDPIN,  10);
  724.                  break;
  725.                  case green2color:
  726.                  analogWrite(REDLEDPIN,   100);
  727.                  analogWrite(GREENLEDPIN, 255);
  728.                  analogWrite(BLUELEDPIN,  10);
  729.                  break;
  730.                  case green3color:
  731.                  analogWrite(REDLEDPIN,   25);
  732.                  analogWrite(GREENLEDPIN, 255);
  733.                  analogWrite(BLUELEDPIN,  0);
  734.                  break;
  735.             }
  736.      
  737.         break;
  738.    
  739.         case blinking:
  740.        
  741.             switch(ledstatecolors) {
  742.              
  743.               case whiteledstate:
  744.                   if (millis() > delayCtr) {
  745.                   analogWrite(REDLEDPIN, ledState);
  746.                   analogWrite(GREENLEDPIN, ledState);
  747.                   analogWrite(BLUELEDPIN, ledState);  
  748.                   }
  749.                   break;
  750.               case redledstate:
  751.                   if (millis() > delayCtr) {
  752.                   analogWrite(REDLEDPIN, ledState);
  753.                   analogWrite(GREENLEDPIN, LOW);
  754.                   analogWrite(BLUELEDPIN, LOW);  
  755.                   }
  756.                   break;
  757.               case greenledstate:
  758.                   if (millis() > delayCtr) {
  759.                   analogWrite(REDLEDPIN, LOW);
  760.                   analogWrite(GREENLEDPIN, ledState);
  761.                   analogWrite(BLUELEDPIN, LOW);  
  762.                   }
  763.                   break;
  764.               case blueledstate:
  765.                   if (millis() > delayCtr) {
  766.                   analogWrite(REDLEDPIN, LOW);
  767.                   analogWrite(GREENLEDPIN, LOW);
  768.                   analogWrite(BLUELEDPIN, ledState);  
  769.                   }
  770.                   break;
  771.               case purpleledstate:
  772.                   if (millis() > delayCtr) {
  773.                   analogWrite(REDLEDPIN, ledState);
  774.                   analogWrite(GREENLEDPIN, LOW);
  775.                   analogWrite(BLUELEDPIN, ledState);  
  776.                   }
  777.                   break;
  778.               case yellowledstate:
  779.                   if (millis() > delayCtr) {
  780.                   analogWrite(REDLEDPIN, ledState);
  781.                   analogWrite(GREENLEDPIN, ledState);
  782.                   analogWrite(BLUELEDPIN, LOW);  
  783.                   }
  784.                   break;
  785.               case coolblueledstate:
  786.                   if (millis() > delayCtr) {
  787.                   analogWrite(REDLEDPIN, LOW);
  788.                   analogWrite(GREENLEDPIN, ledState);
  789.                   analogWrite(BLUELEDPIN, ledState);  
  790.                   }
  791.                   break;        
  792.             }
  793.            
  794.         flashamount = constrain(flashamount, 30, 260);
  795.                  
  796.         strobe_delay = minimum_delay + flashamount * analog_value_multiplier;
  797.        
  798.         currentMillis = millis();
  799.        
  800.         if(currentMillis - previousMillis > strobe_delay) {
  801.         // save the last time you blinked the LED
  802.         previousMillis = currentMillis;
  803.            // if the LED is off turn it on and vice-versa:
  804.         if (ledState == LOW)
  805.         brightness = 255;
  806.          ledState = brightness;
  807.          delayMicroseconds(on_time);
  808.         }
  809.         else
  810.         {
  811.           ledState = LOW;
  812.       }
  813.  
  814.     if (strobe_delay > 10000){
  815.    
  816.         delay((strobe_delay - on_time) / 1000);
  817.    
  818.     }else{
  819.    
  820.       delayMicroseconds(strobe_delay - on_time);
  821.     }
  822.    
  823.     break;
  824.        
  825.       case randommode:
  826.      
  827.         flashamount = constrain(flashamount, 30, 260);
  828.                      
  829.         strobe_delay = minimum_delay + flashamount * analog_value_multiplier;
  830.        
  831.         randomSeed(analogRead(6));
  832.         if (millis() > delayCtr) {
  833.         delayCtr = millis() +  strobe_delay;
  834.         for (int x=0; x<3; x++) {
  835.         INC[x] = (RGB1[x] - RGB2[x]) / 256; }
  836.         for (int x=0; x<256; x++) {
  837.         red = int(RGB1[0]);
  838.         green = int(RGB1[1]);
  839.         blue = int(RGB1[2]);
  840.         analogWrite (REDLEDPIN, red);
  841.         analogWrite (GREENLEDPIN, green);
  842.         analogWrite (BLUELEDPIN, blue);
  843.         if (millis() > flashamount) {
  844.         delayCtr = millis() + (strobe_delay * 25); // the bigger the number (max 50) after * the slower it will change color
  845.         RGB1[0] -= INC[0];
  846.         RGB1[1] -= INC[1];
  847.         RGB1[2] -= INC[2];
  848.         }
  849.         for (int x=0; x<3; x++) {
  850.         RGB2[x] = random(556)-300;
  851.         RGB2[x] = constrain(RGB2[x], 0, 255);
  852.        
  853.             }
  854.           }
  855.         }
  856.    
  857.     break;
  858.    
  859.       }
  860.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement