Arduino Forum

Using Arduino => Programming Questions => Topic started by: mrsin3x on Mar 25, 2019, 04:25 pm

Title: Integrating Sweep Sketch and Accelerometer
Post by: mrsin3x on Mar 25, 2019, 04:25 pm
Greetings. I'm trying to achieve something very simple, however, I'm really struggling. I have a sketch which reads accelerometer values. However, once a threshold value is exceeded, I'd like to the motor to go into sweep mode for 5 seconds then return to a state of reading values. The accelerometer code is working, just need to properly integrate the servo sweep code with the relevant conditional statements or anything else.  Assistance would be greatly appreciated.

#include <Servo.h>
Servo myservo;
#include<Wire.h>
const int MPU_addr=0x68;  // I2C address of the MPU-6050
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
int pos = 0;
void setup(){
  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);
  myservo.attach(9);
 
}
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("AcX = "); Serial.print(AcX);
  Serial.print(" | AcY = "); Serial.print(AcY);
  Serial.print(" | AcZ = "); Serial.print(AcZ);
  Serial.print(" | Tmp = "); Serial.print(Tmp/340.00+36.53); 
  Serial.print(" | GyX = "); Serial.print(GyX);
  Serial.print(" | GyY = "); Serial.print(GyY);
  Serial.print(" | GyZ = "); Serial.println(GyZ);
  if (AcZ > 30000){
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }}else{
 AcZ=Wire.read()<<8|Wire.read();  // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
 }
  delay(1000);
}
Title: Re: Integrating Sweep Sketch and Accelerometer
Post by: PaulS on Mar 25, 2019, 04:31 pm
Why do you have anything in the else block after the if(AcZ > 30000) statement? You requested 14 values, and then, if AcZ is less than 30000, you read 1ll 16 of them. That doesn't make sense.
Title: Re: Integrating Sweep Sketch and Accelerometer
Post by: mrsin3x on Mar 25, 2019, 06:04 pm
Well that isn't an issue. The issue lies in using that one value to achieve the sweep.
Title: Re: Integrating Sweep Sketch and Accelerometer
Post by: PaulS on Mar 25, 2019, 06:33 pm
Quote
Well that isn't an issue.
Nonsense.

Quote
The issue lies in using that one value to achieve the sweep.
Why is that a problem? Do you KNOW that the Z value exceeds that value when you want the sweep to occur?