converting decimal to gray code help

Hi, im a beginner and im writing a program to convert decimal to gray code. So far I am able to convert from decimal to binary. I would like to take the resulting binary numbers and convert to gray code. Thank you

#include <Wire.h> 
#include <LiquidCrystal.h>
#include <Keypad.h>
#include <String.h>

int screenWidth = 16;
int screenHeight = 2;

String line1 = "Decimal-Graycode";
String line2 = "Converter ";
String line3 = "2014 Copyright";
String line4 = "SB";

const byte rows = 4;
const byte cols = 4;

char keys[rows][cols] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

byte rowPins[rows] = {9,8,7,6 };
byte colPins[cols] = {5,4,3,2};

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);

LiquidCrystal lcd(12,11,5,4,3,2);  

void setup ()
{
 Serial.begin(9600); 
 lcd.begin(screenWidth,screenHeight);
 lcd.setCursor(0,0);
intro();

}

void loop ()
{
  getNumber();
}

void intro ()
{   
    lcd.setCursor(0,0);
    lcd.print(line1);
    lcd.setCursor(0,1);
    lcd.print(line2);
    
    
    delay(3000);
    
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print(line3);
    lcd.setCursor(0,1);
    lcd.print(line4);
   
    
    delay(3000);
    lcd.clear();
 
}

int getNumber()
{
  unsigned int num = 0;
  static int a[8],b[8],k=1,i;
  int ans;
  char key = keypad.getKey();
  while(key != '#')
   {
      switch (key)
      {
         case NO_KEY:
            break;

         case '0': case '1': case '2': case '3': case '4':
         case '5': case '6': case '7': case '8': case '9':
            lcd.print(key);
            num = num * 10 + (key - '0');
            break;

         case '*':
            num = 0;
            lcd.clear();
            break;
            
         case 'A':
         lcd.setCursor(0,1);
         lcd.print(num,BIN);
          break;   
      }

      key = keypad.getKey();
   }

   return num;
}

Numbers in a computer are stored as binary -- decimal is just one representation of a binary number.

If you want to interconvert Gray-type binary code into conventional binary, the Wikipedia article provides some example C code: Gray code - Wikipedia

Have a look at: Converting Between Gray and Binary Codes - Wisc-Online OER

how do i write that in c++ language?

steinie44:
Have a look at: Converting Between Gray and Binary Codes - Wisc-Online OER

Very nice.

scorp_88:
how do i write that in c++ language?

You don’t even saw the links!

/*

The purpose of this function is to convert an unsigned
        binary number to reflected binary Gray code.

The operator >> is shift right. The operator ^ is exclusive or.
*/
unsigned int binaryToGray(unsigned int num)
{
        return (num >> 1) ^ num;
}

/*
        The purpose of this function is to convert a reflected binary
        Gray code number to a binary number.
*/
unsigned int grayToBinary(unsigned int num)
{
    unsigned int mask;
    for (mask = num >> 1; mask != 0; mask = mask >> 1)
    {
        num = num ^ mask;
    }
    return num;
}

Thanks for the links. Thats binary to gray code. I know how to get that but how do i go from decimal to gray code. For example take the result from decimal to binary and convert it to gray code.

Google it. The binary to gray code was not hard to find.

scorp_88:
Thanks for the links. Thats binary to gray code. I know how to get that but how do i go from decimal to gray code. For example take the result from decimal to binary and convert it to gray code.

That is a wrong! In a computer (or for the case an Arduino) there ARE NOT decimal numbers! All the numbers are stored in binary. The computer can show it to you in different forms, but it will be always in binary.

here is a code in c language to convert decimal to gray code.int - Gray Code Conversion | DaniWeb. Are you saying that its no possible to write a similar code in arduino?

please i all i need the code in C++ or arduino language because the code in c language doesn't work. thanks

here is a code in c language to convert decimal to gray code.int - Gray Code Conversion | DaniWeb. Are you saying that its no possible to write a similar code in arduino?

please i all i need the code in C++ or arduino language because the code in c language doesn't work. thanks

You might put forth some effort to convert it to C++. It's not that hard to do.
We are here to help get the job done, so you can learn something.
Not do it for you.

scorp_88:
here is a code in c language to convert decimal to gray code.int - Gray Code Conversion | DaniWeb. Are you saying that its no possible to write a similar code in arduino?

scorp_88:
please i all i need the code in C++ or arduino language because the code in c language doesn't work. thanks

That code will work, the same way that the code from wikipedia will work too.

But the code from your link or was wrote by someone that don't know what a processor is and how it work (and store information, etc.) or was wrote by someone that was trying to explain how that can be made "by and" and how easy a computer can do that thing (repeating calculations). You can do it like that if you want but you don't need to, because half the work is already done!

EDIT: BTW, this:

/* gray code conversion */
b[0] = a[7];
for (i = 7; i >= 0; i--)
{ if (a[i] == 0 && a[i - 1] == 0)
    b[k] = 0;
  if (a[i] == 1 && a[i - 1] == 1)
    b[k] = 0;
  if (a[i] == 0 && a[i - 1] == 1)
    b[k] = 1;
  if (a[i] == 1 && a[i - 1] == 0)
    b[k] = 1;
  k++;
}

is exactly the same thing that this:

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

What do you like more? I like more the second way.

And this:

while (n1 != 0) /* converting number to its binary equivalent */
{
  a[i] = n1 % 2;
  n1 /= 2;
  i++;
}

Is bullshit. (OK, can be done, but you don't need to do it).

As the problem is very simple, here is one solution, made in a moment only to show “my point”:

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

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


byte binaryToGray(byte num)
{
   return (num >> 1) ^ num;
}


void showBits (byte number) {
   byte showBit;
   
   for (int i=0; i<8; i++) {
      showBit = ((number<<i) & 0x80) ? 1 : 0;
      
      Serial.print (showBit);
      
      if (i == 3) {
         Serial.print (" ");
      } else if (i == 7) {
         Serial.print ("\n");
      }
   }
}

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

  pinMode(ledPin, OUTPUT);
}


void loop() {
  byte binary;
  byte gray;

  if (Serial.available()) {
    binary = Serial.read() - '0';
    
    blinkLED();
    
    Serial.print ("Original \"decimal\" number: ");
    Serial.println (binary);
    
    Serial.print ("Binary: ");
    showBits (binary);
    
    gray = binaryToGray( binary );
    
    Serial.print ("Gray  : ");
    showBits (gray);    
  }
}

The conversion is made by the function binaryToGray and is the code from the wikipedia. The code converts numbers from 0 up to 9 (bigger numbers is up to you) sent to the Arduino board through serial port. When one number is received the LED in the board blink (only for fun :slight_smile: ). I wrote a function to send to the serial port one number in the binary format (that shows both the original binary number and the converted gray number). This line:

binary = Serial.read() - '0';

receives the number sent through the serial port and convert it from ASCII (this is the way that you receives the key ‘5’, for example) to decimal (or binary if you want).

Do this help?

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.

Did you tried my code? The result is:

Original "decimal" number: 5
Binary: 0000 0101
Gray  : 0000 0111

Why this does not work for you?

EDIT: What is your code and what you really what to do ?

luisilva:
Did you tried my code? The result is:

Original "decimal" number: 5

Binary: 0000 0101
Gray  : 0000 0111




What you need more?

EDIT: What is your code and what you really what to do ?

yes that code from wikipedia does not work.

I am trying to build a converter which will convert decimal numbers into gray code using arduino.

``#include <Wire.h>
#include <LiquidCrystal.h>
#include <Keypad.h>
#include <String.h>

int screenWidth = 16;
int screenHeight = 2;

String line1 = “Decimal-Graycode”;
String line2 = "Converter ";
String line3 = “2014 Copyright”;
String line4 = “SB”;

const byte rows = 4;
const byte cols = 4;

char keys[rows][cols] = {
{‘1’,‘2’,‘3’,‘A’},
{‘4’,‘5’,‘6’,‘B’},
{‘7’,‘8’,‘9’,‘C’},
{’*’,‘0’,’#’,‘D’}
};

byte rowPins[rows] = {9,8,7,6 };
byte colPins[cols] = {5,4,3,2};

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);

LiquidCrystal lcd(12,11,5,4,3,2);

void setup ()
{

lcd.begin(screenWidth,screenHeight);
lcd.setCursor(0,0);
intro();

}

void loop ()
{
getNumber();

}

void intro ()
{
lcd.setCursor(0,0);
lcd.print(line1);
lcd.setCursor(0,1);
lcd.print(line2);

delay(3000);

lcd.clear();
lcd.setCursor(0,0);
lcd.print(line3);
lcd.setCursor(0,1);
lcd.print(line4);

delay(3000);
lcd.clear();

}

int getNumber()
{
unsigned int num = 0;

char key = keypad.getKey();
while(key != ‘#’)
{
switch (key)
{
case NO_KEY:
break;

case ‘0’: case ‘1’: case ‘2’: case ‘3’: case ‘4’:
case ‘5’: case ‘6’: case ‘7’: case ‘8’: case ‘9’:
lcd.print(key);
num = num * 10 + (key - ‘0’);
break;

case ‘*’:
num = 0;
lcd.clear();
break;

case ‘A’:
lcd.clear();
lcd.setCursor(0,0);
num = lcd.pri(num, BIN);

lcd.setCursor(0,1);
lcd.print(num >> 1) ^ num;
break;

}

key = keypad.getKey();
}

return num;

scorp_88:
yes that code from wikipedia does not work.

I am trying to build a converter which will convert decimal numbers into gray code using arduino.

And is what this code do?! This code was wrote for Arduino, the result was paste from the "Serial monitor" after my Arduino MEGA send it to me. So, I don't know why this code does not work for you.

im using an lcd. anyway can you please tell me what i need to add or take out from my code because im not getting the same results. thanks