Converting a decimal number to binary bits and a counter with ten LED's

Hello,

Im new here.

Im trying to convert a decimal number for example 1023 to its representation in 10 digits of binary bits.
This should equal to 1111111111 and decimal 540 should equal 1000011100.
Is there a way to do this conversion and display it on the lcd or am I missing something?

Thank You!

Hi ki2 and welcome to the forum!

I’ve created a library that provides nodes to read each bit of a byte:
https://xod.io/libs/wayland/read-bits/

Here’s an example patch:

In the above patch we take a Number and convert it into a 16-bit unsigned integer, before reading each bit of the two bytes. This patch should be run in the debugger. The top node is a tweak-number node - you can change the value of this node at runtime. Here we are displaying output in a watch node, but you could feed this output into your LCD.

1 Like

@wayland I connected the lcd to the concat and I removed a few links to limit the number to 10 binary digits.It works great, thank you for doing that. Using this patch I can display the number on the top line of the LCD and the binary representation on the bottom line.

Now im trying to make it so this 10 digit binary representation is connected to the LED’s when its a 1 its on and when its 0 that LED is off,

Decimal Number goes in gets converted to binary 10 digits, 10 leds represent those digits.

Good to hear it’s working for you.

For your LEDs you can connect the output of each read-X node directly to the LUM input of the corresponding LED.

You can delete the six read-X nodes you are not using.

1 Like

@wayland Yes, that worked excellent. Im very close, May I ask one more request.
In the screen attached.
I have one button, one counter and one clock.

How do I tell the program to ON BUTTON PRESS,
start incrementing the decimal number at the top from 0 to 1023 at 0.5 second intervals and
on button press again to stop, on button press again continue from the same count and
if the decimal reaches 1023 to stop and wait for button press?

@wayland I drew it out if that helps. Im still learning how to program and im on a steep learning curve all mushed. Thank you for your time.

Been at this for hours, am I anywhere close @wayland ?

The clock node has an ENable pin. If you connect flip-flop-MEM to that pin, the clock will only tick when flip-flop is enabled. Connecting button to flip-flop-TGL will toggle the flip-flop on and off. This gives you your start/stop count.

Once you have that, you can work on detecting your maximum number & resetting the count. Hint: you will need to compare count output to your maximum number, then use that output to loop back up your code (which requires a ‘delay’ node) to control clock (or more likely flip-flop-RST) and button function (‘gate’ node can block/enable button output to control flip-flop-TGL and count-RST; there might be other solutions as well).

1 Like

@gweimer
I still dont understand all the objects.
I followed your advice and connected rightButton to flip flop to the enable pin on the clock.
That tick from the clock is connected to count. Then i took a tweak and the count into an add.

I thought I had it down with the if else but i just dont know how to implement it.
Can you take a look at this, by the way thank you for the Utilities in your Libraries WITH examples.
I learned a lot from that.

Don’t you want to count from 1 to 1023? What is the “add 280” for?

If-else-COND needs a boolean fed to it. A number COULD be used, but is NOT what you want in this case. Zero would be interpreted as false. Since count starts at 0 and you add 280 to it, the output of ‘add’ will never be 0, so this if-else will always output the if-else-T value (which you have hard-coded to 1).

You are trying to print values from 0 to 1023, so you should be feeding the output of count to your print-VAL pin. This should provide count with start/stop:
image

To stop counting at 1023 gets a bit more complicated. First, you need to know when count is greater or equal to 1023 (you could just check for equal, but it is “safer” to check for greater or equal; 1022.999999999999 and 1023.000000000001 will “look” equal to 1023, but are not). This information will be needed “higher” in the program, so you will need a ‘defer’ node to “loop” back.

Connecting a wire back “up” the program can get messy, so I am going to add ‘bus’ nodes. These can be thought of as “hidden” wires (i.e. wires running behind your circuit board that you can’t see). Bus nodes with the same Label are all connected. Multiple ‘to-bus’ nodes cannot have the name (you can’t have multiple inputs to a single wire), but you can have multiple ‘from-bus’ nodes with the same name.


Looking at the new nodes below the ‘count’ node, MAX becomes TRUE when count is greater or equal to 1023. If we feed ‘not’ MAX into the ‘gate’ feeding ‘flip-flop’, the ‘flip-flop’ will only change if count is NOT 1023 (we quit turning count on/off if we are at MAX).

If we feed MAX to flip-flop-RST, we are connecting a boolean to a pulse pin. We could add a pulse-on-true node to convert, but that is implied…a boolean “pulses” when it changes to TRUE, so the extra node is not needed. This turns off the flip-flop when count reaches 1023, so we quit counting.

In order to reset count when button is pressed after we reach max, we need to pulse count-RST. Here I have used a second ‘gate’ node that only passes data when MAX is TRUE (we are at max count), so a button press after we reach MAX will reset the counter. This will NOT start counting immediately; connecting the second ‘gate’ output to flip-flop-SET would cause it to immediately start counting on reset.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.