General

Simple LED button setup

Started by nizer on .

Sign in or create an account to join the conversation.

7 posts

Topic starter
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.
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.
count=sumof(Leds.val)

act() script: Leds.val[count]=1;
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
val = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
or simply
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:
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:
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:
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:
Leds.val=stretch(0,16);
Pads.count = 0;
It would all still work! ;)