Go Down

Topic: [Solved]Basic code to make WS2801 LED strip function. (Read 5273 times) previous topic - next topic

Kerby

Dec 17, 2013, 02:39 pm Last Edit: Dec 18, 2013, 03:37 am by Kerby Reason: 1
I am trying to locate a very basic piece of code to make a WS2801 LED strip function. I just want the minimal amount of code necessary to make the strip light up.

I have found code online to make it do all kinds of interesting things. But none of them explain well what they are doing in basic terms. And when I try to take the code apart to make it do what I want it to do. I mess things up.

I am using an Arduino Micro  SDI = pin 2, CKI = pin 3.

I don't want a bunch of libraries to use. I am just trying to understand the clocking and data manipulation. I have read the data sheets on the chips and quite honestly am overwhelmed.  I am a trial and error kind of guy and so if I had just the basics I can work on it from there.

Or if you could explain to me what is going on here.
Code: [Select]
//Takes the current strip color array and pushes it out
void post_frame (void) {
 //Each LED requires 24 bits of data
 //MSB: R7, R6, R5..., G7, G6..., B7, B6... B0
 //Once the 24 bits have been delivered, the IC immediately relays these bits to its neighbor
 //Pulling the clock low for 500us or more causes the IC to post the data.

 for(int LED_number = 0 ; LED_number < STRIP_LENGTH ; LED_number++) {
   long this_led_color = strip_colors[LED_number]; //24 bits of color data

   for(byte color_bit = 23 ; color_bit != 255 ; color_bit--) {
     //Feed color bit 23 first (red data MSB)
     
     digitalWrite(CKI, LOW); //Only change data when clock is low
     
     long mask = 1L << color_bit;
     //The 1'L' forces the 1 to start as a 32 bit number, otherwise it defaults to 16-bit.
     
     if(this_led_color & mask)
       digitalWrite(SDI, HIGH);
     else
       digitalWrite(SDI, LOW);
 
     digitalWrite(CKI, HIGH); //Data is latched when clock goes high
   }
 }

 //Pull clock low to put strip into reset/post mode
 digitalWrite(CKI, LOW);
 delayMicroseconds(500); //Wait for 500us to go into reset
}


Specifically I don't understand what "  for(byte color_bit = 23 ; color_bit != 255 ; color_bit--) {
     //Feed color bit 23 first (red data MSB)" or  "  long mask = 1L << color_bit; "

is doing.

I borrowed this code from the Sparkfun website.

Thank You.

fungus


I am a trial and error kind of guy and so if I had just the basics I can work on it from there.


Programming isn't really a trial and error process. You need to study.


Specifically I don't understand what "  for(byte color_bit = 23 ; color_bit != 255 ; color_bit--) {
      //Feed color bit 23 first (red data MSB)" or  "  long mask = 1L << color_bit; "


That's a particularly horrible piece of code that the writer should be ashamed of.

It relies on the fact that with byte-sized data, when you subtract 1 from 0 you get 255

(and if your next question is "why?" then it means you need to go back and read the first part of my answer again because you don't know what a 'byte' is).

Result: "color_bit" will start at 23 and go downwards to 0.

Advanced Arduino

Kerby

I beg to differ, programming is trial and error. I am a hobbyist and hobbyist like to play around and explore what the device can and can not do.

And if you can show me where there is a comprehensive study guide to be used for the Arduino I would be happy to purchase it. I have read the basics, I understand industrial PLC's but am new to Arduino.

I understand what a byte is I just can't make sense to why he would have done it that way. You answered that question in that it is a poor piece of code, I guess I would have to quiz the writer to find out why.

Finally my initial question was simply to find a basic piece of code to control the LED's If you did not have the answer you could have simply said so instead of trolling the forums looking for someone to make fun of.



fungus


Finally my initial question was simply to find a basic piece of code to control the LED's If you did not have the answer you could have simply said so instead of trolling the forums looking for someone to make fun of.


It doesn't get much more basic than the one you posted.
Advanced Arduino

Kerby

#4
Dec 17, 2013, 07:30 pm Last Edit: Dec 17, 2013, 08:00 pm by Kerby Reason: 1
Thank You


Kerby

I was unable to use the code I posted to get what I wanted. I did a lot more searching on the web and after about 3 hours found this website page.
http://www.freetronics.com/products/rgb-led-module#.UrEMifRDvSE

Downloaded all the files, and started with the BLINKRGB code in the example file and had my project near completion a couple of hours later.

I am using a 4 wire WS2801  1 meter LED strip. The RED and BLUE are swapped and I am not sure how to fix that except for telling it BLUE when I want RED. meh works for now.

Go Up