Binary to Decimal Conversion Problem

Hi guys,

I want to send register commands through an I2C connection for a sensor. The sensor's registers accepts decimal code, but my Arduino needs firstly to ,,create" the command binary and then send it as a decimal value, so I must program a convertor.

The problem is that the code doesn't appear to function properly at all. (I introduce the equivalent of 25 and it gives me 26, I put 33 and it gives me 1)
And the code is just fine.

This is the program:

void setup()
{
  Serial.begin(9600);
}

void loop()
{
   int unsigned bin=100110; //binary code + i.e
   byte output; //decimal converted number
   for(byte a; bin>0; a++) 
   {
     if(bin%10==1)
     {
       output+=(1<<a);
     }
     bin/=10;
   }
   Serial.println(output); //printing the data through serial
}

Thank you,
RobertEagle

PS: Just for the record, the sensor is a ADXL345

byte output = 0b100110

Note the 0b to tell the compiler it is a binary literal.

Still doesn't work. The same things. Everything looks very clear in the program, but it continues the same way.

RobertEagle:
Still doesn't work. The same things. Everything looks very clear in the program, but it continues the same way.

Where are you getting this binary data from? If you're hardcoding it in, then use what Tom said:

void setup() {
  Serial.begin(9600);
  int output = 0b100110;
  Serial.println(output);
}

100110 does not fit in an int, or indeed in an unsigned int

The binary was just an example. I wrote that in the comment section.
I'll give you just an example:

In the datasheet there is a register called 0x32, and that register can hold 8 bits.
The 1st and the 2nd bit is for Mode selecting(4 modes. 00/01/10/11).
Then the 3rd bit is for trigger.(0/1)
And then the other 4 bits are for other input.(0000...1111)

The communication is made over I2C and it is an ADXL345, as I said earlier.

The idea is that in my Arduino I select what I have to, transform all the values into a binary one, and then send them over I2C as a decimal.

If there's another option I'm glad hear it. (I don't want to use other's library)

Thank you,
RobertEagle

Last time I looked, I2C worked in binary.
That is a couple of days ago, though.
I suspect you may be over thinking the problem.

The register isn't called 0x32, that's its address.

Well, that is.

The range is 0-255. So when I send 255, the Arduino sends it as a binary code (11111111), because this is how the sensor perceps.
I mean, this is what the datasheet is telling me. An 8-bit code which can be translated into a decimal.

Sorry for my mistake regarding the registers.
Is there a more simplistic approach? If it would be, I'll be much glad.

RobertEagle:
An 8-bit code which can be translated into a decimal.

Every 8-bit code can be translated into Decimal. Decimal values are just a more human-friendly way of reading and handling the values.

I still don't understand what the problem is.

If you want to write a value, be it using the decimal or binary form, what's stopping you?

Yes I know. I replied AWOL.

I need a binary-to-decimal convertor. That's all. The configuration is made trough 0's and 1's in Arduino. Then the configuration must be printed to i2c as a decimal value. If it would have existed a Serial.print(DEC) for I2C I wouldn't have bothered now doing this.

We are struggling to work out why you need one. The compiler converts all decimal values to binary anyway.

Perhaps if you posted your full code (the one you posted has no mention of any I2C or whatnot), and also explained what you are trying to do, i.e. where this weird decimal representation of binary comes from, we might be able to help.

Ok.

http://www.sparkfun.com/datasheets/Sensors/Accelerometer/ADXL345.pdf - this is the datasheet from sparkfun of ADXL345 sensor.
Go to page 17, in the top right corner, and you'll see for example register 0x31.
Look at the table: you see D7, D6, D5, D4, D3, D2, D1, D0. Every bit of this byte has a function in this register. (enable and disable).
For example if I want to enable the 3-pin SPI at position D6 I have to send 01000000 code. (in case the other bits are 0's). The equivalent in decimal is 64. But if I want to enable both the SPI and Justify bit I have to send 01000100 which in decimal equals 68.

The problem is that I cannot send the values directly over I2C binary, because the Wire doesn't let me do it. I just can send one value at a time. And the solution is to convert the 8-bit code into a decimal and then print it over I2C. That's why I need a Binary-Decimal convertor. Mine looks ok, but I don't understand why it isn't working.

And...tell me if I say something wrong.
Thank you,
RobertEagle

The problem is that I cannot send the values directly over I2C binary, because the Wire doesn't let me do it.

I don't understand this statement. What happens/fails to happen when you try?

I just can send one value at a time.

Like 64 or 68?

Look here:

Take a look at the syntax and parameters. They say "a value to send as a single byte".
If I would print 10111011, the Wire would translate it in a binary code, because he is considering the given value a decimal.
And yes, like 64 or 68.

If I would print 10111011, the Wire would translate it in a binary code, because he is considering the given value a decimal.

Yeah. But, 0b10111011 would be sent correctly.

0b10111011 = 187 = a single byte.

wire.write(187);

does exactly the same as

wire.write(0b10111011);

Because both fit quite happily into a byte type variable.

Oh yes? You're right. Why I didn't observe this :grin:..?
So much perspiration for this simple thing...

I was so concentrated on this datasheet..

Thank you very much again $)