Bit Reversal (LSB - MSB)

I have a set of variables, called d1- d13. They are Integers, 0-15, in Decimal.

I need to convert them to Binary, compare to the chart, and return an int.

If dB= 0000 - 0 1000 - 1 0100 - 2 1100 - 3 0010 - 4 1010 - 5 0110 - 6 1110 - 7 0001 - 8 1001 - 9 1111 - 0

I'm not printing these, they will be used in other calculations.

Use a look up table

That's weird, it seems like normal binary backwards? If you can swap the table to normal like 0001 = 1 not 1000 = 1 then it would line up perfectly no conversion needed another idea is to compare to the normal value of that like 1000 = 8 so if d1 = 8 (do value 1)

I've never built a lookup table. Could use some guidance. I have 13 variables to test/assign against a lookup table that contains 11 data pairs.

A look up is just a table of values.

const byte LUT [] = {
0,8,4,12. etc
  }

See the pattern? (Can you explain where your 13 variables come from?)

[quote author=Steve Spence link=topic=79900.msg603675#msg603675 date=1321991437] I have a set of variables, called d1- d13. They are Integers, 0-15, in Decimal.

I need to convert them to Binary, compare to the chart, and return an int.

If dB=
0000 - 0
1000 - 1
0100 - 2
1100 - 3
0010 - 4
1010 - 5
0110 - 6
1110 - 7
0001 - 8
1001 - 9
1111 - 0

[/quote]

There are 11 numbers there, not 13 or 16. The code for 0 appears twice.

I have an array (generated by a micrometer), that I've pulled out 13 variables, d1 - d13, than can have a value of 0, 1, 2, 4, 6, 8, 9, 10, 12, 14, or 15, that should match up to 0, 8, 4, 2, 6, 1, 9, 5, 3, 7, or 0 respectively. I'm seeing now that the binary intermediate will not be necessary, so this thread should be renamed.

Is there one of the International Forums that is better suited to your native language?

Would you like to explain your comment? I see you deleted my first response. I assumed you were kidding and responded in kind.

Here, a lookup table:

const byte LUT [] = { 
    0,   // 0
    8,   // 1
    4,   // 2
    0,   // 3 not used
    2,   // 4
    0,   // 5 not used
    6,   // 6
    0,   // 7 not used
    1,   // 8
    9,   // 9
    5,   // 10
    0,   // 11 not used
    3,   // 12
    0,   // 13 not used
    7,   // 14
    0    // 15
};


void setup () 
  {
  Serial.begin (115200);
  
  for (byte test = 0; test <= 15; test++)
    {
    Serial.print ("Input of ");
    Serial.print (test, DEC);
    Serial.print (" gives ");
    Serial.println (LUT [test], DEC);
    }
  }
  
  void loop () {}

Output:

Input of 0 gives 0
Input of 1 gives 8
Input of 2 gives 4
Input of 3 gives 0
Input of 4 gives 2
Input of 5 gives 0
Input of 6 gives 6
Input of 7 gives 0
Input of 8 gives 1
Input of 9 gives 9
Input of 10 gives 5
Input of 11 gives 0
Input of 12 gives 3
Input of 13 gives 0
Input of 14 gives 7
Input of 15 gives 0

[quote author=Steve Spence link=topic=79900.msg603749#msg603749 date=1321995874] ... so this thread should be renamed. [/quote]

You can edit your original post and rename the thread.

Thanks. Done.

So, assuming we have the 13 variables, labeled d1 - d13, how would one submit them (in a loop of some sort?) to the lookup table, and insert the results into another set of variables.

So, assuming we have the 13 variables, labeled d1 - d13,

Let's assume they're actually d[0] to d[12]. That'll make things simpler.

Ok, assuming we have the 13 variables, labeled d0 - d12, how would one submit them (in a loop of some sort?) to the lookup table, and insert the results into another set of variables.

No, not d0 to d12. d[0] to d[12]

Ok, please explain. Assume I already have an array containing 13 nibbles (4 bits each),

mydata[i]

that contains my original feed.

Digimatic interface.pdf (164 KB)

AWOL is suggesting that you store your data in an array, rather than individual variables. Then you can do this:

byte Lookup[] = {0,8,4,0,2,0,6,0,1,9,5,0,3,0,7,0};
byte dArray[13]; // contains d0-d12
byte TargetArray[13];  // results

for(int i=0;i<13;i++)
  TargetArray[i] = Lookup[dArray[i]];

I’m not sure how to integrate that with this:

int req = 5; //mic REQ line goes to pin 5 through q1 (arduino high pulls request line low)
int dat = 2; //mic Data line goes to pin 2
int clk = 3; //mic Clock line goes to pin 3
int i = 0; int j = 0; int k = 0;

byte mydata[14];

void setup()
{
  Serial.begin(19200);
  pinMode(req, OUTPUT);
  pinMode(clk, INPUT);
  pinMode(dat, INPUT);
  digitalWrite(clk, HIGH); // enable internal pull ups
  digitalWrite(dat, HIGH); // enable internal pull ups
  digitalWrite(req,LOW); // set request at high
}

void loop()
{  // while( digitalRead(clk) == LOW) { } // hold until clock is high
    digitalWrite(req, HIGH); // generate set request
   // while( digitalRead(clk) == HIGH) { } // hold until clock is low
    for( i = 0; i < 13; i++ ) {
      k = 0;
      for (j = 0; j < 4; j++) {
      while( digitalRead(clk) == LOW) { } // hold until clock is high
      while( digitalRead(clk) == HIGH) { } // hold until clock is low
      //k = (k << 1) | (digitalRead(dat) && 0x1);
      k = k | (digitalRead(dat) && 0x1) << ( 3 - j);
      }
      mydata[i] = k;
 
     Serial.print(k);

      
    }

    Serial.println(",");
    digitalWrite(req,LOW);
    delay(2000); // do a reading every 2 seconds
}

There's a difference between & and &&.