New project with shiftbrites and controllers - help needed

Hi all,

I’m new to this forum and new to microcontrollers, I’ve never tried to programme one or used one for a personal project so I need some beginner advice.

I plan on making a 4 x 4 grid for a dance floor panel (I plan to use 16 panels in total). I want to build a prototype panel first and work from there.
I’ve looked at the BlinkM LEDs but they are a bit expensive so would like to get my head around a ShiftBrite set up first.

I would need to control the 16 ShiftBrites from one cheap microcontroller in a 1 metre x 1 metre wooden panel with difussed perspex top. The construction is not a problem but I need to know what is a good and easy to use microcontroller that can control 16 ShiftBrites and operate on its own once programmed (probably random flashing and basic patterns).

If this works as a stand alone 16 LED panel I would then just replicate the design with 1 microcontroller and 16 ShiftBrites in each 1 metre by 1 metre panel.

So what controller would you guys recommend (easy to programme and cheap since I will be using 16 of them plus 256 ShiftBrites), and how would I wire it all together in the panel.

I been searching the net for the last few days but can’t find a definitive answer and I’m still a bit confused as to how they are controlled when daisy chained together.

Thanks for any help.

If you have multiple microcontrollers in the system, the lights will not automatically be able to blink exactly in sync with eachother. Typical microcontrollers run at a speed that has a .1% (or much more) margin of error, which is noticeable after they’ve been running all night. So if that’s a problem for you then you might have to add a method to let the microcontrollers sync up with eachother, or you could try just controlling ALL of your shiftbrites from one microcontroller.

I think you should try using the Micro Maestro Servo controller. If you buy 16 of them, it will only be $17.96 per unit. The Maestro is easy to get started with because there is only one software package you need to program it. The Example Scripts section of the Maestro User’s Guide even has an example 24-line script that allows the Maestro to autonomously send commands to a shiftbrite. Many people are already using the Maestro for this purpose.

You could save a few bucks by using a Baby Orangutan B-48 instead of a Micro Maestro, but the learning curve would be much steeper so I don’t think it will be worth it for you if you’ve never programmed microcontrollers before.

There is no definitive answer because virtually any programmable microcontroller will work fine with the shiftbrites; all it needs is 3 digital outputs.

What’s confusing to you about the daisy chaining? If you have a specific question I can probably answer it.

–David

Hello,

I just wanted to mention that you might be disappointed with the Maestro’s performance for controlling a lot of ShiftBrites. I have not timed it, but it is possible that it will take as much as half a second to update 16 ShiftBrites to different colors. And the programming language is very limited, so doing anything fancy, like the synchronizing that David was talking about, would be really tough.

I still think that the Maestro is a good way to start, since it is really easy, and it might be totally fine for what you are imagining. But you might want to upgrade to something better after your first prototype is working well!

-Paul

Thanks for the info guys.

I’ve just read the maestro user guide and it’s a lot clearer now as to what happens data wise. A couple of further questions though.

The manual says

Are these servo channels the signal pins on the maestro?

I think I understand how the scripts work for servos etc but in the ShiftBrite example where would the address for each LED be set? Say LED 1 green, LED 2 red, LED 3 blue … Is it the 0 send_bit? Would this change to 1 send_bit for the next in the sequence? (This would answer my confusion about the daisy chaining).

# Subroutine for setting the RGB value of a ShiftBrite/ShiftBar.
# example usage: 1023 511 255 rgb
sub rgb
0 send_bit # this bit does not matter
0 send_bit # the "address" bit - 0 means a color command
swap rot rot
send_10_bit_value
send_10_bit_value
send_10_bit_value
0 1 8000 1 servo servo # toggle the latch pin
return
# sends a numerical value as a sequence of 10 bits
sub send_10_bit_value
512
begin
dup
while
over over bitwise_and send_bit
1 shift_right
repeat
drop drop
return
# sends a single bit
sub send_bit
if 8000 else 0 endif
2 servo # set DATA to 0 or 1
0 0 8000 0 servo servo # toggle CLOCK
return

Paul - Is the slow update due to the serial data being sent to every ShiftBrite each time? I would be using 16 LEDs per controller.

I noticed in the manual that the Micro doesn’t handle serial Chain inputs but if sync issues were a problem I could use the RST pin when needed to reset all the controllers. Or use Mini Maestro 12s

Thanks again.

Anyone?

I’m about to order a micro maestro, 16 shiftbrites and cables, can anyone answer my questions above before I do just so I can get ready for programming and connecting. If someone can write a small script for me so I can understand whats going on then I could learn to write my own. Something simple like step through each shiftbrite 1 at a time white on full brightness I could then see how to address them and set their colour and brightness.

Cheers

Yes.

There is no place to explictly set an “address”. The send_bit function shifts one bit out to the shiftbrites; this bit is either zero or 1 (hence the “1 send_bit” and “0 send_bit”). The number before send_bit is not an address, it is data.

What you need to do is shift out all the color bits for all the shiftbrites in your chain, starting with the bits needed by the last one in the chain and ending with the bits for the first one in your chain (the one directly connected to your microcontroller). If you have 16 shift brites, that means you need to shift out 16*32 = 512 bits. Then you toggle that latch pin to tell the shiftbrites to use the bits you shifted out and update their color.

Here, I’ve modified the example script to work with 16 shiftbrites (but I have not tested it!):

begin
  1023 0     0     rgb   # Send the color for the last shiftbrite in the chain
  0    1023 0     rgb
  0    0     1023    rgb
  1023 1023     0     rgb
  0    1023 1023     rgb
  1023    0     1023    rgb
  1023 0     0     rgb
  0    1023 0     rgb
  0    0     1023    rgb
  1023 1023     0     rgb
  0    1023 1023     rgb
  1023    0     1023    rgb
  1023 0     0     rgb
  0    1023 0     rgb
  0    0     1023    rgb
  1023 1023     0     rgb    # Send the color for the first shiftbrite
  latch
repeat

# shifts out a single bit
sub send_bit
  if 8000 else 0 endif
  2 servo # set DATA to 0 or 1
  0 0 8000 0 servo servo # toggle CLOCK
  return

# sends a numerical value as a sequence of 10 bits
sub send_10_bit_value
  512
  begin
  dup
  while
  over over bitwise_and send_bit
  1 shift_right
  repeat
  drop drop
  return

sub latch
  0 1 8000 1 servo servo # toggle the latch pin
  return

# Subroutine for shifting out an RGB value for ShiftBrite/ShiftBar.
# example usage: 1023 511 255 rgb
sub rgb
  0 send_bit # this bit does not matter
  0 send_bit # the "address" bit - 0 means a color command
  swap rot rot
  send_10_bit_value
  send_10_bit_value
  send_10_bit_value
  # do NOT toggle the latch pin here, wait until all bits have been sent
  return

Yes. You have to send 512 bits each time you want to update the colors of the shiftbrites IF you want independent control of the colors. That’s probably why Paul said it could be slow.

You can get a faster update rate if you give up independent control of the colors. For example, for every color update, you could just shift out 32 bits and then latch. When you latch, each shiftbrite will assume the color that its neighbor was previously, so it will look like the colors are moving down your chain.

The code for that would be like this:

begin 1023 0 0 rgb latch 0 1023 0 rgb latch 0 0 1023 rgb latch repeat

This method should allow you to update the colors almost 16 times faster.

This sounds like a cool project, so please post some videos here when you have it working!

–David

Hi David,

Thanks very much for the reply, this will give me a good starting point for experimenting and learning the code myself.

I’ve ordered 2 micro maestros and 35 shiftbrites to make a couple of prototype panels (plus a few spares). Once I get one working I will keep building more. I have a few design ideas to make it modular and infinately expandable (only constrained by power supplies). Each panel will act independantly but should still look cool.

I will try to keep a diary of the build and stick it up on the Instructables (or similar) website.

Cheers

I have the frame built and the shiftbrites and micro maestro fitted. I have a rough script and I’m learning the code slowly.

I’ve posted a quick video here http://www.youtube.com/watch?v=nHnq8ERE9kE

The script I’ve used roughly fades between colours from one led to the next and follows the hard wire route then alternates white/off then all off and repeats.

I’m trying to cut down the memory usage by using sub routines which works well for repeat commands but I’m getting a stack overflow due to the stack not clearing. How can I set the stack to clear at certain point in the script?

Here’s what I have:

begin
colour
200 delay
on
200 delay
repeat

sub on
10
begin
dup
while
1023 1023 1023 rgb
d2
0 0 0 rgb
d2
1023 1023 1023 rgb
d2
0 0 0 rgb
d2
1023 1023 1023 rgb
d2
0 0 0 rgb
d2
1023 1023 1023 rgb
d2
0 0 0 rgb
d2
1023 1023 1023 rgb
d2
0 0 0 rgb
d2
1023 1023 1023 rgb
d2
0 0 0 rgb
d2
1023 1023 1023 rgb
d2
0 0 0 rgb
d2
1023 1023 1023 rgb
d2
0 0 0 rgb
d2
1023 1023 1023 rgb
d2
0 0 0 rgb
d2
1023 1023 1023 rgb
d2
0 0 0 rgb
d2
1023 1023 1023 rgb
d2
0 0 0 rgb
d2
1023 1023 1023 rgb
d2
0 0 0 rgb
d2
1023 1023 1023 rgb
d2
0 0 0 rgb
d2
1023 1023 1023 rgb
d2
0 0 0 rgb
d2
1023 1023 1023 rgb
d2
0 0 0 rgb
d2
1023 1023 1023 rgb
d2
c
1 minus
repeat
return

sub c
16
begin
dup
while
0 0 0 rgb
1 minus
repeat
return

sub Colour
10
begin
dup
while
1023 0 0 rgb
d
900 0 0 rgb
d
700 0 0 rgb
d
300 0 0 rgb
d
0 0 0 rgb
d
0 300 0 rgb
d
0 700 0 rgb
d
0 900 0 rgb
d
0 1023 0  rgb
d
0 900 0 rgb
d
0 700 0 rgb
d
0 300 0 rgb
d
0 0 0 rgb
d
0 0 300 rgb
d
0 0 700 rgb
d
0 0 900 rgb
d
0 0 1023 rgb
d
300 300 1023 rgb
d
300 700 1023 rgb
d
700 1023 1023 rgb
d
1023 1023 1023 rgb
d
1 minus
repeat
return


# Subroutine for setting the RGB value of a ShiftBrite/ShiftBar.  
# example usage: 1023 511 255 rgb  
sub rgb  
0 send_bit # this bit does not matter  
0 send_bit # the "address" bit - 0 means a color command  
swap rot rot
send_10_bit_value   
send_10_bit_value   
send_10_bit_value  
0 1 8000 1 servo servo # toggle the latch pin  
return
  
# sends a numerical value as a sequence of 10 bits  
sub send_10_bit_value  
512  
begin  
dup  
while  
over over bitwise_and send_bit  
1 shift_right  
repeat  
drop drop  
return  

sub d
50 delay
return

sub d2
150 delay
return
   
# sends a single bit  
sub send_bit  
if 8000 else 0 endif  
2 servo                # set DATA to 0 or 1  
0 0 8000 0 servo servo # toggle CLOCK  
return

It runs the sub routine “Colour” first, then “On” which flashes them on and off white. At the end of this it runs sub “c” (for clear), it should set all 16 leds to off before restarting the script at “colour”.

It all works apart from the stack filling up after a few cycles so I need it to clear the stack after the top begin/repeat loop. What command does this if any?

And does anyone have any cool ideas or scripts I can use?

Also the latch command in your post David doesn’t work (not recognised). Is there another way to send all bits to the shiftbrites then have them activate at the same time?

Edit: I now realise that “latch” is another subroutine, I tried it to set all leds white but there is still a delay from first to last led in the chain so it still looks like a trail. Oh well just a minor limitation.

Thanks

Another question. Rather than having:

0 0 0 rgb
d2
1023 1023 1023 rgb
d2

etc

repeated numerous times how do I have it automatically repeat say 50 times? I’ve tried things like

50 times 1023 1023 1023 rgb
repeat

but none of the varitions work.

Found it tucked away in the manual. The subroutines should end

drop return

to clear the value used from the stack.

Now to compress and get more flashing sequences.

Feel free to post subroutines and I’ll test them out and stick them on youtube. :smiley:

Here’s my latest script, I’ve figured out some of the repeat commands so it’ s a bit smaller but I still need to tidy it up.

begin
pk
bl3
colour
on
z
bl2
gd
grn
bl
c
repeat

sub bl3
9 begin
dup while
b
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
1 minus repeat
drop return

sub bl2
20 begin
dup while
b
o
o
o
o
1 minus
repeat
drop return

sub p
1000 200 200 rgb
d2
return

sub g
1023 900 0 rgb
d2
return

sub gr
0 1023 0 rgb
d2
return

sub b
0 0 1023 rgb
d2
return

sub bl
10 begin
dup while
b
b
o
o
o
b
o
1 minus
repeat
drop return

sub grn
10 begin
dup while
gr
gr
gr
gr
o
o
o
gr
o
1 minus
repeat
drop return

sub gd
10 begin
dup while
g
g
g
g
o
1 minus
repeat
drop return

sub pk
10 begin
dup while
p
p
p
p
o
1 minus
repeat
drop return

sub z
10 begin
dup while
w
w
w
w
w
o
1 minus repeat
drop return

sub w
1023 1023 1023 rgb
d2
return

sub o
0 0 0 rgb
d2
return

sub on
50
begin
dup
while
w
o
1 minus
repeat
drop return

sub c
16
begin
dup
while
0 0 0 rgb
1 minus
repeat
drop return

sub Colour
10
begin
dup
while
1023 0 0 rgb
d
900 0 0 rgb
d
700 0 0 rgb
d
300 0 0 rgb
d
0 0 0 rgb
d
0 300 0 rgb
d
0 700 0 rgb
d
0 900 0 rgb
d
0 1023 0  rgb
d
0 900 0 rgb
d
0 700 0 rgb
d
0 300 0 rgb
d
0 0 0 rgb
d
0 0 300 rgb
d
0 0 700 rgb
d
0 0 900 rgb
d
0 0 1023 rgb
d
300 300 1023 rgb
d
300 700 1023 rgb
d
700 1023 1023 rgb
d
1023 1023 1023 rgb
d
1 minus
repeat
drop return


# Subroutine for setting the RGB value of a ShiftBrite/ShiftBar.  
# example usage: 1023 511 255 rgb  
sub rgb  
0 send_bit # this bit does not matter  
0 send_bit # the "address" bit - 0 means a color command  
swap rot rot
send_10_bit_value   
send_10_bit_value   
send_10_bit_value  
0 1 8000 1 servo servo # toggle the latch pin  
return
  
# sends a numerical value as a sequence of 10 bits  
sub send_10_bit_value  
512  
begin  
dup  
while  
over over bitwise_and send_bit  
1 shift_right  
repeat  
drop drop  
return  

sub d
50 delay
return

sub d2
50 delay
return
   
# sends a single bit  
sub send_bit  
if 8000 else 0 endif  
2 servo                # set DATA to 0 or 1  
0 0 8000 0 servo servo # toggle CLOCK  
return

As you can see I’ve used a lot of subroutines to save space and typing.

Is there a command that can generate random numbers so I can generate random colours?

Here’s another video:

http://www.youtube.com/watch?v=9GUBqZPvcRM

The video isn’t great, the blues are very bright in real life and the whites look blue on the video, it is also flashing faster than it looks and about 10 seconds in only 1 blue led is on at any one time (it looks like 2 or 3 on screen though).

That’s great that you got things working, and thanks for posting your code!

The Maestro doesn’t have a subroutine for generating random numbers, but you could try configuring some unused channels as inputs, reading their “position” (voltage), and using one or two of the least significant bits which are basically random. That would only give you about 6 bits of randomness at a time though, so it’s not so good.

If you had a noisy analog voltage signal of some sort, that could help you generate more bits of randomness. We sell several sensors that might do the trick: pololu.com/catalog/category/7 And here’s an idea: buy one of the microphones on that page so that your Maestros can sense sound, and your floor can respond to the crowd noise and/or the music!

By the way, having short subroutine names doesn’t save you any program space, since the script is compiled in to a binary bytecode. The source of the script is NOT stored on the Maestro so it doesn’t matter how big it is. I recommend that you add indentation, comments, and increase the length of your subroutine names so it will be easier for you to maintain this code in the future. In general, if you ever think that your change will save some space, you should check to make sure it actually does; the Maestro Control Center shows you exactly how many bytes your script takes, so you can check to see if that number changes when you change your code.

Have you resolved all the other questions you had? Let me know if you’re still wondering about something.

We would definitely be interested in linking to your build diary or any other website you make about the project, so keep us updated!

–David

I wasn’t sure about the text taking up space, I was mostly using short names to save typing rather than space but I will expand the lot and comment the lines to tidy it up. I reduced the code from about 700b to 450b by changing to subs (not the text though). I’ve even forgot parts of what the code do (sub routine names etc) because I haven’t looked at it for a few weeks. I will be doing some more work on it next week and repost the code.

One of my earlier questions about the latch command, it is defind in the RGB subroutine as:

0 1 8000 1 servo servo # toggle the latch pin

so everytime I use RGB it will send a latch after each colour. If I were to remove this part of RGB then use for example:

1023 1023 1023 RGB
500 500 500 RGB
100 100 100 RGB
0 1 8000 1 servo servo # toggle the latch pin  

would this send the bits to three leds then send the latch command rather than having the scroll effect I am getting? Would this speed up the refresh time of the LEDS?

I would like one of the sequences to follow a “all LEDS on, all off” pattern without the scroll effect (maybe light a slow strobe light).
So the code could be:

1023 1023 1023 RGB
1023 1023 1023 RGB
1023 1023 1023 RGB
1023 1023 1023 RGB
1023 1023 1023 RGB
1023 1023 1023 RGB
1023 1023 1023 RGB
1023 1023 1023 RGB
1023 1023 1023 RGB
1023 1023 1023 RGB
1023 1023 1023 RGB
1023 1023 1023 RGB
1023 1023 1023 RGB
1023 1023 1023 RGB
1023 1023 1023 RGB
1023 1023 1023 RGB
0 1 8000 1 servo servo # toggle the latch pin  
0 0 0 RGB
0 0 0 RGB
0 0 0 RGB
0 0 0 RGB
0 0 0 RGB
0 0 0 RGB
0 0 0 RGB
0 0 0 RGB
0 0 0 RGB
0 0 0 RGB
0 0 0 RGB
0 0 0 RGB
0 0 0 RGB
0 0 0 RGB
0 0 0 RGB
0 0 0 RGB
0 1 8000 1 servo servo # toggle the latch pin 

then repeat for a set amount of time with maybe a delay in between flashes depending on how fast the maestro can update the shiftbrites on a batch command. Is this more likely to update all 16 shiftbrites quicker than before?

The answer to all of your questions is yes. Removing the latch code from your RGB function will let you have individual control over the shiftbrites without the scrolling effect, and it should make your update times at least a little bit faster. This is what I originally recommended for you. But as I said before, you should make your own subroutine called “latch”: this lets you save some program space and you don’t have to copy that line of code all over the place.

Send more videos our way when you’re done!

–David

Thanks for clearing that up David, I am still learning this code and got a bit muddled with the latch command before. I’ll test it next week and post a video, hopefully showing the full update speed of the shiftbrites.

Hi all.

I have got back to this project eventually. It is a disco dance floor panel with micro maestro and 16 shiftbrites. I have rewritten the code for easier reading and added a few more sequences.

However I am struggling with the get_position command. I have attached a short wire to input 5 which picks up noise as voltage. When I put my hand near it the noise increases (and voltage input signal).

I want to use this analogue value as an intensity value for each of the colours of the shiftbrite to create a “random colour/brightness effect” when someone is near the panel.

Here is the code I have so far:

begin
strobe3
redstrobe
green-flash
blue-scroll
green-scroll
red-scroll
blue-flash
colour
bluestrobe
pinkflash
white
red-flash
greenredflash
on
random
    repeat

sub input
5 get_position
dup 500 less_than
if
0
else
max 1023
endif
drop
return

sub random
20 begin
dup while
input input input rgb
1 minus repeat
drop return

sub latch
0 1 8000 1 servo servo # toggle the latch pin
return

sub strobeon
16 begin
dup while
    1023 1023 1023 RGB2
1 minus repeat
drop return

sub strobeoff
16 begin
dup while
    0 0 0 RGB2
1 minus repeat
drop return

sub redstrobe
    50
    begin
    dup
    while
    red
    off
    1 minus
    repeat
    drop return


sub strobe3
5 begin
dup while
strobeon
latch
strobeoff
latch
1 minus repeat
drop return

    sub blue-scroll
    5 begin
    dup while
    blue
    off
    off
    off
    off
    off
    off
    off
    off
    off
    off
    off
    off
    off
    off
    off
    1 minus repeat
    drop return

sub green-scroll
    5 begin
    dup while
    green
    off
    off
    off
    off
    off
    off
    off
    off
    off
    off
    off
    off
    off
    off
    off
    1 minus repeat
    drop return

sub red-scroll
    5 begin
    dup while
    red
    off
    off
    off
    off
    off
    off
    off
    off
    off
    off
    off
    off
    off
    off
    off
    1 minus repeat
    drop return


    sub blue-flash
    20 begin
    dup while
    blue
    off
    off
    off
    off
    1 minus
    repeat
    drop return

sub red-flash
    20 begin
    dup while
    red
    off
    off
    off
    off
    1 minus
    repeat
    drop return

sub green-flash
    20 begin
    dup while
    green
    off
    off
    off
    off
    1 minus
    repeat
    drop return

    sub pink
    1000 200 200 rgb
    return

    sub green/red
    1023 900 0 rgb
    return

    sub green
    0 1023 0 rgb
    return

sub red
    1023 0 0 rgb
    return


    sub blue
    0 0 1023 rgb
    return

    sub greenredflash
    10 begin
    dup while
    green/red
    green/red
    green/red
    green/red
    off
    1 minus
    repeat
    drop return

    sub pinkflash
10 begin
dup while
pink
pink
pink
off
off
1 minus 
repeat
drop return

sub bluestrobe
    50
    begin
    dup
    while
    blue
    off
    1 minus
    repeat
    drop return

    10 begin
    dup while
    pink
    pink
    pink
    pink
    off
    1 minus
    repeat
    drop return

    sub white5
    10 begin
    dup while
    white
    white
    white
    white
    white
    off
    1 minus repeat
    drop return

    sub white
    1023 1023 1023 rgb
    return

    sub white2
    1023 1023 1023 rgb2
    delay1
    return

    sub off
    0 0 0 rgb
    return

    sub on
    50
    begin
    dup
    while
    white
    off
    1 minus
    repeat
    drop return

    sub clear
    16
    begin
    dup
    while
    0 0 0 rgb2
    1 minus
    repeat
    drop return

    sub Colour
    10
    begin
    dup
    while
    1023 0 0 rgb
    delay1
    900 0 0 rgb
    delay1
    700 0 0 rgb
    delay1
    300 0 0 rgb
    delay1
    0 0 0 rgb
    delay1
    0 300 0 rgb
    delay1
    0 700 0 rgb
    delay1
    0 900 0 rgb
    delay1
    0 1023 0  rgb
    delay1
    0 900 0 rgb
    delay1
    0 700 0 rgb
    delay1
    0 300 0 rgb
    delay1
    0 0 0 rgb
    delay1
    0 0 300 rgb
    delay1
    0 0 700 rgb
    delay1
    0 0 900 rgb
    delay1
    0 0 1023 rgb
    delay1
0 0 900 rgb
    delay1
0 0 700 rgb
    delay1
0 0 300 rgb
    delay1
    300 300 700 rgb
    delay1
    300 700 900 rgb
    delay1
    700 900 1023 rgb
    delay1
    900 1023 1023 rgb
    delay1
	1023 1023 1023 rgb
    delay1
1023 900 900 rgb
    delay1
1023 700 700 rgb
    delay1
1023 300 300 rgb
    delay1
1023 0 0 rgb
    delay1
    1 minus
    repeat
    drop return


    # Subroutine for setting the RGB value of a ShiftBrite/ShiftBar. 
    # example usage: 1023 511 255 rgb 
    sub rgb 
    0 send_bit # this bit does not matter 
    0 send_bit # the "address" bit - 0 means a color command 
    swap rot rot
    send_10_bit_value   
    send_10_bit_value   
    send_10_bit_value 
    0 1 8000 1 servo servo # toggle the latch pin 
    return

	# Subroutine for setting the RGB value of a ShiftBrite/ShiftBar. 
    # example usage: 1023 511 255 rgb 
    sub rgb2
    0 send_bit # this bit does not matter 
    0 send_bit # the "address" bit - 0 means a color command 
    swap rot rot
    send_10_bit_value   
    send_10_bit_value   
    send_10_bit_value 
    return
     
    # sends a numerical value as a sequence of 10 bits 
    sub send_10_bit_value 
    512 
    begin 
    dup 
    while 
    over over bitwise_and send_bit 
    1 shift_right 
    repeat 
    drop drop 
    return 

    sub delay1
    50 delay
    return
       
    # sends a single bit 
    sub send_bit 
    if 8000 else 0 endif 
    2 servo                # set DATA to 0 or 1 
    0 0 8000 0 servo servo # toggle CLOCK 
    return

The top part - “random” subroutine - uses the “input” subroutine:

sub input
5 get_position
dup 500 less_than
if
0
else
max 1023
endif
drop
return

I can’t figure out how to limit the value to a max of 1023. I have tried a few variations but keep getting a stack overflow when the noise is too high (it basically works otherwise).

How do I set the value to 0 if it below a threshold and set the max to 1023 if the value is greater than this?

Once I figure this out I can then calibrate the settings to only come on if someone is on the panel and have it as part of the disco light sequence.

Cheers

Greg

P.S. I have uploaded a video to youtube with this sequence but from my phone so the flashes blend into each other, I will post a link when I use another camera.

Hello, Greg.

First, make sure the value is on the top of the stack. Then you can call this subroutine:

sub limit
  0 max
  1023 min
  return

This coverts negative numbers to 0 and numbers greater than 1023 to 1023.

–David

Ok, thanks for that.

That should stop the stack overflow, I think my new code will look likr this:

sub input
5 get_position
dup 500 less_than
if
0
else
limit
endif
drop
return

sub limit
0 max
1023 min
return

(Are the min and max commands round the right way?) I will test this tonight and hopefully get a rudimentary proximity detector with increasing intensity as I get closer without the stack overflow.

No I don’t think that code will work. Take a look at this section:

if
0
else
limit
endif

If the “if” conditon is true (non-zero), you add a 0 to the stack, making the stack grow by one. Otherwise, you call “limit” which does not change the size of the stack. It’s unusual to have the two code-paths of the “if” statement have a different effect on the stack size and will probably cause a stack overflow. Also the “drop” statement at the end seems to make the “input” subroutine useless, because you are dropping the result before it can be used to do anything. Maybe this is what you wanted instead:

sub input
  5 get_position
  dup 500 less_than
  if
    drop 0
  else
    1023 min
  endif
  return

–David

Perfect, Thanks David.

This works great and gives a good randomness to the lights. I added “4 times” after the “1023 min” to garauntee intensity and also reduced the “less_than” value to activate on a lower voltage (noise signal), but this is part of the calibration process :smiley:

sub input
  5 get_position
  dup 150 less_than
  if
    drop 0
  else
    1023 min 4 times
  endif
  return

I can now finish the build I think as I am happy with the code so far. Pics and videos to follow.