Controling the 36mm Square RGB LED Pixels - WS2801

EL Wire/Tape/Panels, LEDs, pixels and strips, LCDs and TFTs, etc products from Adafruit

Moderators: adafruit_support_bill, adafruit

Controling the 36mm Square RGB LED Pixels - WS2801

Postby bluegroup » Sun Jun 03, 2012 8:53 am

Hi All,

I'm creating a 6x6 matrix (total36) using the 36mm Square 12V Digital RGB LED Pixels.
I have successfully run the strandtest.pde and it works like a dream.

However I'm stuck on how to control individual squares. For example, lets say I would like an orange "X" and the rest to be blue in the 6x6 matrix.
How would i achieve this or is there a mac controller to do this with? I really would appreciate any help as I'm more of a hardware guy!
Attachments
6x6matrix.gif
6x6matrix.gif (8.46 KiB) Viewed 1404 times
bluegroup
 
Posts: 8
Joined: Sat May 19, 2012 7:36 pm

Re: Controling the 36mm Square RGB LED Pixels - WS2801

Postby adafruit_support_bill » Sun Jun 03, 2012 9:15 am

Start by numbering all the pixels in your diagram - starting with #0 at the arrow. You can control the color of each pixel with calls to "strip.setPixelColor(i, c)", where 'i' is the pixel number and 'c' is the color. Colors can be set using the "Color(r, g, b)" function where r = red, g = green & b = blue. (You will probably have to experiment a bit to find the right mix of R, G & B to get the colors you want.

For example - to set pixel 0 to orange (I'm guessing at the color mix a little)
Code: Select all
strip.SetPixelColor(0, Color(255, 128, 0));

And to set pixel #1 to blue:
Code: Select all
strip.SetPixelColor(0, Color(0, 0, 255));


Once you have all the pixels colors set, call "strip.show()" to display them on the strip.
User avatar
adafruit_support_bill
 
Posts: 16055
Joined: Sat Feb 07, 2009 9:11 am

Re: Controling the 36mm Square RGB LED Pixels - WS2801

Postby mtbf0 » Sun Jun 03, 2012 3:28 pm

if you'd like to address your pixels as positions on a cartesian sort of grid, it might be worthwhile to rotate the whole thing so the first pixel is at the upper left with the second pixel to its right and the twelfth pixel below the first, then you could do a calculation along the lines of this...

Code: Select all
  strip.SetPixelColor ((y * 6) + ((y & 1) ? (5 - x) : x), Color(r, g, b));


where y is an int indicating the pixel's row and x is an int indicating it's column. keep in mind that the first row is row 0 and the last row is row 5. same goes for the columns. r, g, and b are ints that indicate the red, green and blue components of the color you want.
"i want to lead a dissipate existence, play scratchy records and enjoy my decline" - iggy pop, i need more
User avatar
mtbf0
 
Posts: 1642
Joined: Fri Nov 09, 2007 11:59 pm
Location: oakland ca

Re: Controling the 36mm Square RGB LED Pixels - WS2801

Postby bluegroup » Tue Jun 05, 2012 8:33 am

Thanks for the help guys, I managed to get an "x" using the following bit of code
Code: Select all
void MAKEx()
{
int r = 100;
int g = 100;
int b = 100;
strip.setPixelColor(0,Color(g, r, b));//grd
strip.setPixelColor(5,Color(g, r, b));//grd
strip.setPixelColor(7,Color(g, r, b));//grd
strip.setPixelColor(10,Color(g, r, b));//grd
strip.setPixelColor(14,Color(g, r, b));//grd
strip.setPixelColor(15,Color(g, r, b));//grd
strip.setPixelColor(20,Color(g, r, b));//grd
strip.setPixelColor(21,Color(g, r, b));//grd
strip.setPixelColor(25,Color(g, r, b));//grd
strip.setPixelColor(28,Color(g, r, b));//grd
strip.setPixelColor(30,Color(g, r, b));//grd
strip.setPixelColor(35,Color(g, r, b));//grd
strip.show();
}

Is there a way to make the code better?
I was also wondering how I could fade in and out the "x".

Any advice would be appreciated.
bluegroup
 
Posts: 8
Joined: Sat May 19, 2012 7:36 pm

Re: Controling the 36mm Square RGB LED Pixels - WS2801

Postby adafruit_support_rick » Tue Jun 05, 2012 8:56 am

There are a million ways to make the code better. It all depends on what you ultimately want to do with this. Can you give us some idea?
User avatar
adafruit_support_rick
 
Posts: 2905
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY

Re: Controling the 36mm Square RGB LED Pixels - WS2801

Postby bluegroup » Tue Jun 05, 2012 9:45 am

I should have said
Is it possible to reduce the amount of code so it doesn't be come too labor intensive.
bluegroup
 
Posts: 8
Joined: Sat May 19, 2012 7:36 pm

Re: Controling the 36mm Square RGB LED Pixels - WS2801

Postby adafruit_support_rick » Tue Jun 05, 2012 2:43 pm

"Better" and "less labor-intensive" are the same thing. The short answer is that you want to think about patterns.

If you are only going to have a few fixed patterns (for example, if your 'X' is part of a font you're planning to construct), then you want to think about using lookup tables for colors and intensities. You can write a simple loop to pull the pattern's colors and intensities out of one or more tables and write it to the lights. You could have a simple algorithm to do fades and color changes based on the fixed font patterns.

If you want to do some sort of random-looking psychedelic light show, then you want to think about algorithms to generate patterns, mix colors, cycle through different patterns etc.

My rule of thumb for stuff like this is that I want my code to be as simple and stupid as possible. I want to put the smarts into my data structures. Sometimes that takes a little more thought and work up front, but it pays off later on.
User avatar
adafruit_support_rick
 
Posts: 2905
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY

Re: Controling the 36mm Square RGB LED Pixels - WS2801

Postby bluegroup » Tue Jun 12, 2012 2:19 am

I would suggest the two below, unfortunately they don't work with the WS2801.
I'm all but giving up on the WS2801 as there's not a lot of support or controllers for them.

Try:
http://pixelinvaders.ch/
http://www.idolstarastronomer.com/char-pixel_paint
bluegroup
 
Posts: 8
Joined: Sat May 19, 2012 7:36 pm

Re: Controling the 36mm Square RGB LED Pixels - WS2801

Postby mtbf0 » Tue Jun 12, 2012 4:12 pm

bluegroup, try this. it compiles, but i don't have a ws2801 strip to test it on. should fade your orange x on a blue background in, hold for a second, then fade the orange to blue. pretty rudimentary.

Code: Select all
#include "SPI.h"
#include "Adafruit_WS2801.h"

#define DATA_LENGTH 36
uint8_t datums[] = {1,0,0,0,0,1,0,1,0,0,1,0,0,0,1,1,0,0,0,1,0,0,1,0,1,0,0,0,0,1};
uint32_t fg, bg;
uint8_t fg_color[] = {0,0,0};
uint8_t bg_color[] = {0,0,0};

Adafruit_WS2801 strip = Adafruit_WS2801(36);

uint32_t Color(uint8_t r, uint8_t g, uint8_t b) {
  uint32_t c;
  c = r;
  c <<= 8;
  c |= g;
  c <<= 8;
  c |= b;
  return c;
}

void display () {
  bg = Color (bg_color[0], bg_color[1], bg_color[2]);
  fg = Color (fg_color[0], fg_color[1], fg_color[2]);
  for (int i = 0; i < DATA_LENGTH; i++) {
    if (datums[i]) strip.setPixelColor(i, fg);
    else strip.setPixelColor(i, bg);
  }
  strip.show ();
}

void fade (int steps, int d) {
  float start[] = {0.0, 0.0, 0.0};
  float end[] = {0.0, 0.0, 0.0};
  float interval[] = {0.0, 0.0, 0.0};
  int i, j;
  for (i = 0; i < 3; i++) {
    start [i] = (float) fg_color[i];
    interval[i] = ((float) bg_color[i] - start[i]) / (float) steps;
  }
  bg = Color (bg_color[0], bg_color[1], bg_color[2]);
  for (j = 1; j < steps; j++) {
    fg = Color ((uint8_t)start[0], (uint8_t)start[1], (uint8_t)start[2]);
    for (int i = 0; i < DATA_LENGTH; i++) {
      if (datums[i]) strip.setPixelColor(i, fg);
    }
    strip.show ();
    start[0] += interval[0];
    start[1] += interval[1];
    start[2] += interval[2];
    delay (d);
  }
  for (int i = 0; i < DATA_LENGTH; i++) {
    if (datums[i]) strip.setPixelColor(i, bg);
  }
  strip.show ();
}

void setup() {
  strip.begin();
  strip.show();
  for (i = 0; i < 128; i++) {
    bg_color[2] += 2;
    fg_color[0] += 2;
    fg_color[1] += 1;
    display ();
    delay (8);
  }
}

void loop () {
  display ();
  delay (1000);
  fade (256, 4);
  delay (1000);
}
"i want to lead a dissipate existence, play scratchy records and enjoy my decline" - iggy pop, i need more
User avatar
mtbf0
 
Posts: 1642
Joined: Fri Nov 09, 2007 11:59 pm
Location: oakland ca

Re: Controling the 36mm Square RGB LED Pixels - WS2801

Postby bluegroup » Tue Jun 12, 2012 7:31 pm

Thanks mtbf0

I really appreciate the time and effort you have put.
I'll definitely let you know how I go, this sure has given me hope!
bluegroup
 
Posts: 8
Joined: Sat May 19, 2012 7:36 pm

Re: Controling the 36mm Square RGB LED Pixels - WS2801

Postby mtbf0 » Tue Jun 12, 2012 8:26 pm

failed to notice that your strip is grb, so you'll want to change lines like

Code: Select all
  bg = Color (bg_color[0], bg_color[1], bg_color[2]);
  fg = Color (fg_color[0], fg_color[1], fg_color[2]);


to

Code: Select all
  bg = Color (bg_color[1], bg_color[0], bg_color[2]);
  fg = Color (fg_color[1], fg_color[0], fg_color[2]);


fortunately it comes to changing about 8 characters and they're all ones and zeros. without the changes your orange will be kind of gold.
"i want to lead a dissipate existence, play scratchy records and enjoy my decline" - iggy pop, i need more
User avatar
mtbf0
 
Posts: 1642
Joined: Fri Nov 09, 2007 11:59 pm
Location: oakland ca

Re: Controling the 36mm Square RGB LED Pixels - WS2801

Postby bluegroup » Wed Jun 13, 2012 8:23 am

Thanks mtbf0,

I had to make a few mods to the code but I did get it working!

Code: Select all
#include "SPI.h"

#include "Adafruit_WS2801.h"

int dataPin  = 2;   
int clockPin = 3;   

Adafruit_WS2801 strip = Adafruit_WS2801(36, dataPin, clockPin);

#define DATA_LENGTH 36

uint8_t datums[] = {1,0,0,0,0,1,0,1,0,0,1,0,0,0,1,1,0,0,0,0,1,1,0,0,0,1,0,0,1,0,1,0,0,0,0,1};

uint32_t fg, bg;

uint8_t fg_color[] = {0,0,0};

uint8_t bg_color[] = {0,0,0};


uint32_t Color(uint8_t r, uint8_t g, uint8_t b) {
  uint32_t c;
  c = r;
  c <<= 8;
  c |= g;
  c <<= 8;
  c |= b;
  return c;
}


void display () {

  bg = Color (bg_color[1], bg_color[0], bg_color[2]);
  fg = Color (fg_color[1], fg_color[0], fg_color[2]);

  for (int i = 0; i < DATA_LENGTH; i++) {

    if (datums[i]) strip.setPixelColor(i, fg);

    else strip.setPixelColor(i, bg);

  }

  strip.show ();

}


void fade (int steps, int d) {

  float start[] = {0.0, 0.0, 0.0};

  float end[] = {0.0, 0.0, 0.0};

  float interval[] = {0.0, 0.0, 0.0};

  int i, j;

  for (i = 0; i < 3; i++) {

    start [i] = (float) fg_color[i];

    interval[i] = ((float) bg_color[i] - start[i]) / (float) steps;

  }

  bg = Color (bg_color[0], bg_color[1], bg_color[2]);

  for (j = 1; j < steps; j++) {

    fg = Color ((uint8_t)start[0], (uint8_t)start[1], (uint8_t)start[2]);

    for (int i = 0; i < DATA_LENGTH; i++) {

      if (datums[i]) strip.setPixelColor(i, fg);

    }

    strip.show ();

    start[0] += interval[0];

    start[1] += interval[1];

    start[2] += interval[2];

    delay (d);

  }

  for (int i = 0; i < DATA_LENGTH; i++) {

    if (datums[i]) strip.setPixelColor(i, bg);

  }

  strip.show ();

}


void setup() {

  strip.begin();

  strip.show();

  for (int i = 0; i < 255; i++) {

   bg_color[2] += 2;

    fg_color[0] += 2;

    fg_color[1] += 1;

    display ();

    delay (8);

  }

}


void loop () {

  display ();

  delay (1000);

  fade (256, 4);

  delay (1000);
   

}
bluegroup
 
Posts: 8
Joined: Sat May 19, 2012 7:36 pm

Re: Controling the 36mm Square RGB LED Pixels - WS2801

Postby mtbf0 » Wed Jun 13, 2012 11:33 pm

bluegroup wrote:I had to make a few mods to the code but I did get it working!


excellent.
"i want to lead a dissipate existence, play scratchy records and enjoy my decline" - iggy pop, i need more
User avatar
mtbf0
 
Posts: 1642
Joined: Fri Nov 09, 2007 11:59 pm
Location: oakland ca


Return to Glowy things (LCD, LED, TFT, EL) purchased at Adafruit

Who is online

Users browsing this forum: Google [Bot] and 3 guests

Stuff to buy from the Adafruit store and links to product documentation!


New Products [103]

Raspberry Pi[80]
 
FLORA[23]
 
Bunnie Studios[9]
 
FPGA[1]
 
mbed[11]
Arduino[60]
 
NETduino[14]
 
BeagleBone[24]
 
Android[6]
 
XBee[10]
More Dev Boards[30]


 
BoArduino[8]
 
SpokePOV[4]
 
TV-B-Gone[4]
 
MiniPOV[3]
 
SIM reader[3]
 
Microtouch[5]
 
Clocks & Watches[18]
 
Drawdio[4]
 
Brain Machine[1]
 
Game of Life[2]
 
MintyBoost[2]
More DIY Kits[16]


 
MaKey MaKey[3]
 
Tweet-a-Watt[5]
 
Young Engineers[33]
 
Discover Electronics[2]
 
Snap Circuits[4]
 
littleBits[3]
 
Project packs[8]


 
Breakout Boards[33]
LCDs & Displays[48]
Components & Parts[69]
Batteries & Power[49]
EL Wire/Tape/Panel[52]
LEDs[109]
 
Wireless[14]
Cables[61]
 
Lasers[6]
Sensors/Parts[145]
 
Enclosures/Cases[11]
 
Solar[11]
 
RFID / NFC[13]
Prototyping[70]
 
iDevices[13]
Tools[71]
 
Wearables[39]
 
CNC[37]
 
Robotics[29]
 
3D printing[1]
 
Materials[24]


 
Stickers[41]
 
Skill badges[55]
 
Books[25]
 
Circuit Playground[7]
 
Gift Certificates[4]