Hi guys, i just started using Arduino, and any advice would be greatly appreciated.
Im trying to run three ADXL345 sensors at once onto Arduino Yun to measure three different axis of acceleration. I understand that ADXL345 has 2 address for I2C connection which i can use. But how can I add the third onto it? I am trying to add the third one onto Arduino Yun using SPI settings but I just realise the SPI ports arent the same as the one on Arduino Uno.
Would really appreciate if someone experts on this! Thanks!
#include <Bridge.h>
#include <Console.h>
#include <FileIO.h>
#include <HttpClient.h>
#include <Mailbox.h>
#include <Process.h>
#include <YunClient.h>
#include <YunServer.h>
#include <Wire.h>
#define DEVICE (0x53) //ADXL345 device address
#define TO_READ (6) //num of bytes we are going to read each time (two bytes for each axis)
#define THRESH_ACT (0x24)
#define THRESH_INACT (0x25)
#include <SPI.h>
// Declare Variable
byte runComplete; //setting the number of iteration
int i; //delare i for count
byte buff[TO_READ];//6 bytes buffer for saving data read from the device
float voltage; //Analog input voltage from AD8495 thermocouple
float temp; //Temperature variable thermocoupple
float tem[10]; //Temperature variable thermocoupple
float x, y, z; //Acceleration variable
float x1, y1, z1; //Acceleration variable
float a,b,c;
float a1,b1,c1;
String dataString="";
String currentdata="";
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
char values[10]; //This buffer will hold values read from the ADXL345 registers.
int CS=10;
void writeTo(int device, 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
}
void readFrom(int device, byte address, int num, byte buff[]) { //reads num bytes starting from address register on device in to buff array
Wire.beginTransmission(device); //start transmission to device
Wire.write(address); //sends address to read from
Wire.endTransmission(); //end transmission
int n = Wire.requestFrom(device, num); // request 6 bytes from device
if( n == num)
{
Wire.readBytes(buff, n);
}
}
void regAddress()
{
int regAddress = 0x32; //first axis-acceleration-data register on the ADXL345
readFrom(DEVICE, regAddress, TO_READ, 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
x = (((int)buff[1]) << 8) | buff[0];
y = (((int)buff[3])<< 8) | buff[2];
z = (((int)buff[5]) << 8) | buff[4];
x1 = (x/256);
y1 = (y/256);
z1 = (z/256);
//delay(500); // make a string that start with a timestamp for assembling the data to log:
//String dataString;
//dataString += getTimeStamp();
//dataString += ",";
// currentdata=String(x)+","+ String(y)+"," + String(z); //String(getTimeStamp());
currentdata=String(x1)+","+ String(y1)+"," + String(z1); //String(getTimeStamp());
//dataString = dataString + currentdata; // separate the values with a comma
Console.println(currentdata);
}
void acc2()
{
//Reading 6 bytes of data starting at register DATAX0 will retrieve the x,y and z acceleration values from the ADXL345.
//The results of the read operation will get stored to the values[] buffer.
readRegister(DATAX0, 6, values);
//Set up the Chip Select pin to be an output from the Arduino.
pinMode(CS, OUTPUT);
//Before communication starts, the Chip Select pin needs to be set high.
digitalWrite(CS, HIGH);
//Put the ADXL345 into +/- 4G range by writing the value 0x01 to the DATA_FORMAT register.
writeRegister(DATA_FORMAT, 0x01);
//Put the ADXL345 into Measurement Mode by writing 0x08 to the POWER_CTL register.
writeRegister(POWER_CTL, 0x08); //Measurement mode
//delay(10000);
writeTo(DEVICE, 0x2D, 0); //Turning on the ADXL345
writeTo(DEVICE, 0x2D, 16); //Turning on the ADXL345
writeTo(DEVICE, 0x2D, 8); //Turning on the ADXL345
writeTo(DEVICE, 0x31, 8);
//Print the results to the terminal.
Console.print ("2 =");
Console.print(a/100);
Console.print(',');
Console.print(b/100);
Console.print(',');
Console.println(c/100);
// delay(10);
}
//This function will write a value to a register on the ADXL345.
//Parameters:
// char registerAddress - The register to write a value to
// char value - The value to be written to the specified register.
void writeRegister(char registerAddress, char value){
//Set Chip Select pin low to signal the beginning of an SPI packet.
digitalWrite(CS, LOW);
//Transfer the register address over SPI.
SPI.transfer(registerAddress);
//Transfer the desired register value over SPI.
SPI.transfer(value);
//Set the Chip Select pin high to signal the end of an SPI packet.
digitalWrite(CS, HIGH);
}
//This function will read a certain number of registers starting from a specified address and store their values in a buffer.
//Parameters:
// char registerAddress - The register addresse to start the read sequence from.
// int numBytes - The number of registers that should be read.
// char * values - A pointer to a buffer where the results of the operation should be stored.
void readRegister(char registerAddress, int numBytes, char * values){
//Since we're performing a read operation, the most significant bit of the register address should be set.
char address = 0x80 | registerAddress;
//If we're doing a multi-byte read, bit 6 needs to be set as well.
if(numBytes > 1)address = address | 0x40;
//Set the Chip select pin low to start an SPI packet.
digitalWrite(CS, LOW);
//Transfer the starting register address that needs to be read.
SPI.transfer(address);
//Continue to read registers until we've read the number specified, storing the results to the input buffer.
for(int i=0; i<numBytes; i++){
values[i] = SPI.transfer(0x00);
}
//Set the Chips Select pin high to end the SPI packet.
digitalWrite(CS, HIGH);
}
void setup()
{
Bridge.begin();
Console.begin(); // start serial for output
while (!Console) ; // stops the program until Serial Monitor is opened
Wire.begin(); // join i2c bus (address optional for master)
FileSystem.begin();
SPI.begin();
//Configure the SPI connection for the ADXL345.
SPI.setDataMode(SPI_MODE3);
//Create a serial connection to display the data on the terminal.
Console.println("Initializing Accelerometer...");
Console.print("Initializing SD card...");
Console.println(" ");
delay(2000);
}
void loop() {
if (runComplete == 0){
for (i=0; i<2500; i=i+1){
// Console.print("No. of i= "); Console.println(i);
// if(i<1000) Console.print(" Initialisation:");Console.print(initialtrue);
//Console.println("");
regAddress();
acc2();
}
runComplete = 1;
}
}