0

I was trying to use MPU6050 and a servo motor. but mpu6050 arduino crashing after a few seconds . i used arduino uno and sg90 servo motor. this is my code.

#include "Wire.h"
#include <MPU6050_light.h>
#include <Servo.h>
Servo myservo;
MPU6050 mpu(Wire);
unsigned long timer = 0;
int pos = 0; 
void setup() {
  myservo.attach(10);
  Serial.begin(115200);
  Wire.begin();
  byte status = mpu.begin();
  Serial.print(F("MPU6050 status: "));
  Serial.println(status);
  while(status!=0){ } // stop everything if could not connect to MPU6050
  Serial.println(F("Calculating offsets, do not move MPU6050"));
  delay(1000);
  // mpu.upsideDownMounting = true; // uncomment this line if the MPU6050 is mounted upside-down
  mpu.calcOffsets(); // gyro and accelero
  Serial.println("Done!\n");
}
void loop() {
  mpu.update();
  //Serial.println(mpu.getAngleX());
  int lastan = (mpu.getAngleX()-70)*-1;
  if ((lastan> 0) || (lastan< 180)){
  myservo.write(lastan); 
  timer = millis();
  }
}
simo
  • 9
  • 4

1 Answers1

0

From my view you have to check following line again:

if ((lastan> 0) || (lastan< 180))

Its always true because when lastan is less than 0 its also less than 180. Same applies to lastlan bigger than 180... Then first argument is true.

I assume you need to change this line to:

if ((lastan> 0) && (lastan< 180))