How to get MPU6050 readings while actuator moves

Hi. I'm currently trying to make MPU6050 print readings all the time and when the actuator moves. Right now, the MPU6050 stops reading when the actuator moves. The MPU6050 only takes reading when the actuator stop moving. Which part of the codings that I need to adjust? Please help.

const int In1 = 7, In2 = 8, ENA = 9, SPEED = 255, button = 4;
boolean readjust = true;
int val = 0;
int old_val = 0;
int state = 0;
#include<Wire.h>
const int MPU_addr=0x68;  // I2C address of the MPU-6050
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
 
 
void setup(){
  pinMode (In1, OUTPUT);
  pinMode (In2, OUTPUT);
  pinMode (ENA, OUTPUT);
  pinMode (button, INPUT);
  Wire.begin();
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x6B);  // PWR_MGMT_1 register
  Wire.write(0);     // set to zero (wakes up the MPU-6050)
  Wire.endTransmission(true);
  Serial.begin(9600);
  //PLXDAQ readings
  Serial.println("CLEARDATA");
  Serial.println("LABEL,Time,AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ");

 
}
void loop(){
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
  Wire.endTransmission(false);
  Wire.requestFrom(MPU_addr,14,true);  // request a total of 14 registers
  AcX=Wire.read()<<8|Wire.read();  // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)   
  AcY=Wire.read()<<8|Wire.read();  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
  AcZ=Wire.read()<<8|Wire.read();  // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
  Tmp=Wire.read()<<8|Wire.read();  // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
  GyX=Wire.read()<<8|Wire.read();  // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
  GyY=Wire.read()<<8|Wire.read();  // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
  GyZ=Wire.read()<<8|Wire.read();  // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
  Serial.print("DATA,TIME,");
  //Serial.print("AcX = "); 
  Serial.print(AcX);
  Serial.print(",");
  //Serial.print(" | AcY = "); 
  Serial.print(AcY);
  Serial.print(",");
  //Serial.print(" | AcZ = "); 
  Serial.print(AcZ);
  Serial.print(",");
  //Serial.print(" | Tmp = "); 
  Serial.print(Tmp/340.00+36.53);  //equation for temperature in degrees C from datasheet
  Serial.print(",");
 
 // Serial.print(" | GyX = ");
 // Serial.print(GyX);
 // Serial.print(" | GyY = ");
  //Serial.print(GyY);
  //Serial.print(" | GyZ = ");
  //Serial.println(GyZ);
 

  Serial.print(GyX);
  Serial.print(",");
  Serial.print(GyY);
  Serial.print(",");
  Serial.println(GyZ);
 
 if (readjust == true){
  Serial.print ("start adjust");
  Serial.print ('\n');
  analogWrite(ENA, SPEED);
  digitalWrite(In1, HIGH);
  digitalWrite(In2, LOW);  
  delay (6000);

  analogWrite(ENA, 0);
  Serial.print ("done adjust");
  Serial.print ('\n');
 }

 readjust = false;

 val = digitalRead(button);


 if((val == HIGH) && (old_val == LOW || HIGH)){

  Serial.print ("on/off");
  Serial.print ('\n');
  delay (3000);
  state = 1 - state;
  delay(50);
 }
 old_val = val;


 if (state == 1){

  Serial.print ("begin cycle");
  Serial.print ('\n');
  analogWrite(ENA, SPEED);
  digitalWrite(In1, LOW);
  digitalWrite(In2, HIGH);
  delay (6000);

  analogWrite(ENA, 0);
  delay (500);

  analogWrite(ENA, SPEED);
  digitalWrite(In1, HIGH);
  digitalWrite(In2, LOW); 
  delay (6000); 

  analogWrite(ENA, 0);
  delay (500);
  Serial.print ("complete 1 cycle");
  Serial.print ('\n');

 }
  delay(10);
}

If you want a responsive program you should not use delay(). The functions delay() and delayMicroseconds() block the Arduino until they complete.

Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

Also note how each function runs very briefly and returns to loop() so the next one can be called. None of the functions tries to complete a task in one call. And there may be dozens of calls to a function before it is actually time for it to do anything.

And see Using millis() for timing. A beginners guide if you need more explanation.

...R

I tried using millis here. It seems the actuator doesn't move. Can you help to see which part I need to adjust?

const int In1 = 7, In2 = 8, ENA = 9, SPEED = 255, button = 4;
boolean readjust = true;
int val = 0;
int old_val = 0;
int state = 0;
#include<Wire.h>
const int MPU_addr=0x68;  // I2C address of the MPU-6050
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;

 //To Do: Variables for Timed Events
const unsigned long eventTime_1_IMU_Sensor = 10;
unsigned long currentDurationMotor;  
const unsigned long fwdDuration = 6000;   
const unsigned long pauseDuration = 500;  
const unsigned long pauseDuration2 = 3000;
const unsigned long pauseDuration3 = 50;
const unsigned long bwdDuration = 6000;   

unsigned long previousTime_1 = 0;
unsigned long previousTime_2 = 0;
 
void setup(){
  pinMode (In1, OUTPUT);
  pinMode (In2, OUTPUT);
  pinMode (ENA, OUTPUT);
  pinMode (button, INPUT);
  Wire.begin();
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x6B);  // PWR_MGMT_1 register
  Wire.write(0);     // set to zero (wakes up the MPU-6050)
  Wire.endTransmission(true);
  Serial.begin(9600);
  //PLXDAQ readings
  Serial.println("CLEARDATA");
  Serial.println("LABEL,Time,AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ");

 
}
void loop(){
  unsigned long currentTime = millis();

  if (currentTime - previousTime_1 >= eventTime_1_IMU_Sensor){
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
  Wire.endTransmission(false);
  Wire.requestFrom(MPU_addr,14,true);  // request a total of 14 registers
  AcX=Wire.read()<<8|Wire.read();  // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)   
  AcY=Wire.read()<<8|Wire.read();  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
  AcZ=Wire.read()<<8|Wire.read();  // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
  Tmp=Wire.read()<<8|Wire.read();  // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
  GyX=Wire.read()<<8|Wire.read();  // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
  GyY=Wire.read()<<8|Wire.read();  // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
  GyZ=Wire.read()<<8|Wire.read();  // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
  Serial.print("DATA,TIME,");
  //Serial.print("AcX = "); 
  Serial.print(AcX);
  Serial.print(",");
  //Serial.print(" | AcY = "); 
  Serial.print(AcY);
  Serial.print(",");
  //Serial.print(" | AcZ = "); 
  Serial.print(AcZ);
  Serial.print(",");
  //Serial.print(" | Tmp = "); 
  Serial.print(Tmp/340.00+36.53);  //equation for temperature in degrees C from datasheet
  Serial.print(",");
 
 // Serial.print(" | GyX = ");
 // Serial.print(GyX);
 // Serial.print(" | GyY = ");
  //Serial.print(GyY);
  //Serial.print(" | GyZ = ");
  //Serial.println(GyZ);
 

  Serial.print(GyX);
  Serial.print(",");
  Serial.print(GyY);
  Serial.print(",");
  Serial.println(GyZ);

  previousTime_1 = currentTime;
  }
 if (currentTime - previousTime_2 >= currentDurationMotor){  
 if (readjust == true){
  Serial.print ("start adjust");
  Serial.print ('\n');
  analogWrite(ENA, SPEED);
  digitalWrite(In1, HIGH);
  digitalWrite(In2, LOW);  
  //delay (6000);
  currentDurationMotor = fwdDuration;

  analogWrite(ENA, 0);
  Serial.print ("done adjust");
  Serial.print ('\n');
 }

 readjust = false;

 val = digitalRead(button);


 if((val == HIGH) && (old_val == LOW || HIGH)){

  Serial.print ("on/off");
  Serial.print ('\n');
  //delay (3000);
  currentDurationMotor = pauseDuration2;
  state = 1 - state;
  //delay(50);
  currentDurationMotor = pauseDuration3;
 }
 old_val = val;


 if (state == 1){

  Serial.print ("begin cycle");
  Serial.print ('\n');
  analogWrite(ENA, SPEED);
  digitalWrite(In1, LOW);
  digitalWrite(In2, HIGH);
  //delay (6000);
  currentDurationMotor = bwdDuration;

  analogWrite(ENA, 0);
  //delay (500);
  currentDurationMotor = pauseDuration;

  analogWrite(ENA, SPEED);
  digitalWrite(In1, HIGH);
  digitalWrite(In2, LOW); 
  //delay (6000); 
  currentDurationMotor = fwdDuration;

  analogWrite(ENA, 0);
  //delay (500);
  currentDurationMotor = pauseDuration;
  Serial.print ("complete 1 cycle");
  Serial.print ('\n');

 }
  previousTime_2 = currentTime;
}
}

I can't see what's wrong, but I'm not good at spotting mistake without being able to run a program.

You should use the AutoFormat tool to indent your code to make it much easier to read.

You should also break the program into several short single-purpose functions that are called from loop() - something like this

void loop() {
   currentMillis = millis();
   read6050();
   checkButtons();
   adjustSomething();
   controlMotor();
}

that way you can test the different parts separately and more easily identify if one part interferes with another previously working part.

Have a look at Planning and Implementing a Program

You seem to be reading the MPU every 10 millisecs. Don't print that often - printing slows the Arduino and humans can't read that fast.

...R

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