converting decimal to gray code help

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.)

How many digits do you need? If just a few, might be easier to make a look up table.

25-30 digits. Do you have any examples of a look up table?

scorp_88:
25-30 digits. Do you have any examples of a look up table?

Really? Decimal digits, or binary digits?

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

So, with 4 decimal digits you can reach 9999, that is, less that 14 bits, so a int variable, can hold that number.

So, you can change the original sketch to:

const int ledPin = 13;      // the pin that the LED is attached to

unsigned int integer=0;
byte count=0;
unsigned int gray;
byte chRead;

boolean valueCompleted = false;


void blinkLED (void) {
   digitalWrite(ledPin, HIGH);
   delay(200);
   digitalWrite(ledPin, LOW);
}


unsigned int binaryToGray(unsigned int num)
{
   return (num >> 1) ^ num;
}


void showBits (unsigned int number) {
   byte showBit;
   
   for (int i=0; i<16; i++) {
      showBit = ((number<<i) & 0x8000) ? 1 : 0;
      
      Serial.print (showBit);
      
      if (i == 3 || i == 7 || i == 11) {
         Serial.print (" ");
      } else if (i == 15) {
         Serial.print ("\n");
      }
   }
}

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

  pinMode(ledPin, OUTPUT);
}


void loop() {
  
  if (Serial.available()) {
    chRead = Serial.read();
    
    if (chRead == '\n') {
       valueCompleted = true;
       count = 0;
    }
    else if (chRead >= '0' && chRead <= '9' && count < 4) {
       integer = integer*10 + chRead - '0'; 
       count++;
    }
    
  }   
  if (valueCompleted == true) {
    valueCompleted = false;
    
    blinkLED();
    
    Serial.print ("Original \"decimal\" number: ");
    Serial.println (integer);
    
    Serial.print ("Binary: ");
    showBits (integer);
    
    gray = binaryToGray( integer );
    
    Serial.print ("Gray  : ");
    showBits (gray);    
    Serial.println (" ");
    integer = 0;
  }
}

The result is:

Original "decimal" number: 1234
Binary: 0000 0100 1101 0010
Gray  : 0000 0110 1011 1011

Original "decimal" number: 12
Binary: 0000 0000 0000 1100
Gray  : 0000 0000 0000 1010

Original "decimal" number: 9999
Binary: 0010 0111 0000 1111
Gray  : 0011 0100 1000 1000

Original "decimal" number: 123
Binary: 0000 0000 0111 1011
Gray  : 0000 0000 0100 0110

Original "decimal" number: 12
Binary: 0000 0000 0000 1100
Gray  : 0000 0000 0000 1010

Original "decimal" number: 5
Binary: 0000 0000 0000 0101
Gray  : 0000 0000 0000 0111
void showBits (unsigned int number) {
   byte showBit;
   
   for (int i=0; i<16; i++) {
      showBit = ((number<<i) & 0x8000) ? 1 : 0;
      
      Serial.print (showBit);
      
      if (i == 3 || i == 7 || i == 11) {
         Serial.print (" ");
      } else if (i == 15) {
         Serial.print ("\n");
      }
   }
}

Please can you tell me how to show the result in 12 bits or explain the code to me so i can fix it. thanks

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.

tried it not working

Yes, you are right. I'm sorry.

You must do this:

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:

41/02=21 1
41/04=10 0
41/08=05 1
41/16=03 1
41/32=01 1
41/64=01 1

Gray code: 111101