K
K
Kirill Yashin2019-02-25 10:35:33
Arduino
Kirill Yashin, 2019-02-25 10:35:33

How to convert 4 bit Gray code to binary in Arduino?

Hello
There is a code found on the Internet:

boolean A, B, C, D, S0, S1, S2, S3;

void setup ()
{
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(4, INPUT);
  pinMode(5, INPUT);
  Serial.begin(9600);
}

void loop()
{
  A=digitalRead(2);
  B=digitalRead(3);
  C=digitalRead(4);
  D=digitalRead(5);
  
  S0=A;
  S1=(!A&&B)||(A&&!B);
  S2=(!B&&C)||(B&&!C);
  S3=(!C&&D)||(C&&!D);

  Serial.print("Grey:");
  Serial.print(S0);
  Serial.print(S1);
  Serial.print(S2);
  Serial.println(S3);
  Serial.print("Bin:");
  Serial.print(A);
  Serial.print(B);
  Serial.print(C);
  Serial.println(D);
  
  delay(1000);
}

The problem is that it converts binary to Gray, and I need it the other way around
. Please tell me how to do it

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2019-02-25
@Kirill_Iashin

A = S0;
B = A^S1;
C = B^S2;
D = C^S3;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question