Simultaneous/Non-blocking behaviour ERM Motor and ADXL345 using I2C

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;

}

There's quite a bit of printing. What does print out, and when does it stop?

Or do you mean you get no printing?

Add printing to the setup() function and see if you can tell where it goes sideways.

Also, you might want to try using an I2C scanner sketch, variously with and without either or both things connected.

Also, you may benefit from using an AXDL345 library. I like to write my own code, but sometimes there's a little pseky detail a library author got right...

HTH

a7

Does each device have a different I2C-adress?
If yes this is not the problem.

The Wire.beginTransmission() and the Wire.endTransmission() should only be used when sending data over the I2C bus.
The Wire.requestFrom() should not be used with those functions.

Which Arduino board do you use ?

The printing does the following: a) prints 6 accelerometer readings (3 xyz per accelerometer) b) prints the current waveform for the ERM (code runs through 127 different waveforms).
Essentially, what I want to get from this code is to view the response of the accelerometers to the 127 different waveforms, in order to then select one of them, appropriate for my application.

I was aware of the I2C Scanner and used it, it outputing 0x1D and 0x53 for the two ADXL345, and 0x5A for the ERM motor.

I previously tried using the Sparkfun ADXL345 Library, but couldn't get it to work for two accelerometers simultaneously.

Yes.
From the I2C Scanner, 0x1D and 0x53 for the two ADXL345, and 0x5A for the ERM motor.

I reference both the accelerometers' I2C addresses in the getAcceleration function, so was wondering if I should reference the ERM motor's address somewhere else as well.

I am using an Arduino Nano board, ATMEGA328.

Please reduce the size of the buffers. Two buffers of 512 byte is a lot for the classic Arduino Nano.
I suggest to avoid sprintf() and just use Serial.print() and remove both buffers.

Solution was to remove all printing from the wave function... Thank you!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.