簡體   English   中英

mpu6050 arduino uno 幾秒鍾后停止

[英]mpu6050 arduino uno stoping after a few seconds

我試圖使用 MPU6050 和伺服電機。 但 mpu6050 arduino 幾秒鍾后崩潰。 我使用了 arduino uno 和 sg90 伺服電機。 這是我的代碼。

#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();
  }
}

在我看來,您必須再次檢查以下行:

如果((拉斯坦> 0)||(拉斯坦< 180))

它總是正確的,因為當 lastan 小於 0 時它也小於 180。這同樣適用於大於 180 的 lastlan ... 那么第一個參數為真。

我假設您需要將此行更改為:

如果((拉斯坦> 0)&&(拉斯坦< 180))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM