Advertisement
DarioCas

Arduino LED Strip Projekt

Dec 16th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Adafruit_NeoPixel.h>
  2. #define PIN 4
  3. #define NUM_LIGHTS  50
  4.  
  5. // =========================================================================================================
  6.  
  7. Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LIGHTS, PIN, NEO_GRB + NEO_KHZ800);
  8.  
  9. int currentColorValueRed;
  10. int currentColorValueGreen;
  11. int currentColorValueBlue;
  12. int colorData;
  13. int brightnessValue;
  14. int currentMode;
  15. int counter = 1;
  16. int colorValueRed;
  17. int colorValueGreen;
  18. int colorValueBlue;
  19. int fadeValue;
  20. int buttonState = 0;
  21. int potPinValue;
  22. int delayValue;
  23. int speedValue;
  24.  
  25. int ColorPotPin = A3;
  26. int brightnessPotPin = A4;
  27. int modeButtonPin = 2;
  28.  
  29. // =========================================================================================================
  30.  
  31. void setup() {
  32.     pinMode(ColorPotPin, INPUT);
  33.     pinMode(brightnessPotPin, INPUT);
  34.     pinMode(modeButtonPin, INPUT);
  35.     pinMode(13, OUTPUT);
  36.     Serial.begin(9600);
  37.     strip.begin();
  38.     strip.show(); // Initialize all pixels to 'off'
  39. }
  40.  
  41. // =========================================================================================================
  42.  
  43. void loop() {
  44.  
  45.  
  46.     int brightnessPot = map(analogRead(brightnessPotPin), 0, 1024, 1, 255);
  47.  
  48.     float brightnessValue = brightnessPot / 255.0;
  49.  
  50. // =========================================================================================================
  51.  
  52.     if (potPinValue <= 42.5) {
  53.     currentColorValueRed = 255;
  54.     currentColorValueGreen = potPinValue * 6;
  55.     currentColorValueBlue = 0;
  56.     }
  57.  
  58.     if ((potPinValue > 42.5) && (potPinValue <= 85)) {
  59.     currentColorValueRed = 255 - (potPinValue - 43) * 6;
  60.     currentColorValueGreen = 255;
  61.     currentColorValueBlue = 0;
  62.     }
  63.  
  64.     if ((potPinValue > 85) && (potPinValue <= 127.5)) {
  65.     currentColorValueRed = 0;
  66.     currentColorValueGreen = 255;
  67.     currentColorValueBlue = (potPinValue - 85) * 6;   }
  68.  
  69.     if ((potPinValue > 127.5) && (potPinValue <= 170)) {
  70.     currentColorValueRed = 0;
  71.     currentColorValueGreen = 255 - (potPinValue - 127.5) * 6;
  72.     currentColorValueBlue = 255;
  73.     }
  74.  
  75.     if ((potPinValue > 170) && (potPinValue <= 212.5)) {
  76.     currentColorValueRed = (potPinValue - 170) * 6;
  77.     currentColorValueGreen = 0;
  78.     currentColorValueBlue = 255;
  79.     }
  80.  
  81.     if ((potPinValue > 212.5) && (potPinValue <= 255)) {
  82.     currentColorValueRed = 255;
  83.     currentColorValueGreen = 0;
  84.     currentColorValueBlue = 255 - (potPinValue - 212.5) * 6;
  85.     }
  86.  
  87.  
  88. // =========================================================================================================
  89.  
  90.  
  91.     if (fadeValue <= 42.5) {
  92.     colorValueRed = 255;
  93.     colorValueGreen = fadeValue * 6;
  94.     colorValueBlue = 0;
  95.     }
  96.  
  97.     if ((fadeValue > 42.5) && (fadeValue <= 85)) {
  98.     colorValueRed = 255 - (fadeValue - 43) * 6;
  99.     colorValueGreen = 255;
  100.     colorValueBlue = 0;
  101.     }
  102.  
  103.     if ((fadeValue > 85) && (fadeValue <= 127.5)) {
  104.     colorValueRed = 0;
  105.     colorValueGreen = 255;
  106.     colorValueBlue = (fadeValue - 85) * 6;   }
  107.  
  108.     if ((fadeValue > 127.5) && (fadeValue <= 170)) {
  109.     colorValueRed = 0;
  110.     colorValueGreen = 255 - (fadeValue - 127.5) * 6;
  111.     colorValueBlue = 255;
  112.     }
  113.  
  114.     if ((fadeValue > 170) && (fadeValue<= 212.5)) {
  115.     colorValueRed = (fadeValue - 170) * 6;
  116.     colorValueGreen = 0;
  117.     colorValueBlue = 255;
  118.     }
  119.  
  120.     if ((fadeValue > 212.5) && (fadeValue <= 255)) {
  121.     colorValueRed = 255;
  122.     colorValueGreen = 0;
  123.     colorValueBlue = 255 - (fadeValue - 212.5) * 6;
  124.     }
  125.  
  126.  
  127. // =========================================================================================================
  128.  
  129.    
  130.  
  131.     if (fadeValue == 256) {
  132.         fadeValue = 0;
  133.     }
  134.  
  135.     if (fadeValue == 0) {
  136.         fadeValue = 1;
  137.     }
  138.  
  139. // =========================================================================================================
  140.  
  141.  
  142.     buttonState = digitalRead(2);
  143.  
  144.     if (buttonState == HIGH) {
  145.      
  146.         counter++;
  147.         delay(1000);
  148.    
  149.     if (counter == 6) {
  150.           counter = 1;
  151.         }
  152.         Serial.println(counter);
  153.     }
  154.  
  155. // =========================================================================================================
  156.    
  157.     delayValue = map(analogRead(ColorPotPin), 0, 1024, 500, 5);
  158.  
  159. // =========================================================================================================
  160.  
  161.  
  162.     if (counter == 1) {
  163.      
  164.         potPinValue = map(analogRead(ColorPotPin), 0, 1024, 3, 255);
  165.      
  166.         uint32_t high = strip.Color(currentColorValueRed, currentColorValueGreen, currentColorValueBlue);
  167.  
  168.         strip.setBrightness(brightnessPot);
  169.    
  170.         for( int i = 0; i<NUM_LIGHTS; i++){
  171.             strip.setPixelColor(i, high);
  172.             strip.show();
  173.         }
  174.     }
  175.  
  176. // =========================================================================================================
  177.  
  178.     if (counter == 2) {
  179.        
  180.         delayValue = map(analogRead(ColorPotPin), 0, 1024, 500, 1);
  181.        
  182.         uint32_t change = strip.Color(colorValueRed, colorValueGreen, colorValueBlue);
  183.  
  184.         strip.setBrightness(brightnessPot);
  185.    
  186.         for( int i = 0; i<NUM_LIGHTS; i++){
  187.             strip.setPixelColor(i, change);
  188.             strip.show();
  189.         }
  190.        
  191.      
  192.         if (fadeValue == 256) {
  193.           fadeValue = 0;
  194.         }
  195.      
  196.         else {
  197.            
  198.           fadeValue += 1;
  199.           delay(delayValue);
  200.    
  201.         }
  202.        
  203.      
  204.         delay(10);
  205.        
  206.     }
  207.  
  208. // =========================================================================================================
  209.  
  210.     if (counter == 3) {
  211.  
  212.        uint32_t change = strip.Color(255, 255, 255);
  213.  
  214.        strip.setBrightness(brightnessPot);
  215.  
  216.        for( int i = 0; i<NUM_LIGHTS; i++){
  217.             strip.setPixelColor(i, change);
  218.             strip.show();
  219.         }
  220.  
  221.     }
  222.  
  223. // =========================================================================================================
  224.  
  225.     if (counter == 4) {
  226.  
  227.       rainbow(2);
  228.      
  229.     }
  230.  
  231. // =========================================================================================================
  232.  
  233.     if (counter == 5) {
  234.  
  235.       int r = 255;
  236.       int g = 96;
  237.       int b = 12;
  238.      
  239.       for(int x = 0; x <50; x++)
  240.       {
  241.       int flicker = random(8,40);
  242.       int r1 = r-flicker;
  243.       int g1 = g-flicker;
  244.       int b1 = b-flicker;
  245.       if(g1<0) g1=0;
  246.       if(r1<0) r1=0;
  247.       if(b1<0) b1=0;
  248.       strip.setPixelColor(x,r1,g1, b1);
  249.  
  250.       strip.setBrightness(brightnessPot);
  251.  
  252.      
  253.       }
  254.       strip.show();
  255.       delay(random(40,125));
  256.      
  257.     }
  258.  
  259. }
  260.  
  261. // =========================================================================================================
  262.  
  263. void rainbow(uint8_t wait) {
  264.   uint16_t i, j;
  265.  
  266.   speedValue = map(analogRead(ColorPotPin), 0, 1024, 25, 1);
  267.  
  268.   for(j=0; j<256; j++) {
  269.     for(i=0; i<strip.numPixels(); i++) {
  270.       strip.setPixelColor(i, Wheel((i*3+j) & 255));
  271.  
  272.       int brightnessPot = map(analogRead(brightnessPotPin), 0, 1024, 1, 255);
  273.      
  274.       strip.setBrightness(brightnessPot);
  275.      
  276.       buttonState = digitalRead(2);
  277.       if (buttonState == HIGH) {
  278.         counter = 5;
  279.         delay(50);
  280.         Serial.println(counter);
  281.     }
  282.  
  283.     }
  284.     strip.show();
  285.     delay(speedValue);
  286.   }
  287. }
  288.  
  289.  
  290. uint32_t Wheel(byte WheelPos) {
  291.   if(WheelPos < 85) {
  292.     return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  293.   }
  294.   else if(WheelPos < 170) {
  295.     WheelPos -= 85;
  296.     return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  297.   }
  298.   else {
  299.     WheelPos -= 170;
  300.     return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  301.   }
  302. }
  303.  
  304. // =========================================================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement