Page 1 of 1

Simple LED button setup

Posted: 18 May 2015 15:01
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.

Re: Simple LED button setup

Posted: 19 May 2015 19:29
by Softcore
*See posts below*

Re: Simple LED button setup

Posted: 20 May 2015 03:16
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;

Re: Simple LED button setup

Posted: 20 May 2015 18:38
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! ;)

Re: Simple LED button setup

Posted: 20 May 2015 19:52
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

Re: Simple LED button setup

Posted: 21 May 2015 10:33
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.

Re: Simple LED button setup

Posted: 21 May 2015 12:41
by Softcore
hey thats very kind of you! Thanks! ;)

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