74HC595 object and high DSP load


#1

Hello!

I'm a beginner in C coding, I learn it to do some Axoloti objects.

Actually I'm driving 4 74HC595 like this:

It's a big mess but it's working.

So I made an object to simplify that:

It's working exept 2 things:

1 - For the first 3 leds it's ok but each next eat 10% of DSP load.

2 - If I hit the led n°32, all the other lit on exept the 32.

I use pow() function, maybe the cause of the high DSP load...

74HC.axo (1.9 KB)

A patch whith 32 disp bool to test
74HC.axp (22.6 KB)


#2

pow() is very expensive. If you're doing X to the power of a whole number, why not just do X * X * X etc. instead?

a|x


#3

I will try to do it without pow()... It was easy to code like this.


#4

Solved.

Code is ugly but it's working good!


#5

Excellent! Good to hear it's now working as it should.

a|x


#6

Hi, don't know how you solved it, so I can only comment on the object you shared (with the pow() function) what I see is a very complicated way of solving a simple problem. there is no need to use if- statements, you already have the values in the led parameters. just bit-shift to the right position and OR them together, bitshifting and boolean math is how the processor works internally so these operations are much faster.

your entire code in the 74HC object can be replaced by a single line of code, like this

outlet_out=(inlet_led1|(inlet_led2<<1)|(inlet_led3<<2)|(inlet_led4<<3)|(inlet_led5<<4)|(inlet_led6<<5)|(inlet_led7<<6)|(inlet_led8<<7))<<(param_0to3 * 8);

try it, you'll like it :sunglasses:


#7

Thanks I will try this even if I don't understand :slight_smile:

As I said I'm a complete beginner in coding.

My actual object output i values: if 0to3 = 0 :1, 2, 4, 8.... , if 0to3=1 : 256, 512,..... 2147483648!

Very annoying to write :smile:


#8

Okay it's working!

That would be cool if you explain me the code a little bit...


#9

it's all 0's and1's..just like the shift register
you don't have to test the led-input because you already have the answer in the inputs, an "on" is a 1, an "off" is a 0, you so you don't need the internal int's for storing the inputs. all you have to do is then to place the 1's ond 0's in the right place. you do so by "bitshifting" to the left.
for instance led 3, if it's "on" the parameter (inlet_led3) is a 1(00000001), then you want to send the 595 a bit in the third place, (00000100). "inlet_led3<<2" does that, it gives you the value bit shifted 2 steps to the left (>> bit sifts to the right). every bit shift to the left doubles the value so x<<2 is actually the same as x*4 and x<<3 is the same as x*8, only much faster because this is how computers work internally so the compiler doesn't have to do any complicated stuff, only pass the values.
right
then you add it up, say you enable led 1, 2, and 5, then you get 00000001, 00000010, and 00010000, add them together and you get 00010011 to send to the shift register
you can add by a simple + of course, but the "|" (bit OR) is simpler in this case and, same as bit shift, how the processor works.
last you bit shift the whole thing 0,8,16 or 24 steps to the left depending on which of the 4 shift registers is set in the "0to3" parameter.

actually, I realise my code was a little sloppy, the param_0to3 * 8 should of course be param_0to3<<3, same thing but faster

outlet_out=(inlet_led1|(inlet_led2<<1)|(inlet_led3<<2)|(inlet_led4<<3)|(inlet_led5<<4)|(inlet_led6<<5)|(inlet_led7<<6)|(inlet_led8<<7))<<(param_0to3 <<3);

hope it's a little clearer now


#10

Yeah thanks it is much clearer but I realize that I have a lot of things to learn :slight_smile:

But it's exciting!

I always wanted to learn C, arduino .... but without a project it's difficult to be motivated. Now with Axoloti I have a good reason to do it!


#11

Same! And with the Axoloti, you can hear the results immediately. Plus the factory and other objects provide a great learning resource.

Having said that, it's a shame there aren't more comments in library objects. Sometimes, when you're generally clueless like me, it's nice to be given some pointers about how things work.

a|x


#12

I had some issues with this code, here is the solution:


#13

in the original example my example did the trick but if the input is not true true/false boolean, then my code won't work, I realise that. (but 32 bit boolean is kind of overkill for a yes/no true/false, isn't it?) so then we're back to the "if" statement in the form of ?:.
I didn't know about that expression, so at least @johannes code is still just one line of code and we all learnt something :slight_smile: