Advertisement
Guest User

Wooden USB-powered RGB-LED lamp sketch

a guest
Jan 11th, 2016
600
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1.  
  2. #include <FastLED.h>
  3. #define NUM_LEDS 5
  4. #define DATA_PIN 3
  5.  
  6. int hue = 128;
  7. int saturation = 255;
  8. int brightness = 255;
  9. int cosBright[] = { 250, 248, 243, 236, 226, 213, 198, 181, 163, 144, 125, 105, 86, 68, 51, 36, 23, 13, 6, 1, 0, 1, 6, 13, 23, 36, 51, 68, 86, 105, 125, 144, 163, 181, 198, 213, 226, 236, 243, 248 };
  10.  
  11. unsigned long updateTime = 25;
  12. unsigned long partyTime = 520;
  13. unsigned long startTime = 0;
  14. unsigned long timeoutTime = 5000000;
  15. unsigned long clickDuration;
  16.  
  17. int state = 1;
  18. int index = 0;
  19. int stufe = 0;
  20.  
  21. CRGB leds[NUM_LEDS];
  22.  
  23. void setup() {
  24. FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
  25. pinMode(4, INPUT);
  26. }
  27.  
  28. void loop() {
  29.  
  30. switch (state) {
  31.  
  32. case 1:
  33. saturation = 255;
  34. brightness = 255;
  35. if (millis() - startTime > updateTime) {
  36. for (int dot = 0; dot < NUM_LEDS; dot++) {
  37. leds[dot].setHSV( hue, saturation, brightness);
  38. }
  39. startTime = millis();
  40. }
  41. FastLED.show();
  42. clickDuration = pulseIn(4, HIGH, timeoutTime);
  43. if (clickDuration > 10 && clickDuration < 1000000) {
  44. clickDuration = pulseIn(4, HIGH, 300000);
  45. if (clickDuration > 50000 && clickDuration < 200000) {
  46. state = 3;
  47. }
  48. else {
  49. hue = hue + 16;
  50. hue = hue % 256;
  51. }
  52. }
  53. else if (clickDuration >= 1000000) {
  54. state = 2;
  55.  
  56. }
  57. break;
  58.  
  59. case 2:
  60. while (digitalRead(4) == LOW) {
  61. saturation = 255;
  62. brightness = cosBright[index];
  63. constrain(brightness, 10, 255);
  64. index++;
  65. index = index % 40;
  66.  
  67. hue = hue + 1;
  68. hue = hue % 256;
  69.  
  70. for (int dot = 0; dot < NUM_LEDS; dot++) {
  71. leds[dot].setHSV( (hue + (dot * 60)), 255, brightness);
  72.  
  73. }
  74.  
  75. FastLED.show();
  76. delay(partyTime / 40); //geht schöner
  77. }
  78. state = 1;
  79. break;
  80.  
  81. case 3:
  82. brightness = (64 * (stufe + 1)) - 1;
  83. saturation = 0;
  84. if (millis() - startTime > updateTime) {
  85. for (int dot = 0; dot < NUM_LEDS; dot++) {
  86. leds[dot].setHSV( hue, saturation, brightness);
  87.  
  88. }
  89. startTime = millis();
  90. }
  91. FastLED.show();
  92. clickDuration = pulseIn(4, HIGH, timeoutTime);
  93. if (clickDuration > 10 && clickDuration < 1000000) {
  94. clickDuration = pulseIn(4, HIGH, 300000);
  95. if (clickDuration > 50000 && clickDuration < 200000) {
  96. state = 1;
  97. stufe = 0;
  98. }
  99. else {
  100. stufe = stufe + 1;
  101. stufe = stufe % 4;
  102. }
  103. }
  104. else if (clickDuration >= 1000000) {
  105. state = 2;
  106.  
  107. }
  108. break;
  109.  
  110. }
  111.  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement