Guest User

Untitled

a guest
Nov 13th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.54 KB | None | 0 0
  1. /*
  2.   Nathan Seidle
  3.   SparkFun Electronics 2011
  4.  
  5.   This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
  6.  
  7.   Controlling an LED strip with individually controllable RGB LEDs. This stuff is awesome.
  8.  
  9.   The SparkFun (individually controllable) RGB strip contains a bunch of WS2801 ICs. These
  10.   are controlled over a simple data and clock setup. The WS2801 is really cool! Each IC has its
  11.   own internal clock so that it can do all the PWM for that specific LED for you. Each IC
  12.   requires 24 bits of 'greyscale' data. This means you can have 256 levels of red, 256 of blue,
  13.   and 256 levels of green for each RGB LED. REALLY granular.
  14.  
  15.   To control the strip, you clock in data continually. Each IC automatically passes the data onto
  16.   the next IC. Once you pause for more than 500us, each IC 'posts' or begins to output the color data
  17.   you just clocked in. So, clock in (24bits * 32LEDs = ) 768 bits, then pause for 500us. Then
  18.   repeat if you wish to display something new.
  19.  
  20.   This example code will display bright red, green, and blue, then 'trickle' random colors down
  21.   the LED strip.
  22.  
  23.   You will need to connect 5V/Gnd from the Arduino (USB power seems to be sufficient).
  24.  
  25.   For the data pins, please pay attention to the arrow printed on the strip. You will need to connect to
  26.   the end that is the begining of the arrows (data connection)--->
  27.  
  28.   If you have a 4-pin connection:
  29.   Blue = 5V
  30.   Red = SDI
  31.   Green = CKI
  32.   Black = GND
  33.  
  34.   If you have a split 5-pin connection:
  35.   2-pin Red+Black = 5V/GND
  36.   Green = CKI
  37.   Red = SDI
  38.  */
  39.  
  40. int SDI = 2; //Red wire (not the red 5V wire!)
  41. int CKI = 3; //Green wire
  42. int ledPin = 13; //On board LED
  43.  
  44. #define STRIP_LENGTH 32 //32 LEDs on this strip
  45. long strip_colors[STRIP_LENGTH];
  46.  
  47. void setup() {
  48.   pinMode(SDI, OUTPUT);
  49.   pinMode(CKI, OUTPUT);
  50.   pinMode(ledPin, OUTPUT);
  51.  
  52.   //Clear out the array
  53.   for(int x = 0 ; x < STRIP_LENGTH ; x++)
  54.     strip_colors[x] = 0;
  55.    
  56.   randomSeed(analogRead(0));
  57.  
  58.   //Serial.begin(9600);
  59.   //Serial.println("Hello!");
  60. }
  61.  
  62. void loop() {
  63.   //Pre-fill the color array with known values
  64.   strip_colors[0] = 0xFF0000; //Bright Red
  65.   strip_colors[1] = 0x00FF00; //Bright Green
  66.   strip_colors[2] = 0x0000FF; //Bright Blue
  67.   strip_colors[3] = 0x010000; //Faint red
  68.   strip_colors[4] = 0x800000; //1/2 red (0x80 = 128 out of 256)
  69.   post_frame(); //Push the current color frame to the strip
  70.  
  71.   delay(2000);
  72.  
  73.   while(1){ //Do nothing
  74.     addRandom();
  75.     post_frame(); //Push the current color frame to the strip
  76.  
  77.     digitalWrite(ledPin, HIGH);   // set the LED on
  78.     delay(250);                  // wait for a second
  79.     digitalWrite(ledPin, LOW);    // set the LED off
  80.     delay(250);                  // wait for a second
  81.   }
  82. }
  83.  
  84. //Throws random colors down the strip array
  85. void addRandom(void) {
  86.   int x;
  87.  
  88.   //First, shuffle all the current colors down one spot on the strip
  89.   for(x = (STRIP_LENGTH - 1) ; x > 0 ; x--)
  90.     strip_colors[x] = strip_colors[x - 1];
  91.    
  92.   //Now form a new RGB color
  93.   long new_color = 0;
  94.   for(x = 0 ; x < 3 ; x++){
  95.     new_color <<= 8;
  96.     new_color |= random(0xFF); //Give me a number from 0 to 0xFF
  97.     //new_color &= 0xFFFFF0; //Force the random number to just the upper brightness levels. It sort of works.
  98.   }
  99.  
  100.   strip_colors[0] = new_color; //Add the new random color to the strip
  101. }
  102.  
  103. //Takes the current strip color array and pushes it out
  104. void post_frame (void) {
  105.   //Each LED requires 24 bits of data
  106.   //MSB: R7, R6, R5..., G7, G6..., B7, B6... B0
  107.   //Once the 24 bits have been delivered, the IC immediately relays these bits to its neighbor
  108.   //Pulling the clock low for 500us or more causes the IC to post the data.
  109.  
  110.   for(int LED_number = 0 ; LED_number < STRIP_LENGTH ; LED_number++) {
  111.     long this_led_color = strip_colors[LED_number]; //24 bits of color data
  112.  
  113.     for(byte color_bit = 23 ; color_bit != 255 ; color_bit--) {
  114.       //Feed color bit 23 first (red data MSB)
  115.      
  116.       digitalWrite(CKI, LOW); //Only change data when clock is low
  117.      
  118.       long mask = 1L << color_bit;
  119.       //The 1'L' forces the 1 to start as a 32 bit number, otherwise it defaults to 16-bit.
  120.      
  121.       if(this_led_color & mask)
  122.         digitalWrite(SDI, HIGH);
  123.       else
  124.         digitalWrite(SDI, LOW);
  125.  
  126.       digitalWrite(CKI, HIGH); //Data is latched when clock goes high
  127.     }
  128.   }
  129.  
  130.   //Pull clock low to put strip into reset/post mode
  131.   digitalWrite(CKI, LOW);
  132.   delayMicroseconds(500); //Wait for 500us to go into reset
  133. }
Add Comment
Please, Sign In to add comment