Simple LED button setup

Discuss Lemur and share techniques.
Post Reply
nizer
Newbie
Posts: 14
Joined: 03 May 2015 16:40

Simple LED button setup

Post by nizer »

Loving Lemur. I have a basic question and as I am not a expert coder am stuck.

My wish is to click a PAD button and have it turn on a series LEDS. With each push of the button the next LED in line would turn ON and the other LED would remain ON.

Basically counting how many times the Pad button has been pushed.

Also a simple reset would be nice.

I have played with Stretch, firstof etc to no joy.

If NumberofPushesOfButton = 0 then;
setattribute (myLED,'value',{0,0,0,0,0});
else;
If NumberofPushesOfButton = 1 then;
setattribute (myLED,'value',{1,0,0,0,0});
else;
If NumberofPushesOfButton = 2 then;
setattribute (myLED,'value',{1,1,0,0,0});
else;
If NumberofPushesOfButton = 3 then;
setattribute (myLED,'value',{1,1,1,0,0});
else;
If NumberofPushesOfButton = 4 then;
setattribute (myLED,'value',{1,1,1,1,0});
else;
If NumberofPushesOfButton = 5 then;
setattribute (myLED,'value',{1,1,1,1,1});

Any help would be so appreciated.
Softcore
Regular
Posts: 1613
Joined: 04 Nov 2012 08:34

Re: Simple LED button setup

Post by Softcore »

*See posts below*
Last edited by Softcore on 21 May 2015 12:42, edited 2 times in total.
nizer
Newbie
Posts: 14
Joined: 03 May 2015 16:40

Re: Simple LED button setup

Post by nizer »

So simple and perfect!!!!

I just tweaked it into what I needed it for and it is BRILLIANT. Exactly what I was trying to do for the last three DAYS!!!

Thank you so much for the help.

If you feel like it, I would love a explanation of the script and what it is doing.

Code: Select all

count=sumof(Leds.val)

act() script: Leds.val[count]=1;
Softcore
Regular
Posts: 1613
Joined: 04 Nov 2012 08:34

Re: Simple LED button setup

Post by Softcore »

We have created a custom expression "val" inside our Leds to use it in their "value" field.

Obviously this custom expression must be the size of n, where n is the number of LEDs we use (columns * rows).

Also, obviously, in our initial state, all leds are off.....so in our 16 LEDs example our custom value should be

Code: Select all

val = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
or simply

Code: Select all

val = stretch(0,16)
Now, what we are after is that on each subsequent press of the pad, the next LED that is off, goes on.
lets see though what that actually means:

When all LEDS are off then we need to go
Leds.val={1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}

At the next press:
Leds.val = {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}

at the next press:
Leds.val={1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0}

and so on....

Remember though we started with
Leds.val = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}

So that means that on the first press we simply have to go:
Leds.val[0]=1;

next press
Leds.val[1]=1;

next press
Leds.val[2]=1;

and so on....

So, all we need is a "counter" that will start from 0, go up by one after each press and use it as our "val" index in the script (the number inside those [ ] )

But here's the smart part....
sumof(array) function returns the addition of all the elements of an array.

So when val = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
then sumof(Leds.val) = 0;

when Leds.val = {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
then sumof(Leds.val) = 1

and so on...

So....in our Pads script we have typed:

Code: Select all

Leds.val[count]=1;
Lets see what this does, when all Leds are off (initial state).

When all Leds are off, count = sumof(Leds.val) = sumof(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0) = 0+0+0.....+0 = 0
so Leds.val[count] = 1 -> Leds.val[0] = 1;

Hooray, our first Led is lit.

Now we press the pad again....
this time though, since the first Led is lit from the previous press ( so from the previous press Leds.val = {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} )
count = sumof(Leds.val) = sumof(1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0) = 1+0+0.....+0 = 1;
so
Leds.val[count] = 1 -> Leds.val[1] =1;

Hooray our second Led is lit....Trust me, :P , on each subsequent press, the result will be what we need - on each subsequent press the "count" value will have risen by one, and this will cause the next LED of the array to go 1 (on)

And this how it all works....Notice, that in the "reset" button we have:

Code: Select all

Leds.val=stretch(0,16);
all we have to do, is set the Leds.val again to {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} and this also resets our "counter" (count) because now, sumof(Leds.val) will be zero again.

___________________________________________________

semantics:

NOT having thought that count=sumof(Leds.val)
it would still be possible to achieve the same thing by "manually" adding 1, to our counter.....so...
we could simply have an undefined "count" expression (leave it blank) and in our Pads script go:

Code: Select all

Leds.val[count]=1;
count ++;
This would achieve the same thing - the only difference being, that in our "reset" Pads, we would also have to explicitely reset "count" too like so:

Code: Select all

Leds.val=stretch(0,16);
Pads.count = 0;
It would all still work! ;)
nizer
Newbie
Posts: 14
Joined: 03 May 2015 16:40

Re: Simple LED button setup

Post by nizer »

Thanks for the detailed explanation and code. I am so grateful.

I listened to your music!!!! Awesome stuff. Gonna buy it and practice to it today. me… http://nizer.com
nizer
Newbie
Posts: 14
Joined: 03 May 2015 16:40

Re: Simple LED button setup

Post by nizer »

Not that anyone may care or need it, here is a tweaked version with 2 examples, some credits and a monitor.

Again, massive thanks to Softcore. You have made a friend and a new FAN.
Attachments
PadsToLeds-2.jzml
(15.06 KiB) Downloaded 107 times
Softcore
Regular
Posts: 1613
Joined: 04 Nov 2012 08:34

Re: Simple LED button setup

Post by Softcore »

hey thats very kind of you! Thanks! ;)

Im deleting my initial example since we have the final versions in your attachment.
Post Reply