Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @file moodlight.ino
- * @author Hannes Beem
- * @date 09-06-2014
- *
- * @par License info
- *
- * Copyright (C) 2014 Hannes Beem
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- * Program : Mood light Copyright (C) 2014 Hannes Beem
- * This is free software, and you are welcome to redistribute it.
- *
- * Thanks to Alexander Brevig for his work on the MenuBackend library. http://www.arduino.cc/playground/uploads/Profiles/MenuBackend_1-4.zip
- * Thanks to rafbuff for his work on the most accurate encoder code found on the Arduino.cc website. http://playground.arduino.cc/Main/RotaryEncoders
- * Thanks to transcendtient for his work on the LiquidCrystal library found on the Arduino.cc website. http://playground.arduino.cc/Main/LiquidCrystal
- * Thanks to Nico Verduin, shooter, resu from the arduinoforum.nl forum.
- * Thanks to PaulS from the Arduino.cc forum.
- * Thanks to Michael McRoberts Author of Beginning Arduino pdf.
- ! 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
- void toRoot() {
- setCurrent( &getRoot() );
- }
- */
- #include <SPI.h> // Necessary for use with the 74HC595 chip
- #include <MenuBackend.h> // MenuBackend library
- #include <LiquidCrystal.h> // LCD Display Library for use with 74HC595 chip
- // initialize the library with the number of the sspin
- //(or the latch pin of the 74HC595)
- LiquidCrystal lcd(10);
- //
- // Encoder Definitions
- //
- #define encoderPinA 2 // right
- #define encoderPinB 3 // left
- #define encoderButton 4 // another two pins
- //
- // PIN definitions LEDS
- //
- #define REDLEDPIN 5
- #define GREENLEDPIN 6
- #define BLUELEDPIN 9
- //
- // FSM States
- //
- #define presetleds 0
- #define blinking 1
- #define randommode 2
- #define blinkadjust 3
- #define whiteledstate 4
- #define redledstate 5
- #define greenledstate 6
- #define blueledstate 7
- #define purpleledstate 8
- #define yellowledstate 9
- #define coolblueledstate 10
- #define mintgreenledstate 11
- //
- // Preset colors
- //
- #define whitecolor 12
- #define purple1color 13
- #define purple2color 14
- #define purple3color 15
- #define blue1color 16
- #define blue2color 17
- #define blue3color 18
- #define yellow1color 19
- #define yellow2color 20
- #define orange1color 21
- #define orange2color 22
- #define green1color 23
- #define green2color 24
- #define green3color 25
- //
- // Encoder Variables
- //
- volatile unsigned int encoderPos = 30000; // a counter for the dial
- unsigned int lastReportedPos = 1; // change management
- static boolean rotating=false; // debounce management
- int reading; // For reading the buttonstate
- int lastButtonState; // the previous reading from the input pin
- long lastDebounceTime = 0; // the last time the output pin was toggled
- long debounceDelayButton = 50; // the debounce time; increase if the output flickers
- int buttonState; // the current reading from the input pin
- #define holdTime 1000 // ms hold period: how long to wait for press+hold event
- int buttonVal = 0; // value read from button
- int buttonLast = 0; // buffered value of the button's previous state
- long btnDnTime; // time the button was pressed down
- long btnUpTime; // time the button was released
- boolean ignoreUp = false; // whether to ignore the button release because the click+hold was triggered
- // interrupt service routine vars
- boolean A_set = false;
- boolean B_set = false;
- //
- // Variables FSM States
- //
- int moodmodes; // Switch variable for the Modes
- int ledstatecolors; // Switch variable for the Stroboscope colors
- int presetcolors; // Switch variable for the Presetcolors
- //
- // Variables for preset and Strobo
- //
- int ledState = LOW; // Set ledstate for Stroboscope to off
- int x; // Variable used in Randommode
- // How much time should the light stay on between delay times (in Micro Seconds)?
- /* Big number = more blur, more perceived brightness
- * Small number = less blur, less perceived brightness */
- long on_time = 500;
- // How much should we multiply the incoming value by?
- /* Big number = less precision, more range
- * Small number = more precision, less range */
- long analog_value_multiplier = 1; // 15 Seems to work well for fast fans =)
- // What should the minimum delay be (in Micro Seconds)?
- // This sets the bottom delay range of the strobe, as a delay of 0 doesn't actually flash =P
- // The strobe starts with this is the "fastest" mode, and goes slower from there, adding to the delay
- long minimum_delay = 5; // 500 (half a millisecond) is a great superfast start
- // Initialize the number to hold our strobe delay. Isn't used till we get to the main loop
- long strobe_delay = 1000;
- //
- // Strobe & Random mode variables
- //
- float RGB1[3];
- float RGB2[3];
- float INC[3];
- int red, green, blue;
- int brightness = 0; // How bright the LED is
- int flashamount = 30; // Start value for the blink speed of the Stroboscope
- unsigned long delayCtr = 0L; // Used to determin when to do something
- //
- //timers
- //
- unsigned long currentMillis = millis(); // Used to determin when to do something
- long previousMillis = 0; // Used to determin when to do something
- //
- // Menu setup
- //
- //this controls the menu backend and the event generation
- MenuBackend menu = MenuBackend(menuUsed,menuChanged);
- //beneath is list of menu items needed to build the menu
- MenuItem presetmode = MenuItem("Preset Mode ");
- MenuItem white = MenuItem("1. White ");
- MenuItem purple1 = MenuItem("2. Dark Purple ");
- MenuItem purple2 = MenuItem("3. Medium Purple");
- MenuItem purple3 = MenuItem("4. Light Purple ");
- MenuItem blue1 = MenuItem("5. Dark Blue ");
- MenuItem blue2 = MenuItem("6. Medium Blue ");
- MenuItem blue3 = MenuItem("7. Light Blue ");
- MenuItem yellow1 = MenuItem("8. Dark Yellow ");
- MenuItem yellow2 = MenuItem("9. Yellow ");
- MenuItem orange1 = MenuItem("10. Dark Orange ");
- MenuItem orange2 = MenuItem("11. Orange ");
- MenuItem green1 = MenuItem("12. Bright Green");
- MenuItem green2 = MenuItem("13. Medium Green");
- MenuItem green3 = MenuItem("14. Light Green ");
- MenuItem strobomode = MenuItem("Stroboscope Mode");
- MenuItem strobespeed = MenuItem("Adjust Speed? ");
- MenuItem flashadjust30 = MenuItem("Fastest Speed ");
- MenuItem flashadjust40 = MenuItem("S IF");
- MenuItem flashadjust50 = MenuItem("S I F");
- MenuItem flashadjust60 = MenuItem("S I F");
- MenuItem flashadjust70 = MenuItem("S I F");
- MenuItem flashadjust80 = MenuItem("S I F");
- MenuItem flashadjust90 = MenuItem("S I F");
- MenuItem flashadjust100 = MenuItem("S I F");
- MenuItem flashadjust120 = MenuItem("S I F");
- MenuItem flashadjust140 = MenuItem("S I F");
- MenuItem flashadjust160 = MenuItem("S I F");
- MenuItem flashadjust180 = MenuItem("S I F");
- MenuItem flashadjust200 = MenuItem("S I F");
- MenuItem flashadjust220 = MenuItem("S I F");
- MenuItem flashadjust240 = MenuItem("SI F");
- MenuItem flashadjust260 = MenuItem("Slowest Speed ");
- MenuItem strobowhite = MenuItem("White ");
- MenuItem strobored = MenuItem("Red ");
- MenuItem strobogreen = MenuItem("Green ");
- MenuItem stroboblue = MenuItem("Blue ");
- MenuItem strobopurple = MenuItem("Purple ");
- MenuItem stroboyellow = MenuItem("Yellow ");
- MenuItem strobocoolblue = MenuItem("Cool Blue ");
- MenuItem _randommode = MenuItem("Random Mode ");
- MenuItem randomspeed = MenuItem("Change Speed? ");
- MenuItem randomadjust30 = MenuItem("Fastest Speed ");
- MenuItem randomadjust40 = MenuItem("S IF");
- MenuItem randomadjust50 = MenuItem("S I F");
- MenuItem randomadjust60 = MenuItem("S I F");
- MenuItem randomadjust70 = MenuItem("S I F");
- MenuItem randomadjust80 = MenuItem("S I F");
- MenuItem randomadjust90 = MenuItem("S I F");
- MenuItem randomadjust100 = MenuItem("S I F");
- MenuItem randomadjust120 = MenuItem("S I F");
- MenuItem randomadjust140 = MenuItem("S I F");
- MenuItem randomadjust160 = MenuItem("S I F");
- MenuItem randomadjust180 = MenuItem("S I F");
- MenuItem randomadjust200 = MenuItem("S I F");
- MenuItem randomadjust220 = MenuItem("S I F");
- MenuItem randomadjust240 = MenuItem("SI F");
- MenuItem randomadjust260 = MenuItem("Slowest Speed ");
- void setup()
- {
- //
- // Encoder Setup
- //
- pinMode(encoderPinA, INPUT);
- pinMode(encoderPinB, INPUT);
- pinMode(encoderButton, INPUT);
- // turn on pullup resistors
- digitalWrite(encoderPinA, HIGH);
- digitalWrite(encoderPinB, HIGH);
- digitalWrite(encoderButton, HIGH);
- // encoder pin on interrupt 0 (pin 2)
- attachInterrupt(0, doEncoderA, CHANGE);
- // encoder pin on interrupt 1 (pin 3)
- attachInterrupt(1, doEncoderB, CHANGE);
- //
- // LED Setup
- //
- pinMode(REDLEDPIN, OUTPUT);
- pinMode(GREENLEDPIN, OUTPUT);
- pinMode(BLUELEDPIN, OUTPUT);
- //
- // LEDS OFF at the start
- //
- analogWrite(REDLEDPIN, 0);
- analogWrite(GREENLEDPIN, 255);
- analogWrite(BLUELEDPIN, 255);
- //
- // LCD Menu Setup
- //
- lcd.begin(16, 2);
- lcd.setCursor(0,0);
- lcd.print("Mood Light");
- delay(2000);
- lcd.setCursor(0,1);
- lcd.print("Turn Encoder ");
- }
- void loop()
- {
- moodlightModes(); // FSM moodlight Modes loop
- menuSetup(); // Menu loop
- readButtons(); // Read buttons and encoder turns status loop
- //
- // Encoder
- //
- rotating = true; // reset the debouncer
- if (lastReportedPos != encoderPos) {
- lastReportedPos = encoderPos;
- }
- if (digitalRead(encoderButton) == LOW ) {
- encoderPos = 30000;
- }
- }
- // Interrupt on A changing state
- void doEncoderA(){
- // debounce
- if ( rotating ) delay (1); // wait a little until the bouncing is done
- // Test transition, did things really change?
- if( digitalRead(encoderPinA) != A_set ) { // debounce once more
- A_set = !A_set;
- // adjust counter + if A leads B
- if ( A_set && !B_set )
- encoderPos += 5; // Adds 5 to 30000, Menu responds bij moving up
- rotating = false; // no more debouncing until loop() hits again
- }
- }
- // Interrupt on B changing state, same as A above
- void doEncoderB(){
- if ( rotating ) delay (1);
- if( digitalRead(encoderPinB) != B_set ) {
- B_set = !B_set;
- // adjust counter - 1 if B leads A
- if( B_set && !A_set )
- encoderPos -= 5; // decrements 5 from 30000, Menu responds bij moving up
- rotating = false;
- }
- }
- void menuSetup()
- {
- //add the file menu to the menu root
- menu.getRoot().add(presetmode);
- //setup the presetmode menu item
- presetmode.addRight(white);
- //we want looping both up and down
- white.addBefore(green3);
- white.addAfter(purple1);
- purple1.addBefore(white);
- purple1.addAfter(purple2);
- purple2.addBefore(purple1);
- purple2.addAfter(purple3);
- purple3.addBefore(purple2);
- purple3.addAfter(blue1);
- blue1.addBefore(purple3);
- blue1.addAfter(blue2);
- blue2.addBefore(blue1);
- blue2.addAfter(blue3);
- blue3.addBefore(blue2);
- blue3.addAfter(yellow1);
- yellow1.addBefore(blue3);
- yellow1.addAfter(yellow2);
- yellow2.addBefore(yellow1);
- yellow2.addAfter(orange1);
- orange1.addBefore(yellow2);
- orange1.addAfter(orange2);
- orange2.addBefore(orange1);
- orange2.addAfter(green1);
- green1.addBefore(orange2);
- green1.addAfter(green2);
- green2.addBefore(green1);
- green2.addAfter(green3);
- green3.addBefore(green2);
- green3.addAfter(white);
- //we want a left movement to pin to presetmode from anywhere
- white.addLeft(presetmode);
- purple1.addLeft(presetmode);
- purple2.addLeft(presetmode);
- purple3.addLeft(presetmode);
- blue1.addLeft(presetmode);
- blue2.addLeft(presetmode);
- blue3.addLeft(presetmode);
- yellow1.addLeft(presetmode);
- yellow2.addLeft(presetmode);
- orange1.addLeft(presetmode);
- orange2.addLeft(presetmode);
- green1.addLeft(presetmode);
- green2.addLeft(presetmode);
- green3.addLeft(presetmode);
- presetmode.addBefore(_randommode); // I HAD to use an underscore _ before randommode or I would get an
- presetmode.addAfter(strobomode); // Error message
- strobomode.addRight(strobespeed);
- strobespeed.addBefore(strobocoolblue);
- strobespeed.addAfter(strobowhite);
- strobespeed.addRight(flashadjust30);
- flashadjust40.addAfter(flashadjust30);
- flashadjust50.addAfter(flashadjust40);
- flashadjust60.addAfter(flashadjust50);
- flashadjust70.addAfter(flashadjust60);
- flashadjust80.addAfter(flashadjust70);
- flashadjust90.addAfter(flashadjust80);
- flashadjust100.addAfter(flashadjust90);
- flashadjust120.addAfter(flashadjust100);
- flashadjust140.addAfter(flashadjust120);
- flashadjust160.addAfter(flashadjust140);
- flashadjust180.addAfter(flashadjust160);
- flashadjust200.addAfter(flashadjust180);
- flashadjust220.addAfter(flashadjust200);
- flashadjust240.addAfter(flashadjust220);
- flashadjust260.addAfter(flashadjust240);
- strobowhite.addBefore(strobespeed);
- strobowhite.addAfter(strobored);
- strobored.addBefore(strobespeed);
- strobored.addAfter(strobogreen);
- strobogreen.addBefore(strobored);
- strobogreen.addAfter(stroboblue);
- stroboblue.addBefore(strobogreen);
- stroboblue.addAfter(strobopurple);
- strobopurple.addBefore(stroboblue);
- strobopurple.addAfter(stroboyellow);
- stroboyellow.addBefore(strobopurple);
- stroboyellow.addAfter(strobocoolblue);
- strobocoolblue.addBefore(stroboyellow);
- strobocoolblue.addAfter(strobespeed);
- //we want a left movement to pin to presetmode from anywhere
- strobespeed.addLeft(strobomode);
- strobowhite.addLeft(strobomode);
- strobored.addLeft(strobomode);
- strobogreen.addLeft(strobomode);
- stroboblue.addLeft(strobomode);
- strobopurple.addLeft(strobomode);
- stroboyellow.addLeft(strobomode);
- strobocoolblue.addLeft(strobomode);
- flashadjust30.addLeft(strobespeed);
- flashadjust40.addLeft(strobespeed);
- flashadjust50.addLeft(strobespeed);
- flashadjust60.addLeft(strobespeed);
- flashadjust70.addLeft(strobespeed);
- flashadjust80.addLeft(strobespeed);
- flashadjust90.addLeft(strobespeed);
- flashadjust100.addLeft(strobespeed);
- flashadjust120.addLeft(strobespeed);
- flashadjust140.addLeft(strobespeed);
- flashadjust160.addLeft(strobespeed);
- flashadjust180.addLeft(strobespeed);
- flashadjust200.addLeft(strobespeed);
- flashadjust220.addLeft(strobespeed);
- flashadjust240.addLeft(strobespeed);
- flashadjust260.addLeft(strobespeed);
- strobomode.addBefore(presetmode);
- strobomode.addAfter(_randommode);
- _randommode.addRight(randomspeed);
- //we want a left movement to pin to presetmode from randomspeed
- randomspeed.addLeft(_randommode);
- randomspeed.addRight(randomadjust30);
- randomadjust40.addAfter(randomadjust30);
- randomadjust50.addAfter(randomadjust40);
- randomadjust60.addAfter(randomadjust50);
- randomadjust70.addAfter(randomadjust60);
- randomadjust80.addAfter(randomadjust70);
- randomadjust90.addAfter(randomadjust80);
- randomadjust100.addAfter(randomadjust90);
- randomadjust120.addAfter(randomadjust100);
- randomadjust140.addAfter(randomadjust120);
- randomadjust160.addAfter(randomadjust140);
- randomadjust180.addAfter(randomadjust160);
- randomadjust200.addAfter(randomadjust180);
- randomadjust220.addAfter(randomadjust200);
- randomadjust240.addAfter(randomadjust220);
- randomadjust260.addAfter(randomadjust240);
- //we want a left movement to pin to presetmode from anywhere
- randomadjust30.addLeft(_randommode);
- randomadjust40.addLeft(_randommode);
- randomadjust50.addLeft(_randommode);
- randomadjust60.addLeft(_randommode);
- randomadjust70.addLeft(_randommode);
- randomadjust80.addLeft(_randommode);
- randomadjust90.addLeft(_randommode);
- randomadjust100.addLeft(_randommode);
- randomadjust120.addLeft(_randommode);
- randomadjust140.addLeft(_randommode);
- randomadjust160.addLeft(_randommode);
- randomadjust180.addLeft(_randommode);
- randomadjust200.addLeft(_randommode);
- randomadjust220.addLeft(_randommode);
- randomadjust240.addLeft(_randommode);
- randomadjust260.addLeft(_randommode);
- _randommode.addBefore(strobomode);
- _randommode.addAfter(presetmode);
- }
- void menuChanged(MenuChangeEvent changed)
- {
- lcd.setCursor(0,0);
- printLine(0, "Menu change ");
- lcd.setCursor(0,1);
- lcd.print(changed.from.getName());
- lcd.print(" ");
- lcd.setCursor(0,1);
- lcd.print(changed.to.getName());
- MenuItem newMenuItem=changed.to; //get the destination menu
- if(newMenuItem.getName()==menu.getRoot()){
- }else if(newMenuItem.getName()=="Preset Mode "){
- }else if(newMenuItem.getName()=="Stroboscope Mode"){
- }else if(newMenuItem.getName()=="Random Mode "){
- }else if(newMenuItem.getName()=="Fastest Speed "){
- flashamount = 30;
- }else if(newMenuItem.getName()=="S IF"){
- flashamount = 40;
- }else if(newMenuItem.getName()=="S I F"){
- flashamount = 50;
- }else if(newMenuItem.getName()=="S I F"){
- flashamount = 60;
- }else if(newMenuItem.getName()=="S I F"){
- flashamount = 70;
- }else if(newMenuItem.getName()=="S I F"){
- flashamount = 80;
- }else if(newMenuItem.getName()=="S I F"){
- flashamount = 100;
- }else if(newMenuItem.getName()=="S I F"){
- flashamount = 120;
- }else if(newMenuItem.getName()=="S I F"){
- flashamount = 130;
- }else if(newMenuItem.getName()=="S I F"){
- flashamount = 140;
- }else if(newMenuItem.getName()=="S I F"){
- flashamount = 160;
- }else if(newMenuItem.getName()=="S I F"){
- flashamount = 180;
- }else if(newMenuItem.getName()=="S I F"){
- flashamount = 200;
- }else if(newMenuItem.getName()=="S I F"){
- flashamount = 220;
- }else if(newMenuItem.getName()=="SI F"){
- flashamount = 240;
- }else if(newMenuItem.getName()=="Slowest Speed "){
- flashamount = 260;
- }else if(newMenuItem.getName()=="White "){
- ledstatecolors = whiteledstate;
- }else if(newMenuItem.getName()=="Red "){
- ledstatecolors = redledstate;
- }else if(newMenuItem.getName()=="Green "){
- ledstatecolors = greenledstate;
- }else if(newMenuItem.getName()=="Blue "){
- ledstatecolors = blueledstate;
- }else if(newMenuItem.getName()=="Purple "){
- ledstatecolors = purpleledstate;
- }else if(newMenuItem.getName()=="Yellow "){
- ledstatecolors = yellowledstate;
- }else if(newMenuItem.getName()=="Cool Blue "){
- ledstatecolors = coolblueledstate;
- }else if(newMenuItem.getName()=="1. White "){
- presetcolors = whitecolor;
- }else if(newMenuItem.getName()=="2. Dark Purple "){
- presetcolors = purple1color;
- }else if(newMenuItem.getName()=="3. Medium Purple"){
- presetcolors = purple2color;
- }else if(newMenuItem.getName()=="4. Light Purple "){
- presetcolors = purple3color;
- }else if(newMenuItem.getName()=="5. Dark Blue "){
- presetcolors = blue1color;
- }else if(newMenuItem.getName()=="6. Medium Blue "){
- presetcolors = blue2color;
- }else if(newMenuItem.getName()=="7. Light Blue "){
- presetcolors = blue3color;
- }else if(newMenuItem.getName()=="8. Dark Yellow "){
- presetcolors = yellow1color;
- }else if(newMenuItem.getName()=="9. Yellow "){
- presetcolors = yellow2color;
- }else if(newMenuItem.getName()=="10. Dark Orange "){
- presetcolors = orange1color;
- }else if(newMenuItem.getName()=="11. Orange "){
- presetcolors = orange2color;
- }else if(newMenuItem.getName()=="12. Bright Green"){
- presetcolors = green1color;
- }else if(newMenuItem.getName()=="13. Medium Green"){
- presetcolors = green2color;
- }else if(newMenuItem.getName()=="14. Light Green "){
- presetcolors = green3color;
- }
- }
- void menuUsed(MenuUseEvent used)
- {
- lcd.setCursor(0,0);
- lcd.print("Menu use ");
- lcd.setCursor(0,1);
- lcd.print(used.item.getName());
- //
- // Mode to choose when buttons is held
- //
- MenuItem choosenMenuItem=used.item;
- if(choosenMenuItem.getName()==menu.getRoot()){
- }else if(choosenMenuItem.getName()=="Preset Mode "){
- moodmodes = presetleds;
- }else if(choosenMenuItem.getName()=="Stroboscope Mode"){
- moodmodes = blinking;
- }else if(choosenMenuItem.getName()=="Random Mode "){
- moodmodes = randommode;
- }
- }
- void readButtons(){ //Read buttons and encoder turns status
- if (encoderPos <= 29995 ) {
- menu.moveDown();
- encoderPos=30000;
- } else if (encoderPos >= 30005 ) {
- menu.moveUp();
- encoderPos=30000;
- } else {
- encoderPos=30000;
- }
- // Read the state of the button
- buttonVal = digitalRead(encoderButton);
- // Test for button pressed and store the down time
- if (buttonVal == LOW && buttonLast == HIGH && (millis() - btnUpTime) > long(debounceDelayButton))
- {
- btnDnTime = millis();
- }
- // Test for button release and store the up time
- if (buttonVal == HIGH && buttonLast == LOW && (millis() - btnDnTime) > long(debounceDelayButton))
- {
- if (ignoreUp == false) {
- menu.moveLeft();
- }else { ignoreUp = false;
- btnUpTime = millis();
- }
- }
- // Test for button held down for longer than the hold time
- if (buttonVal == LOW && (millis() - btnDnTime) > long(holdTime))
- {
- menu.use();
- menu.moveRight();
- ignoreUp = true;
- btnDnTime = millis();
- }
- buttonLast = buttonVal;
- }
- void printLine(uint8_t regel, char * text) {
- lcd.setCursor(0, regel);
- lcd.print(" ");
- lcd.setCursor(0, regel);
- lcd.print(text);
- }
- //
- // FSM MoodlightModes
- //
- void moodlightModes() {
- switch(moodmodes) {
- case presetleds:
- analogWrite(REDLEDPIN, 0);
- analogWrite(GREENLEDPIN, 0);
- analogWrite(BLUELEDPIN, 0);
- switch(presetcolors) {
- case whitecolor:
- analogWrite(REDLEDPIN, 255);
- analogWrite(GREENLEDPIN, 255);
- analogWrite(BLUELEDPIN, 255);
- break;
- case purple1color:
- analogWrite(REDLEDPIN, 255);
- analogWrite(GREENLEDPIN, 0);
- analogWrite(BLUELEDPIN, 255);
- break;
- case purple2color:
- analogWrite(REDLEDPIN, 255);
- analogWrite(GREENLEDPIN, 25);
- analogWrite(BLUELEDPIN, 255);
- case purple3color:
- analogWrite(REDLEDPIN, 255);
- analogWrite(GREENLEDPIN, 100);
- analogWrite(BLUELEDPIN, 255);
- case blue1color:
- analogWrite(REDLEDPIN, 0);
- analogWrite(GREENLEDPIN, 0);
- analogWrite(BLUELEDPIN, 255);
- break;
- case blue2color:
- analogWrite(REDLEDPIN, 0);
- analogWrite(GREENLEDPIN, 50);
- analogWrite(BLUELEDPIN, 255);
- case blue3color:
- analogWrite(REDLEDPIN, 255);
- analogWrite(GREENLEDPIN, 100);
- analogWrite(BLUELEDPIN, 255);
- case yellow1color:
- analogWrite(REDLEDPIN, 255);
- analogWrite(GREENLEDPIN, 255);
- analogWrite(BLUELEDPIN, 0);
- break;
- case yellow2color:
- analogWrite(REDLEDPIN, 255);
- analogWrite(GREENLEDPIN, 100);
- analogWrite(BLUELEDPIN, 0);
- break;
- case orange1color:
- analogWrite(REDLEDPIN, 255);
- analogWrite(GREENLEDPIN, 50);
- analogWrite(BLUELEDPIN, 0);
- break;
- case orange2color:
- analogWrite(REDLEDPIN, 255);
- analogWrite(GREENLEDPIN, 25);
- analogWrite(BLUELEDPIN, 0);
- break;
- case green1color:
- analogWrite(REDLEDPIN, 150);
- analogWrite(GREENLEDPIN, 255);
- analogWrite(BLUELEDPIN, 10);
- break;
- case green2color:
- analogWrite(REDLEDPIN, 100);
- analogWrite(GREENLEDPIN, 255);
- analogWrite(BLUELEDPIN, 10);
- break;
- case green3color:
- analogWrite(REDLEDPIN, 25);
- analogWrite(GREENLEDPIN, 255);
- analogWrite(BLUELEDPIN, 0);
- break;
- }
- break;
- case blinking:
- switch(ledstatecolors) {
- case whiteledstate:
- if (millis() > delayCtr) {
- analogWrite(REDLEDPIN, ledState);
- analogWrite(GREENLEDPIN, ledState);
- analogWrite(BLUELEDPIN, ledState);
- }
- break;
- case redledstate:
- if (millis() > delayCtr) {
- analogWrite(REDLEDPIN, ledState);
- analogWrite(GREENLEDPIN, LOW);
- analogWrite(BLUELEDPIN, LOW);
- }
- break;
- case greenledstate:
- if (millis() > delayCtr) {
- analogWrite(REDLEDPIN, LOW);
- analogWrite(GREENLEDPIN, ledState);
- analogWrite(BLUELEDPIN, LOW);
- }
- break;
- case blueledstate:
- if (millis() > delayCtr) {
- analogWrite(REDLEDPIN, LOW);
- analogWrite(GREENLEDPIN, LOW);
- analogWrite(BLUELEDPIN, ledState);
- }
- break;
- case purpleledstate:
- if (millis() > delayCtr) {
- analogWrite(REDLEDPIN, ledState);
- analogWrite(GREENLEDPIN, LOW);
- analogWrite(BLUELEDPIN, ledState);
- }
- break;
- case yellowledstate:
- if (millis() > delayCtr) {
- analogWrite(REDLEDPIN, ledState);
- analogWrite(GREENLEDPIN, ledState);
- analogWrite(BLUELEDPIN, LOW);
- }
- break;
- case coolblueledstate:
- if (millis() > delayCtr) {
- analogWrite(REDLEDPIN, LOW);
- analogWrite(GREENLEDPIN, ledState);
- analogWrite(BLUELEDPIN, ledState);
- }
- break;
- }
- flashamount = constrain(flashamount, 30, 260);
- strobe_delay = minimum_delay + flashamount * analog_value_multiplier;
- currentMillis = millis();
- if(currentMillis - previousMillis > strobe_delay) {
- // save the last time you blinked the LED
- previousMillis = currentMillis;
- // if the LED is off turn it on and vice-versa:
- if (ledState == LOW)
- brightness = 255;
- ledState = brightness;
- delayMicroseconds(on_time);
- }
- else
- {
- ledState = LOW;
- }
- if (strobe_delay > 10000){
- delay((strobe_delay - on_time) / 1000);
- }else{
- delayMicroseconds(strobe_delay - on_time);
- }
- break;
- case randommode:
- flashamount = constrain(flashamount, 30, 260);
- strobe_delay = minimum_delay + flashamount * analog_value_multiplier;
- randomSeed(analogRead(6));
- if (millis() > delayCtr) {
- delayCtr = millis() + strobe_delay;
- for (int x=0; x<3; x++) {
- INC[x] = (RGB1[x] - RGB2[x]) / 256; }
- for (int x=0; x<256; x++) {
- red = int(RGB1[0]);
- green = int(RGB1[1]);
- blue = int(RGB1[2]);
- analogWrite (REDLEDPIN, red);
- analogWrite (GREENLEDPIN, green);
- analogWrite (BLUELEDPIN, blue);
- if (millis() > flashamount) {
- delayCtr = millis() + (strobe_delay * 25); // the bigger the number (max 50) after * the slower it will change color
- RGB1[0] -= INC[0];
- RGB1[1] -= INC[1];
- RGB1[2] -= INC[2];
- }
- for (int x=0; x<3; x++) {
- RGB2[x] = random(556)-300;
- RGB2[x] = constrain(RGB2[x], 0, 255);
- }
- }
- }
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement