Combining bytes into multiple integers


i'm new type union combining bytes float/integer. issue anyways covered on internet. now, question is there way of combining bytes multiple integers without creating loop?

my sensor data looks so:
d0    d1    d2    d3    d4    d5    d6    d7
16   128   63   126    110   120

every pair d0-d1 consists of 1 byte. want assign each byte int.

i have coded this:

void loop(){

int combined;
int fx,fy,fz;

       
for(int i=0;i<5;){

   combined = message.data[i+1];
   combined = combined<<8;
   combined |= message.data;
   if (message.id == 970){  //filtering message data
        if (i==0){
                fx = (((combined - 32768)*scale_factor)/32768)+0;
              }
              if (i==2){
                fy = (((combined - 32768)*scale_factor)/32768)+21;
              }
              if (i==4){
                fz = (((combined - 32768)*scale_factor)/32768)+46;
              }
          }
   i=i+2;
}
serial.print(fx)
serial.print(", ")
serial.print(fy)
serial.print(", ")
serial.print(fz)

but seems read wrong.

i wanted use union this:

union{
      byte databytes[3];
      int value;
     } sensordata;

void loop(){

sensordata.databytes[0]=message.data[0]
sensordata.databytes[1]=message.data[1]
sensordata.databytes[2]=message.data[2]

serial.println(sensordata.value)

but think combine 3 bytes single int. need use 3 unions solve this?


please use code tags (</> button on toolbar) when post code or warning/error messages. reason forum software can interpret parts of code markup (for example, italics in code above), leading confusion, wasted time, , reduced chance problem. make easier read code , copy ide or editor. if browser doesn't show posting toolbar can manually add code tags:
[code]// code here[/code]
using code tags , other important information explained in how use forum post. please read it.

please tools > auto format on code before posting it. make easier spot bugs , make easier read. if you're using arduino web editor not have access useful tool it's still unacceptable post poorly formatted code. recommend use standard ide instead.


Arduino Forum > Using Arduino > Programming Questions > Combining bytes into multiple integers


arduino

Comments

Popular posts from this blog

Error compiling for board Arduino/Genuino Uno.

Installation database is corrupt

esp8266 (nodemcu 0.9) client.write très lent ???