The Arduino with adxl 345 does not sample at precise intervals

I have a problem with sample time and acceleration.
Can y help me to fix this problem.
This is may code.

example-spi.ino (1.61 KB)

#include <SPI.h>

int i=0;
int size=100;
unsigned long time[100];
unsigned int rawdata[100];
int CS=10;

//ADXL345 Register Addresses
#define DEVID 0x00 //Device ID Register
#define BW_RATE 0x2C //Data rate and power mode control
#define POWER_CTL 0x2D //Power Control Register
#define DATA_FORMAT 0x31 //Data format control
#define DATAX0 0x32 //X-Axis Data 0
#define DATAX1 0x33 //X-Axis Data 1
#define DATAY0 0x34 //Y-Axis Data 0
#define DATAY1 0x35 //Y-Axis Data 1
#define DATAZ0 0x36 //Z-Axis Data 0
#define DATAZ1 0x37 //Z-Axis Data 1

char values[10];
char output[20];

int x,y,z;
double xg, yg, zg;

void setup(){

SPI.begin();

SPI.setDataMode(SPI_MODE3);

Serial.begin(250000);

pinMode(CS, OUTPUT);

digitalWrite(CS, HIGH);

writeRegister(DATA_FORMAT, 0x01);

writeRegister(POWER_CTL, 0x08); //Measurement mode

writeRegister(BW_RATE, 0x0E);
}

void writeRegister(char registerAddress, char value){
digitalWrite(CS, LOW);
SPI.transfer(registerAddress);
SPI.transfer(value);
digitalWrite(CS, HIGH);
}
void readRegister(char registerAddress, int numBytes, char * values)
{
char address = 0x80 | registerAddress;
if(numBytes > 1)address = address | 0x40;

digitalWrite(CS, LOW);
SPI.transfer(address);
for(int i=0; i<numBytes; i++)
{
values = SPI.transfer(0x00);

  • }*
  • digitalWrite(CS, HIGH);*
    }
    void loop()
    {
    for (i=0; i<size ; i++) {
    _ time = micros();_
    * readRegister(DATAZ0, 6, values);*
    _ rawdata = ((int)values[1]<<8)|(int)values[0];
    }
    for(i=0; i<size ; i++){
    Serial.print(time*);
    Serial.print("\t");
    Serial.println(rawdata);
    }
    }*_

The compiler doesn't like itallics or smiley faces in the code. That will cause an error every time.

Now go read the instructions at the top of every board, "How To Use This Forum". The ability to read first and write second is what separates a good programmer from a crappy one.

You aren't making any attempt in loop() to read at particular intervals, it just reads 100 times
as fast as possible, then prints them out.

Have a look at BlinkWithoutDelay example perhaps?