bitWrite() creates drifting bytes (reorganizing bytes and splitting up)

Hi,
I received my arduino yesterday and yet it's plenty of fun ;). I posted a few posts here because I want to build a datalogger.
Currently I am working on some data processing:
an unsigned long needs to be rerranged (byte2,3,0,1) and then be splitted in two parts (first 12 bit for count_data and last bits for time_data)
I use a for-loop which iterates by four to run a subroutine and load the data (byte0-3, byte4-7, and so on) from the bytearray to a bytearray with four bytes for processing. Then I assign the appropriate bits to the corresponding bits in the variable to hold the time.
The first data package (byte0-3) works well, but somehow the second package spins around. I checked the Bytes and they seem to be fine for both variables: looks like it should from the manual :wink:
Well, if I print out the time_variable as binary, the first one looks fine (also in Dez) but the second is messed up. I overlooked it several times but can't find a problem yet.
All seems to work well, but the bit-writing seems a bit messed.
The Code and the processing (is and should be) is above.
Maybe someone knows the problem (or mybe I just need a break ;))
Thanks!

the Code:

byte opc_respond[8]={0x00,0x00,0x37,0x01,0x37,0x06,0x31,0x61};      //,0x37,0x06,0x31,0x61      0x00,0x00,0x37,0x01
byte usl[4]={0x00,0x00,0x00,0x00};

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


void loop()
{
  int i;
  long PBP_count;
  unsigned long PBP_time;

delay(3000);
  for(i=0;i<8;i+=4)
  {
    
    PBP(i);  
    Serial.println("");

  }

 
delay(5000); 
}

//############################process data for Particle by Particle
void PBP(int j)
{
  long count=0;
  usl[0]=opc_respond[j+2];
  usl[1]=opc_respond[j+3];
  usl[2]=opc_respond[j+0];
  usl[3]=opc_respond[j+1];
 Serial.println(usl[0],BIN);
 Serial.println(usl[1],BIN);
 Serial.println(usl[2],BIN);
 Serial.println(usl[3],BIN);
 
 bitWrite(count,0,bitRead(usl[j],0));
 bitWrite(count,1,bitRead(usl[j],1));
 bitWrite(count,2,bitRead(usl[j],2));
 bitWrite(count,3,bitRead(usl[j],3));
 bitWrite(count,4,bitRead(usl[j],4));
 bitWrite(count,5,bitRead(usl[j],5));
 bitWrite(count,6,bitRead(usl[j],6));
 bitWrite(count,7,bitRead(usl[j],7));
 bitWrite(count,8,bitRead(usl[j+1],0));
 bitWrite(count,9,bitRead(usl[j+1],1));
 bitWrite(count,10,bitRead(usl[j+1],2));
 bitWrite(count,11,bitRead(usl[j+1],3));
 Serial.println("");
 Serial.println("New arranged bits: ");
 Serial.print(count,BIN);
}

Processing:
0x00 0x00 0x37 0x01 will be shifted to 0x37 0x01 0x00 0x00
= 0b0011 0111 0b0000 00001 0b0000 0000 0b0000 0000

0x37 0x06 0x31 0x61 will be shifted to 0x31 0x61 0x37 0x06
= 0b0011 0001 0b0110 00001 0b0011 0111 0b0000 0110

Count according to manual:
0b0001 0011 0111
0b0001 0011 0001

so that is fine it seems:
first data package by Program
0b0001 0011 0111 (ok)
second data package by Program
0b1011 1100 0010 (not what I would like to have seen ;))

The problem is that you are indexing usl with j and j+1, not 0 and 1. When j is 4 you have gone off the end of usl.

Not your problem but surely

bitWrite(count,0,bitRead(usl[j],0));
 bitWrite(count,1,bitRead(usl[j],1));
 bitWrite(count,2,bitRead(usl[j],2));
 bitWrite(count,3,bitRead(usl[j],3));
 bitWrite(count,4,bitRead(usl[j],4));
 bitWrite(count,5,bitRead(usl[j],5));
 bitWrite(count,6,bitRead(usl[j],6));
 bitWrite(count,7,bitRead(usl[j],7));
 bitWrite(count,8,bitRead(usl[j+1],0));
 bitWrite(count,9,bitRead(usl[j+1],1));
 bitWrite(count,10,bitRead(usl[j+1],2));
 bitWrite(count,11,bitRead(usl[j+1],3));

could be

count = usl[j] + (usl[j+1] << 8);

or did I miss some nuance of the code?

Also j is running from 0 to 7 and indexing into an array of 4 bytes (usl).


Rob

Thaaanks!... OK, I need a break it seems. How is it about the trees and the forest ;)...

You're right. Indexing with j makes no sense, because the data is fixed in here. I had the original data in mind somehow.