tags:

views:

64

answers:

2

This relates to both physical programming as well as Ruby running on a web server. I have an array of RGB leds, which is 5x5 so a total of 25 leds. They are numbered and individually addressable as such:

 1   2   3   4   5
 6   7   8   9  10
11  12  13  14  15
16  17  18  19  20
21  22  23  24  25

Here is a photo:

led matrix

As for the hardware (which really isn't important, because it works fine), the system consists of 25 BlinkM's, an Arduino, and some various cabling and connectors.

The led's are sent commands via serial with a command as such:

@sp.write ["\x01", led, "\x04\x00", "c", color]

Which writes the byte array out to serial using ruby's Serialport gem, the variables "led" and "color" are substituted with the hex of each, so for example if I wanted to make led number 8 turn red, my output would read:

@sp.write ["\x01","\x08", "\x04\x00", "c", "\xff\x00\x00"]

So far all of this works wonders, and I'm really happy with what I have, now my question relates pretty much to general mathematics and simple programming, but somehow the implementation goes over my head.

Here is a sample of such animation. Mostly I'm interesting in how one could animate patterns using ruby here. I recall certain "processing" animation scripts, just looping over a function using the array as an object and affecting the elements of the array creating interesting animations just due to the mathematics of the output.

Does anyone have any idea on how I could get started with something like that? I'm currently able to affect the LED's one at a time with my script, and I can string them together with sleep x after each command and manually build animations, but how could I make one run indefinitely with some sort of procedural animation?


EDIT

I really didn't describe the bytecode array in its entirety, here are what each part does:

@sp.write ["\x01", led, "\x04\x00", "c", color]
             ^      ^      ^   ^     ^    ^
             a      b      c   d     e    f

a. start byte (not important, tells serial that it is the start of a command)
b. hex for LED address, ex. `\x07` is led 7
c. length of command (starting at "e")
d. bytes to be read (always 0 in our case)
e. the "fade to color" command
f. the color we want to fade to in rrggbb hex format.
+1  A: 

If you just want to make animations, you should try to abstract away the hardware so it can be represented by some data structure that is easy to work with. I'm not familiar with Ruby so I don't know the best way to go about it. If you were making something like that table, which is just a grid, I would try to map the LEDs to a 2D array.

I would then create an infinite loop. This loop would contain another set of loops that iterates through each pixel in that array and writes the color in each element out to the corresponding hardware. Once it writes out all the pixels, it could then sleep for a few ms, call some function that steps your animation when it wakes and repeat the loop again.

Once you do this then all you'll have to manipulate is that data structure. Does that make any sense?

So something like this:

function stepAnimation(){
    //modify 2d array for each step of the animation here
}

//I'm assuming you have a function that gets
//Looped forever. In Wiring you do, not sure 
//about working with Arduino using Ruby, if not
//just add while(1) in there.. 
function mainLoop(){ 
    for(var y = 0; y < 5; y++){
     for(var x = 0; x < 5; x++){
         sp.write(2darray[x][y]) //write color from array to hardware
     }
    }
    sleep(60);
    stepAnimation();
}
Ronald
Yah the led's are mapped to a 2d array, as it's just a 5 x 5 array, as they are numbered 1-25. I understand your solution, just having a tough time figuring how I'll implement it.
Joseph Silvashy
Which part are you stuck on?
Ronald
Well, it's super easy for me to write to an array, it's more about how can I create interesting patterns over the array, for some reason I recall folks using sin and cos to make really simple patterns in there arrays by just comparing numbers etc. Thanks for your sample so far that helps, mostly I'm just trying to picture it in ruby.
Joseph Silvashy
Like Lissajous curves (http://en.wikipedia.org/wiki/Lissajous_curve)?
Ronald
Yah that is pretty cool! Things like that, mostly math oriented procedural animation? otherwise I guess I could script a few. I suppose the big issue is I need to figure out how to loop something indefinitely with ruby. Until the Arduino receives another command to stop the animation or start another one.
Joseph Silvashy
+1  A: 

It should be easy to map your leds to a 2d array

@led = []
led = 1
5.times do |y|
  5.times do |x|
    @led[x] ||= []
    @led[x][y] = led
    led +=1
  end
end

I'd probably make an LED class that encapsulates the ability to write out colors, so instead of this:

@led[x][y] = led

it becomes

@led[x][y] = Led.new(:id => led)

And then write a method so you can easily do something like this:

@led[1][5].color(255,255,255)

or whatever.

sax