Sunday, November 10, 2019

adxl345_and_335_combined_tx_230400

#include <SPI.h>

#include <RF24_config.h>
#include <RF24.h>
#include <printf.h>
#include <nRF24L01.h>
#include <Wire.h>

RF24 radio(7,8);
byte addresses[][6] = {"12"};

#define DEVICE (0x53) // Device address as specified in data sheet

byte _buff[6];

char POWER_CTL = 0x2D;  //Power Control Register
char DATA_FORMAT = 0x31;
char DATAX0 = 0x32; //X-Axis Data 0
char DATAX1 = 0x33; //X-Axis Data 1
char DATAY0 = 0x34; //Y-Axis Data 0
char DATAY1 = 0x35; //Y-Axis Data 1
char DATAZ0 = 0x36; //Z-Axis Data 0
char DATAZ1 = 0x37; //Z-Axis Data 1
int x,y,z;
const int buttonPin = 2;
int buttonState = 0; 

typedef struct
{
  int x, y, z;
}dataStruct;

 dataStruct data;

 void setup()
 {
   pinMode(buttonPin, INPUT);
   radio.begin();
   radio.openWritingPipe(addresses[0]);

    Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(230400);  // start serial for output. Ensure Serial Monitor at same rate
 // Serial.print("init");
 
  //Put the ADXL345 into +/- 4G range by writing the value 0x01 to the DATA_FORMAT register.
  writeTo(DATA_FORMAT, 00001000);
  //Put the ADXL345 into Measurement Mode by writing 0x08 to the POWER_CTL register.
  writeTo(POWER_CTL, 0x08);
 
 }
 void loop()
 {

   buttonState = digitalRead(buttonPin);
   //data.x = x;
   //data.y = y;
   //data.z = z;
   if (buttonState == HIGH) {
   readAccel(); // read the x/y/z tilt
 
 
   radio.write(&data,sizeof(data));
 
   delay(50);}
 
   else
   {
    data.x = analogRead(A0);
    data.y = analogRead(A1);
    data.z = analogRead(A2);
 
   radio.write(&data,sizeof(data));
 
   delay(50);}
 
 
 }
 void readAccel() {
  uint8_t howManyBytesToRead = 6;
  readFrom( DATAX0, howManyBytesToRead, _buff); //read the acceleration data from the ADXL345

  // each axis reading comes in 10 bit resolution, ie 2 bytes.  Least Significat Byte first!!
  // thus we are converting both bytes in to one int
  int x = (((int)_buff[1]) << 8) | _buff[0]; 
  int y = (((int)_buff[3]) << 8) | _buff[2];
  int z = (((int)_buff[5]) << 8) | _buff[4];


  data.x = x;
   data.y = y;
   data.z = z;

 
 
 
   radio.write(&data,sizeof(data));
 
 
 // Serial.print("x: ");
  Serial.print( x );
  Serial.print(",");
  Serial.print( y );
  Serial.print(",");
  Serial.println( z );
}

void writeTo(byte address, byte val) {
  Wire.beginTransmission(DEVICE); // start transmission to device
  Wire.write(address);             // send register address
  Wire.write(val);                 // send value to write
  Wire.endTransmission();         // end transmission
}

// Reads num bytes starting from address register on device in to _buff array
void readFrom(byte address, int num, byte _buff[]) {
  Wire.beginTransmission(DEVICE); // start transmission to device
  Wire.write(address);             // sends address to read from
  Wire.endTransmission();         // end transmission

  Wire.beginTransmission(DEVICE); // start transmission to device
  Wire.requestFrom(DEVICE, num);    // request 6 bytes from device

  int i = 0;
  while(Wire.available())         // device may send less than requested (abnormal)
  {
    _buff[i] = Wire.read();    // receive a byte
    i++;
  }
  Wire.endTransmission();         // end transmission
}

No comments:

Post a Comment