Hello!
I am trying to measure an ERM motor's vibrations simultaneously with two ADXL345 accelerometers.
The ADXL345 code works fine on its own, so does the ERM code. I am using a DRV2605L Sparkfun Haptic Driver for the ERM. The error I am having is that when I include the ERM function "waves" (basic Adafruit Library DRV2605 code for various wave types), the code does not display the accelerometer's readings anymore. I think this has to do with the I2C address and bus.
Thank you for your help!
#include<Wire.h>
#include "Adafruit_DRV2605.h"
Adafruit_DRV2605 drv;
#define adxl_1 (0x1d) // SDO-> Vcc
#define adxl_2 (0x53) // SDO-> GND
#define erm (0x5A)
byte values[6];
byte values2[6];
char output[512];
char output2[512];
float cal_1[2][3] = { // calibration matrix
{10.3,1.8,10.8}, // offset
{261,259,250} // gain
};
float cal_2[2][3] = { // calibration matrix
{11.4,-1.8,-1.3}, // offset
{256.8,260,251} // gain
};
void getAcceleration(int accelSensor, float cal[2][3])
{
int xyzregister = 0x32;
int x, y, z;
float x_cal,y_cal,z_cal;
Wire.beginTransmission(accelSensor);
Wire.write(0x2D);
Wire.write(0);
Wire.endTransmission();
Wire.beginTransmission(accelSensor);
Wire.write(0x2D);
Wire.write(16);
Wire.endTransmission();
Wire.beginTransmission(accelSensor);
Wire.write(0x2D);
Wire.write(8);
Wire.endTransmission();
Wire.beginTransmission(accelSensor);
Wire.write(xyzregister);
Wire.endTransmission();
Wire.beginTransmission(accelSensor);
Wire.requestFrom(accelSensor, 6);
int i = 0;
while(Wire.available())
{
values[i] = Wire.read();
i++;
}
Wire.endTransmission();
x = (((int)values[1]) << 8) | values[0];
y = (((int)values[3])<< 8) | values[2];
z = (((int)values[5]) << 8) | values[4];
// sprintf(output, " %d, %d, %d", x, y, z);
//**************** Calibration *******************//
x_cal = (x - cal[0][0]) / (float)cal[1][0];
y_cal = (y - cal[0][1]) / (float)cal[1][1];
z_cal = (z - cal[0][2]) / (float)cal[1][2];
// sprintf(output, "X: %.2f, Y: %.2f, Z: %.2f", x_cal, y_cal, z_cal); // Format for X, Y, Z values with two decimal places
// convert calibrated values to strings to print as decimal
char x_cal_str[10], y_cal_str[10], z_cal_str[10];
dtostrf(x_cal, 5, 2, x_cal_str);
dtostrf(y_cal, 5, 2, y_cal_str);
dtostrf(z_cal, 5, 2, z_cal_str);
// Concatenate the strings into the output string
sprintf(output, "X: %s, Y: %s, Z: %s", x_cal_str, y_cal_str, z_cal_str);
Serial.print(output);
}
void setup()
{
Wire.begin();
Serial.begin(9600);
drv.begin();
drv.selectLibrary(1);
drv.setMode(DRV2605_MODE_INTTRIG);
}
void loop()
{
getAcceleration(adxl_1, cal_1); // ********accel 0x1D*********//
Serial.print("\t");
getAcceleration(adxl_2, cal_2); //*******accel 0x53********//
Serial.println();
waves();
}
void waves() {
static uint8_t effect = 1;
// Serial.print("Effect #"); Serial.println(effect);
if (effect == 1) {
Serial.println("11.2 Waveform Library Effects List");
}
if (effect == 1) {
Serial.println(F("1 − Strong Click - 100%"));
}
if (effect == 2) {
Serial.println(F("2 − Strong Click - 60%"));
}
/*
...
*/
}
if (effect == 123) {
Serial.println(F("123 − Smooth Hum 5 (No kick or brake pulse) – 10%"));
}
// set the effect to play
drv.setWaveform(0, effect); // play effect
drv.setWaveform(1, 0); // end waveform
// play the effect!
drv.go();
effect++;
if (effect > 117) effect = 1;
}