Advertisement
DerMarten

[Arduino]WS2801 FAST LED

Sep 17th, 2017
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.08 KB | None | 0 0
  1. #include "FastLED.h"
  2.  
  3. //Tell it how many leds are in the strip. AndyMark's 2.5 meter strip has 80 leds
  4. #define NUM_LEDS 64
  5. // This is an array of leds. One item for each led in your strip.
  6. CRGB leds[NUM_LEDS];
  7.  
  8. //CSK 3/17/2014 I moved these to pins that don't conflict with Ethernet functions in case you want to control LEDs via Ethernet
  9. #define DATA_PIN 2 //Green wire from AM-2640's power connector
  10. // Clock pin SPI
  11. #define CLOCK_PIN 3 //Blue wire from AM-2640's power connector
  12.  
  13. #define MAX_BRIGHTNESS 100
  14.  
  15.  
  16. String incomingString = "rainbow";   // for incoming serial data
  17. String oldAnim;
  18.  
  19. //This function is used to setup things like pins, Serial ports etc.
  20. //Here we specify which chipset our LEDs run off of by our choice of config function
  21. void setup()
  22. {
  23.   Serial.begin(9600);
  24.   Serial.setTimeout(10);
  25.  
  26.   //attachInterrupt(digitalPinToInterrupt(interruptPin), serial_read, HIGH);
  27.   Serial.println("[Warning]: Warnung vor photosensitiven Anfällen !!!");
  28.   Serial.println("[INFO]: Animationen rainbow, cylon, color_chase, missing_dot_chase, test2, police");
  29.   //***This is the chipset in the AM-2640 LED strip***
  30.   //CSK 3/17/2013 Changed to this function to allow direct data and clock pin specification
  31.   FastLED.addLeds<WS2801, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
  32. }
  33. void loop()
  34. {
  35.   //This is kind of Arduino's equivalent to Main() in a standard C program
  36.   //This, as the name implies, loops endlessly.
  37.   //https://code.google.com/p/fastspi/wiki/CRGBreference
  38.   //CSK 3/20/2014 I added a rainbow function just for grins
  39.   // send data only when you receive data:
  40.         if (Serial.available() > 0) {
  41.                 // read the incoming byte:
  42.                 incomingString = Serial.readString();
  43.  
  44.                 // say what you got:
  45.                 Serial.println("[Serial][in]: Animation '" + incomingString + "' wird gestartet");
  46.         }
  47.   if(incomingString == "rainbow"){
  48.     rainbow(20);
  49.   }
  50.   else if(incomingString == "cylon"){
  51.     cylon(CRGB::Aqua,25 ,15);  
  52.   }
  53.   else if(incomingString == "color_chase"){
  54.     color_chase(CRGB::Cyan, 15);
  55.   }
  56.   else if(incomingString == "missing_dot_chase"){
  57.     missing_dot_chase(0x3000cc, 20);  
  58.   }
  59.   else if(incomingString == "test2"){
  60.     test2(80);  
  61.   }
  62.   else if(incomingString == "police"){
  63.     police(150);  
  64.   }
  65.  
  66. }
  67. void printAnimation(String text){
  68.   Serial.println("[Animation]: " + text);
  69.   return;
  70. }
  71. void police(uint8_t wait){
  72.   CRGB left = CRGB::Red;
  73.   CRGB right = CRGB::Blue;
  74.  
  75.   FastLED.setBrightness(MAX_BRIGHTNESS);
  76.  
  77.   if(oldAnim != "police"){
  78.       printAnimation("police");    
  79.     }
  80.     oldAnim = "police";
  81.   FastLED.clear();
  82.   for(int led_nummer = 0; led_nummer < NUM_LEDS/2; led_nummer++){
  83.     leds[led_nummer] = left;
  84.   }
  85.   FastLED.show();
  86.   FastLED.clear();
  87.   delay(wait/2);
  88.   for(int led_nummer = 0; led_nummer < NUM_LEDS/2; led_nummer++){
  89.     leds[led_nummer] = CRGB::Black;
  90.   }
  91.   FastLED.show();
  92.   FastLED.clear();
  93.   delay(wait/2);
  94.   for(int led_nummer = 0; led_nummer < NUM_LEDS/2; led_nummer++){
  95.     leds[led_nummer] = left;
  96.   }
  97.   FastLED.show();
  98.   delay(wait/2);
  99.   for(int led_nummer = 0; led_nummer < NUM_LEDS/2; led_nummer++){
  100.     leds[led_nummer] = CRGB::Black;
  101.   }
  102.   FastLED.show();
  103.   FastLED.clear();
  104.   delay(wait/2);
  105.   for(int led_nummer = 0; led_nummer < NUM_LEDS/2; led_nummer++){
  106.     leds[led_nummer] = left;
  107.   }
  108.   FastLED.show();
  109.  
  110.   ///////////////////
  111.   delay(wait);
  112.   ///////////////////
  113.   FastLED.clear();
  114.   for(int led_nummer = NUM_LEDS/2; led_nummer < NUM_LEDS; led_nummer++){
  115.     leds[led_nummer] = right;
  116.   }
  117.   FastLED.show();
  118.   FastLED.clear();
  119.   delay(wait/2);
  120.   for(int led_nummer = NUM_LEDS/2; led_nummer < NUM_LEDS; led_nummer++){
  121.     leds[led_nummer] = CRGB::Black;
  122.   }
  123.   FastLED.show();
  124.   FastLED.clear();
  125.   delay(wait/2);
  126.   for(int led_nummer = NUM_LEDS/2; led_nummer < NUM_LEDS; led_nummer++){
  127.     leds[led_nummer] = right;
  128.   }
  129.   FastLED.show();
  130.   delay(wait/2);
  131.   for(int led_nummer = NUM_LEDS/2; led_nummer < NUM_LEDS; led_nummer++){
  132.     leds[led_nummer] = CRGB::Black;
  133.   }
  134.   FastLED.show();
  135.   FastLED.clear();
  136.   delay(wait/2);
  137.   for(int led_nummer = NUM_LEDS/2; led_nummer < NUM_LEDS; led_nummer++){
  138.     leds[led_nummer] = right;
  139.   }
  140.   FastLED.show();
  141.   delay(wait);
  142. }
  143.  
  144. //These are the functions we have defined to do chase patterns.  They are actually called inside the  loop() above
  145. //They are meant to demonstrate things such as setting LED colors, controlling brightness
  146. void color_chase(uint32_t color, uint8_t wait){
  147.   if(oldAnim != "color_chase"){
  148.       printAnimation("color_chase");    
  149.     }
  150.     oldAnim = "color_chase";
  151.   //clear() turns all LEDs off
  152.   FastLED.clear();
  153.   //The brightness ranges from 0-255
  154.   //Sets brightness for all LEDS at once
  155.   FastLED.setBrightness(MAX_BRIGHTNESS);
  156.   // Move a single led
  157.   for(int led_number = 0; led_number < NUM_LEDS; led_number++)
  158.   {
  159.     // Turn our current led ON, then show the leds
  160.     leds[led_number] = color;
  161.  
  162.     // Show the leds (only one of which is has a color set, from above
  163.     // Show turns actually turns on the LEDs
  164.     FastLED.show();
  165.  
  166.     // Wait a little bit
  167.     delay(wait);
  168.  
  169.     // Turn our current led back to black for the next loop around
  170.     leds[led_number] = CRGB::Black;
  171.   }
  172.   return;
  173. }
  174. void test2(uint8_t wait){
  175.  
  176.   if(oldAnim != "test2"){
  177.       printAnimation("test2");    
  178.   }
  179.     oldAnim = "test2";
  180.   for(int led_number = 0; led_number < NUM_LEDS; led_number++)
  181.   {
  182.     FastLED.clear();
  183.      randomSeed(millis());
  184.      //Eine LED
  185.     FastLED.setBrightness(random(255));
  186.     leds[random(NUM_LEDS)] = CRGB::Aqua; //CRGB(random(255),random(255),random(255));
  187.     delay(wait);
  188.     FastLED.show();
  189.   }
  190.  
  191.  
  192.     //delay(wait);
  193. }
  194. void test(uint8_t wait)
  195. {
  196.    
  197.     if(oldAnim != "test"){
  198.       printAnimation("test");    
  199.     }
  200.     oldAnim = "test";
  201.     randomSeed(millis());
  202.     FastLED.clear();
  203.     //Eine LED
  204.     FastLED.setBrightness(random(255));
  205.     leds[random(NUM_LEDS)] = CRGB(random(255),random(255),random(255));
  206.     //Zweite LED
  207.     FastLED.setBrightness(random(255));
  208.     leds[random(NUM_LEDS)] = CRGB(random(255),random(255),random(255));
  209.     //Dritte LED
  210.     FastLED.setBrightness(random(255));
  211.     leds[random(NUM_LEDS)] = CRGB(random(255),random(255),random(255));
  212.     //Vierte LED
  213.     FastLED.setBrightness(random(255));
  214.     leds[random(NUM_LEDS)] = CRGB(random(255),random(255),random(255));
  215.     FastLED.show();
  216.     delay(wait);
  217.  
  218.    
  219. }
  220. //Move an "empty" dot down the strip
  221. void missing_dot_chase(uint32_t color, uint8_t wait)
  222. {
  223.   if(oldAnim != "missing_dot_chase"){
  224.     printAnimation("missing_dot_chase");    
  225.   }
  226.   oldAnim = "missing_dot_chase";
  227.  
  228.   //Step thru some brightness levels from max to 10.  led_brightness/=2 is a cryptic shorthand way of saying led_brightness = led_brightness/2
  229.   for (int led_brightness = MAX_BRIGHTNESS; led_brightness > 10; led_brightness/=2)
  230.   {
  231.     FastLED.setBrightness(led_brightness);
  232.  
  233.     // Start by turning all pixels on:
  234.     //for(int led_number = 0; led_number < NUM_LEDS; led_number++) leds[led_number] = color;
  235.     //https://github.com/FastLED/FastLED/wiki/Controlling-leds
  236.     fill_solid(leds, NUM_LEDS, color);
  237.  
  238.     // Then display one pixel at a time:
  239.     for(int led_number = 0; led_number < NUM_LEDS; led_number++)
  240.     {
  241.       leds[led_number] =  CRGB::Black; // Set new pixel 'off'
  242.       if( led_number > 0 && led_number < NUM_LEDS)
  243.       {
  244.         leds[led_number-1] = color; // Set previous pixel 'on'
  245.       }
  246.       FastLED.show();
  247.       delay(wait);
  248.     }
  249.   }
  250.   return;
  251. }
  252.  
  253. //Cylon - LED sweeps back and forth, with the color, delay and number of cycles of your choice
  254. void cylon(CRGB color, uint16_t wait, uint8_t number_of_cycles)
  255. {
  256.   if(oldAnim != "cylon"){
  257.     printAnimation("cylon");    
  258.   }
  259.   oldAnim = "cylon";
  260.   FastLED.setBrightness(255);
  261.   for (uint8_t times = 0; times<=number_of_cycles; times++)
  262.   {
  263.     // Make it look like one LED is moving in one direction
  264.     for(int led_number = 0; led_number < NUM_LEDS; led_number++)
  265.     {
  266.       //Apply the color that was passed into the function
  267.       leds[led_number] = color;
  268.       //Actually turn on the LED we just set
  269.       FastLED.show();
  270.       // Turn it back off
  271.       leds[led_number] = CRGB::Black;
  272.       // Pause before "going" to next LED
  273.       delay(wait);
  274.     }
  275.  
  276.     // Now "move" the LED the other direction
  277.     for(int led_number = NUM_LEDS-1; led_number >= 0; led_number--)
  278.     {
  279.       //Apply the color that was passed into the function
  280.       leds[led_number] = color;
  281.       //Actually turn on the LED we just set
  282.       FastLED.show();
  283.       // Turn it back off
  284.       leds[led_number] = CRGB::Black;
  285.       // Pause before "going" to next LED
  286.       delay(wait);
  287.     }
  288.   }
  289.   return;
  290. }
  291.  
  292. void rainbow(uint8_t wait)
  293. {
  294.   if(oldAnim != "rainbow"){
  295.     printAnimation("rainbow");    
  296.   }
  297.   oldAnim = "rainbow";
  298.   uint16_t hue;
  299.   FastLED.clear();
  300.  
  301.   for(hue=10; hue<255*3; hue++)
  302.   {
  303.  
  304.     fill_rainbow( &(leds[0]), NUM_LEDS /*led count*/, hue /*starting hue*/);    
  305.     FastLED.show();
  306.     delay(wait);
  307.   }
  308.   return;
  309. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement