Converting two decimal to 12 bit binary each and assigning value to digital pins

hello everyone

I am stuck at some point in data conversion project and need help.

i wan to convert two decimal values to equivalent 12-bit binary values for each decimal values and assign each binary values to digital pins.

i m using arduino mega 2560.

need your help in coding.

thank you

Hello and welcome,

At least give an example of such decimal numbers and what you expect the output to be.

Such as "123" into 0001 0010 0011 as on/off outputs?
or "789" into 0111 1000 1010?

hello

for example i have decimal 276 and 153 whos equivalent are 000100010100 and 000010011001.

my problem s i have declared local variable x and y which stores processed data (deciamal values), now next step is to convert these decimal values to equivalent 12 bit binary and assign each bit of binary value to digital pins ( i mean 12 * 2 binary values to 24 digital pins).

thank you.

You don't need to convert anything, a number can be written in different ways:

x = 276; // decimal

x = 0b0000000100010100; // binary

x = 0b100010100; // binary with useless zero's removed

x = 0x114; // hexadecimal

It's all the same number.

To get the value of a bit, use bitRead.

Use a for loop to read the first 12 bits, and write the value of the bit to the digital output of your choice.

For example:

uint16_t x = 276;

for ( uint8_t i = 0; i < 12; i++ )
  digitalWrite( i, bitRead(x, i) == 1 ? HIGH : LOW );

that would output:

pin  state
 0     0
 1     0
 2     1
 3     0
 4     1

etc..

Thankq guix

that was so helpful...

appreciate your concern ...

good day