scorp_88:
Please i have tried everything im still not getting the results i want. I am able to convert to decimal to binary but cant convert binary to gray code.
As others have said, you need to exert some effort, and tell the forum about what you have tried.
Post the code you are currently using, and provide a detailed description of what it is doing wrong. Tell us about the different things you've tried.
Note that the Arduino runs C or C++ just as easily. C++ can do everything that C can do, plus more, so C code is also valid C++ code (with a few obscure exceptions having to do with data typing that won't be an issue for you.)
maybe i misunderstood his question. If he referring to digits in decimal its up to 4 digits. I thought he meant the number of pairs im using in the look up table which will be 25 - 30 columns
Yes. As the result is shown in 16 bits, and you want 12, maybe changing in the place where you have 16 to 12 can do that. Do you try it?
Change too the if 15 to 11.
void showBits (unsigned int number) {
byte showBit;
for (int i=0; i<12; i++) {
showBit = ((number<<i) & 0x0800) ? 1 : 0;
Serial.print (showBit);
if (i == 3 || i == 7) {
Serial.print (" ");
} else if (i == 11) {
Serial.print ("\n");
}
}
}
What I did:
I changed the 16 to 12;
I changed the 15 to 11;
I delete the || (i==11) from the first if;
and (the most important thing) I changed 0x8000 by 0x0800.
I didn't realise about this last thing in the first post.
NOTE that:
0x8000 = 0b1000 0000 0000 0000
and
0x0800 = 0b0000 1000 0000 0000
This "method" checks the most significant bit (or MSb - that is, the bit at the left) and when you change the number of bits that you want to show, the MSb "changes is place" (goes from the "real" MSb to the MSb of the 12 bit number).
I discovered the following method. I want to find the gray code of number 41. It is 2^5<41<2^6, so the 41 will have 6 binary digits. Divide the 41 by the six numbers 2, 4, 8, 16, 32, 64. Round each quotient to the nearest integer. If this number is odd, write next to this digit 1, otherwise write the digit 0: