Printing data from I2C communication

Hello,

I need some help over understanding some lines of code and maybe replacing them with possible to read for me ones.

I'm trying to make my accelometer work which is part of my thesis subject, for now I want to know how to read values of acceleration in three axis I read from datasheet registers adresses I can pick any axis register value and put it in the code so the value of g in this axis would be read, but since I'm using some found code for some other accelerometer I don't know how to aquire data from all 3 of the axis at the same time.

My question is if somebody could explain me why "char output[512]" is used on top and I know that it stores data for print but I don't know how exacly, and also I dont know what this lines does:

z = (((int)values[1]) << 8);

sprintf(output, "%d", z);

Serial.print(output);

// I mean I know that Serial.print just sends data as a string to serial monitor but it works as combination with sprintf and I not sure how?

Could somebody maybe write me example that could receive values of registers on top of the code and print them all as a single string using Serial.print maybe?

I attach datasheet for my accelometer so maybe you could also check if I'm connecting and communicating with it correctly?

   //Hex values for registers: X-0x29 ; Y-0x2B ; Z-0x2D
       // Ctrl_Reg1 - 0x20

       
        #define accel_module (0x1D)
        byte values[6];
        char output[512];
        
        
        #include <Wire.h>
        
        void setup()
        {
          Wire.begin();        // join i2c bus (address optional for master)
          Serial.begin(9600);  // start serial for output
          Wire.beginTransmission(accel_module);
          Wire.write(0x20);
          Wire.write(64);
          Wire.endTransmission();
        }
          

      void loop()
      {
          int xregister = 0x2D;
          int x,y,z;
          
          Wire.beginTransmission(accel_module);
          Wire.write(xregister);
          Wire.endTransmission();
          
          Wire.beginTransmission(accel_module);
          Wire.requestFrom(accel_module, 7);
          
          int i = 0;
          while(Wire.available()){
          values[i] = Wire.read();
          i++;
          }
          Wire.endTransmission();
          
            z = (((int)values[1]) << 8);
            
          sprintf(output, "%d", z); 
          Serial.print(output);
          Serial.write(10);                        
          
                     
          delay(100); 
      }

LIS35DE.pdf (646 KB)

This code already acquires the values from all 3 accelerometers. It defines an array of 6 bytes to store the values. Then it starts a request to read from the X register. Since the registers are stored in order and each byte request sends the next register in sequence, 6 reads will read all the bytes for the 3 accelerometers. The first two bytes will contain the value for X, the next two for Y and the last 3 for Z.

HOWEVER, it seems to be requesting 7 bytes from the chip. This will cause an overrun and will damage other memory, since the Arduino doesn't check if you are accessing items outside of the declared length of an array. Unless you know why it makes 7 requests, I would change this to 6.

Then it needs to convert bytes to integers. A byte has 8 bits and can store the values from -128 to +127. An integer has 16 bits and can store values between -32768 and +32767. The bit-shift operator << is often used for this, to take the high byte and move it up into the appropriate place in the integer variable. The calculation in the code is only using one byte of the X accelerometer and storing it in a variable called Z. You need to fix this. Go back to the original code you copied and find the rest of it.

The last part of the code converts the integer into a string which can be printed to the serial monitor. It uses sprintf(), which is a version of the common C function printf() which writes to a string. This can be used to control formatting and add other information in one simple step. It is rarely used in Arduino programming as you can usually just write out the strings and values in sequential lines of code. This example is using ONE QUARTER of all of your available memory to store something which doesn't need to be stored at all.

Thank you for the reply and sorry for posting "damaged code" I've been doing some experiments with it to learn which part does what but, from your post I think I should start with learning more about functions in C, sorry for trouble and thanks again for brief theoretical explonation.