Tjakka5

Untitled

Sep 26th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.93 KB | None | 0 0
  1. #include <EEPROM.h>
  2. #include "FastLED.h"
  3.  
  4. #define BUTTON_PIN   3
  5. #define LED_PIN      6
  6. #define NUM_LEDS     80
  7. #define RING_SIZE    5 // Leds on the horizontal plane
  8. #define MODE_ADDRESS 0 // Address to store current lamp mode in
  9.  
  10. #define SPIRAL_COUNT NUM_LEDS/RING_SIZE // Leds on the vertical plane
  11.  
  12. bool buttonState;
  13. bool lastButtonState = LOW;
  14.  
  15. unsigned long lastDebounceTime = 0;
  16. unsigned long debounceDelay    = 1;
  17.  
  18. /*
  19. 'mode' holds the current lamp mode.
  20.   0:  Solid white
  21.   1:  Breathing red
  22.   2:  Breathing green
  23.   3:  Breathing blue
  24.   4:  Rainbow
  25.   5:  Move to music
  26.   6:  Spiral
  27.   7:  Ring
  28.   8:  Fire (Red > Orange)
  29.   9:  Fire2 (Blue > Cyan)
  30.   10: Sombra
  31.   11: Rave
  32. */
  33. int mode;       // Current mode
  34. int modes = 12; // Maximum modes
  35.  
  36. // Array of all the LEDS
  37. CRGB leds[NUM_LEDS];
  38.  
  39. void setup() {
  40.   // Initialize LED strip
  41.   FastLED.addLeds<WS2811, LED_PIN, GRB>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  42.   FastLED.setMaxPowerInVoltsAndMilliamps(5, 1000);
  43.  
  44.   // Initialize button
  45.   pinMode(BUTTON_PIN, INPUT);
  46.  
  47.   // Read memory for current address
  48.   EEPROM.get(MODE_ADDRESS, mode);
  49. }
  50.  
  51. void loop() {
  52.   // Get button input
  53.   int reading = digitalRead(BUTTON_PIN);
  54.  
  55.   // If the switch changed, due to noise or pressing:
  56.   if (reading != lastButtonState) {
  57.     // reset the debouncing timer
  58.     lastDebounceTime = millis();
  59.   }
  60.  
  61.   // Account for debouncing
  62.   if ((millis() - lastDebounceTime) > debounceDelay) {
  63.     // if the button state has changed:
  64.     if (reading != buttonState) {
  65.       buttonState = reading;
  66.  
  67.       // Increment state when button is pressed
  68.       if (buttonState == HIGH) {
  69.         mode++;
  70.  
  71.         // Loop around if we have hit the last state
  72.         if (mode == modes) {
  73.           mode = 0;
  74.         }
  75.  
  76.         // Save the mode to memory
  77.         EEPROM.put(MODE_ADDRESS, mode);
  78.       }
  79.     }
  80.   }
  81.  
  82.   // Save the reading.,
  83.   lastButtonState = reading;
  84.  
  85.  
  86.   // Default brightness to 255, effects may change this
  87.   FastLED.setBrightness(255);
  88.   // Reset all leds
  89.   fill_solid(leds, NUM_LEDS, CRGB::Black);
  90.  
  91.   // Choose effect depending on 'state'
  92.   switch(mode) {
  93.     case 0:
  94.       solidWhite(); break;
  95.     case 1:
  96.       breathingRed(); break;
  97.     case 2:
  98.       breathingGreen(); break;
  99.     case 3:
  100.       breathingBlue(); break;
  101.     case 4:
  102.       rainbow(); break;
  103.     case 5:
  104.       music(); break;
  105.     case 6:
  106.       spiral(); break;
  107.     case 7:
  108.       ring(); break;
  109.     case 8:
  110.       fire(); break;
  111.     case 9:
  112.       fire2(); break;
  113.     case 10:
  114.       sombra(); break;
  115.     case 11:
  116.       rave(); break;
  117.     default:
  118.       break;
  119.   }
  120.  
  121.   // Display LEDS
  122.   LEDS.show();
  123. }
  124.  
  125. void solidWhite() {
  126.   fill_solid(leds, NUM_LEDS, CRGB::White);
  127. }
  128.  
  129. void breathingRed() {
  130.   static float breathingSpeed = 5000.0f;
  131.   static int   minBrightness  = 40;
  132.   static int   maxBrightness  = 255;
  133.    
  134.   fill_solid(leds, NUM_LEDS, CRGB::Red);
  135.  
  136.   float breath = (exp(sin(millis()/breathingSpeed*PI)) - 0.36787944)*108.0;
  137.   FastLED.setBrightness(map(breath, 0, 255, minBrightness, maxBrightness));
  138. }
  139.  
  140.  
  141. void breathingGreen() {
  142.   static float breathingSpeed = 5000.0f;
  143.   static int   minBrightness  = 40;
  144.   static int   maxBrightness  = 255;
  145.    
  146.   fill_solid(leds, NUM_LEDS, CRGB::Green);
  147.  
  148.   float breath = (exp(sin(millis()/breathingSpeed*PI)) - 0.36787944)*108.0;
  149.   FastLED.setBrightness(map(breath, 0, 255, minBrightness, maxBrightness));
  150. }
  151.  
  152. void breathingBlue() {
  153.   static float breathingSpeed = 5000.0f;
  154.   static int   minBrightness  = 40;
  155.   static int   maxBrightness  = 255;
  156.    
  157.   fill_solid(leds, NUM_LEDS, CRGB::Blue);
  158.  
  159.   float breath = (exp(sin(millis()/breathingSpeed*PI)) - 0.36787944)*108.0;
  160.   FastLED.setBrightness(map(breath, 0, 255, minBrightness, maxBrightness));
  161. }
  162.  
  163. void rainbow() {
  164.   static float movingSpeed = 0.2f;
  165.   static float offset      = 0;
  166.   static float delta       = 2.0f;
  167.  
  168.   for (int i = 0; i < NUM_LEDS; i++) {
  169.    leds[i].setHSV(offset + (i * delta), 255, 255);
  170.   }
  171.  
  172.   offset += movingSpeed;
  173. }
  174.  
  175. void music() {
  176. }
  177.  
  178. void spiral() {
  179.   static CRGB primaryColor   = CRGB::Green;
  180.   static CRGB secondaryColor = CRGB::Cyan;
  181.   static int tailLength      = 12;
  182.   static int waitTime        = 50;
  183.  
  184.   static int spiral = 0;
  185.   static int pos    = 0;
  186.  
  187.   fill_solid(leds, NUM_LEDS, primaryColor);
  188.  
  189.   for (int i = pos; i > pos-tailLength; i--) {
  190.     int realPos = (i * 5) + spiral;
  191.     if (realPos > -1) {
  192.       leds[realPos] = secondaryColor;
  193.     }
  194.   }
  195.  
  196.   if (pos == 15 + tailLength) {
  197.     pos = 0;
  198.     spiral = spiral + 1;
  199.   }
  200.  
  201.   pos++;
  202.   delay(waitTime);
  203. }
  204.  
  205. void ring() {
  206.   static CRGB primaryColor   = CRGB::Green;
  207.   static CRGB secondaryColor = CRGB::Cyan;
  208.   static int waitTime        = 100;
  209.  
  210.   static int pos = 0;
  211.   static int dir = 1;
  212.  
  213.   fill_solid(leds, NUM_LEDS, primaryColor);
  214.  
  215.   for (int i = 0; i < 5; i++) {
  216.     int realPos = (pos * 5) + i;
  217.     leds[realPos] = secondaryColor;
  218.   }
  219.  
  220.   if (pos == 0) {
  221.     dir = 1;
  222.   }
  223.   if (pos == 15) {
  224.     dir = -1;
  225.   }
  226.  
  227.   pos += dir;
  228.  
  229.   delay(waitTime);
  230. }
  231.  
  232. void fire() {
  233.   static CHSV  primaryColor   = CHSV(0, 255, 255);
  234.   static CHSV  secondaryColor = CHSV(50, 255, 150);
  235.   static float flickerTime    = 60.0f;
  236.   static int   intensity      = 3;
  237.   static int   endPos         = 40;
  238.   static int   minPos         = 5;
  239.   static int   maxPos         = NUM_LEDS;
  240.  
  241.  
  242.   endPos += random(-1, 2) * intensity;
  243.  
  244.   if (endPos <= minPos) {
  245.     endPos = minPos;
  246.   } else if (endPos >= maxPos) {
  247.     endPos = maxPos;
  248.   }
  249.  
  250.   fill_gradient(leds, 0, primaryColor, endPos, secondaryColor, SHORTEST_HUES);
  251.  
  252.   delay(flickerTime);
  253. }
  254.  
  255.  
  256. void fire2() {
  257.   static CHSV  primaryColor   = CHSV(150, 255, 255);
  258.   static CHSV  secondaryColor = CHSV(110, 255, 255);
  259.   static float flickerTime    = 60.0f;
  260.   static int   intensity      = 3;
  261.   static int   endPos         = 40;
  262.   static int   minPos         = 5;
  263.   static int   maxPos         = NUM_LEDS;
  264.  
  265.  
  266.   endPos += random(-1, 2) * intensity;
  267.  
  268.   if (endPos <= minPos) {
  269.     endPos = minPos;
  270.   } else if (endPos >= maxPos) {
  271.     endPos = maxPos;
  272.   }
  273.  
  274.   fill_gradient(leds, 0, primaryColor, endPos, secondaryColor, SHORTEST_HUES);
  275.  
  276.   delay(flickerTime);
  277. }
  278.  
  279. void sombra() {
  280.   static CRGB  primaryColor   = CHSV(210, 255, 255);
  281.   static CHSV  secondaryColor = CHSV(210, 140, 140);
  282.   static int   pos            = 0;
  283.   static int   tailLength     = 50;
  284.   static float waitTime       = 30.0f;
  285.  
  286.   fill_solid(leds, NUM_LEDS, secondaryColor);
  287.  
  288.   for (int i = 0; i < tailLength; i++) {
  289.     int realPos = pos + i;
  290.     if (realPos >= 0 && realPos <= NUM_LEDS) {
  291.       leds[pos + i] = primaryColor;
  292.     }
  293.   }
  294.  
  295.   if (pos > NUM_LEDS) {
  296.     pos = -tailLength;
  297.   }
  298.  
  299.   pos++;
  300.  
  301.   delay(waitTime);
  302. }
  303.  
  304. void rave() {
  305.   static float waitTime = 150.0f;
  306.  
  307.   for (int i = 0; i < NUM_LEDS; i++) {
  308.     leds[i].setRGB(random(30, 176), random(30, 176), random(30, 176));
  309.   }
  310.  
  311.   delay(waitTime);
  312. }
Add Comment
Please, Sign In to add comment