reading data over Serial and bitwise operators

Hey guys!

Ive been trying to read multiple sensors from ardunio and get that data into oF

I came across a
useful-post from a while back which works beautifully (thanks theo)… for one sensor.

Naturally theo is using some foreign concept called bitwise operations, to be all clever =D

So i went and researched about these things, and came across a helpful-post which so rather useful for someone whos got the concept down pat.

so for one sensor he has

  
unsigned char val1;  
  unsigned char val2;  
  
       //gnarly code to turn the int into two bytes     
  
          val1 = value &0xFF;  
          val2 = (value >> 8) &0xFF;   
            
          //send both bytes  
         Serial.print(val1, BYTE);  
         Serial.print(val2, BYTE);  

and then back in oF to convert it back

  
sensor_01 = ( (unsigned char)bytesReadString[1] << 8 | (unsigned char)bytesReadString[0] );  


So while i stumble around and hopefully fluke the right answer, does anyone have some pointers to extend the code to consist of 4 varibles?

Thanks heaps!

Hi,

an alternative to bitshifting, might be to consider unions.

They allow you to access a unique memory location as a set of different variable types. For instance you could create a union “class” to represent both an int and and unsigned char array of the size of an int (i.e. 4 in case of a 32 bit int). You can then read/write to a variable of this union type either using its int representation or its char*[] representation. I don’t have relevant code with me right now, but this link should help you with the concept.

hope this helps.

regards
david

ps. btw this also works on the arduino side of things