while循环开始后程序停止
Program stops after while loop begins
我正在尝试根据陀螺仪的数据移动舵机。不幸的是,我的程序在 while 循环开始后停止了。例如,在我的代码中,一旦 gyroX 值达到 3000,串行监视器就会停止向我提供陀螺仪数据,而伺服系统不会 运行。任何有关如何解决此问题的建议将不胜感激。
#include <Wire.h>
#include <Servo.h>
Servo servo;
long accelX, accelY, accelZ;
float gForceX, gForceY, gForceZ;
long gyroX, gyroY, gyroZ;
float rotX, rotY, rotZ;
int pos1 = 90;
void setup() {
Serial.begin(9600);
Wire.begin();
setupMPU();
}
void loop() {
recordAccelRegisters();
recordGyroRegisters();
printData();
while( gyroX > 3000 ){ //problem happens here
pos1 += 5;
servo.write(pos1);
}
}
void setupMPU(){
Wire.beginTransmission(0b1101000);
Wire.write(0x6B);
Wire.write(0b00000000);
Wire.endTransmission();
Wire.beginTransmission(0b1101000);
Wire.write(0x1B);
Wire.write(0x00000000);
Wire.endTransmission();
Wire.beginTransmission(0b1101000);
Wire.write(0x1C);
Wire.write(0b00000000);
Wire.endTransmission();
}
void recordGyroRegisters() {
Wire.beginTransmission(0b1101000);
Wire.write(0x3B);
Wire.endTransmission();
Wire.requestFrom(0b1101000,6);
while(Wire.available() < 6);
gyroX = Wire.read()<<8|Wire.read();
gyroY = Wire.read()<<8|Wire.read();
gyroZ = Wire.read()<<8|Wire.read();
}
void recordAccelRegisters() {
Wire.beginTransmission(0b1101000);
Wire.write(0x43);
Wire.endTransmission();
Wire.requestFrom(0b1101000,6);
while(Wire.available() < 6);
accelX = Wire.read()<<8|Wire.read();
accelY = Wire.read()<<8|Wire.read();
accelZ = Wire.read()<<8|Wire.read();
}
void printData() {
Serial.print("Gyro (deg)");
Serial.print(" X=");
Serial.print(gyroX);
Serial.print(" Y=");
Serial.print(gyroY);
Serial.print(" Z=");
Serial.print(gyroZ);
Serial.print(" Accel (g)");
Serial.print(" X=");
Serial.print(accelX);
Serial.print(" Y=");
Serial.print(accelY);
Serial.print(" Z=");
Serial.println(accelZ);
}
在那个 while 循环中没有可以改变 gyroX 值的命令。所以一旦你在里面,它就变成了一个无限循环。您需要在 while 循环内调用 recordGyroRegisters 函数,以便在循环期间更新变量。或者您可以利用 loop 函数已经在循环这一事实,简单地摆脱 for 循环并选择基于 if 语句的东西。
我正在尝试根据陀螺仪的数据移动舵机。不幸的是,我的程序在 while 循环开始后停止了。例如,在我的代码中,一旦 gyroX 值达到 3000,串行监视器就会停止向我提供陀螺仪数据,而伺服系统不会 运行。任何有关如何解决此问题的建议将不胜感激。
#include <Wire.h>
#include <Servo.h>
Servo servo;
long accelX, accelY, accelZ;
float gForceX, gForceY, gForceZ;
long gyroX, gyroY, gyroZ;
float rotX, rotY, rotZ;
int pos1 = 90;
void setup() {
Serial.begin(9600);
Wire.begin();
setupMPU();
}
void loop() {
recordAccelRegisters();
recordGyroRegisters();
printData();
while( gyroX > 3000 ){ //problem happens here
pos1 += 5;
servo.write(pos1);
}
}
void setupMPU(){
Wire.beginTransmission(0b1101000);
Wire.write(0x6B);
Wire.write(0b00000000);
Wire.endTransmission();
Wire.beginTransmission(0b1101000);
Wire.write(0x1B);
Wire.write(0x00000000);
Wire.endTransmission();
Wire.beginTransmission(0b1101000);
Wire.write(0x1C);
Wire.write(0b00000000);
Wire.endTransmission();
}
void recordGyroRegisters() {
Wire.beginTransmission(0b1101000);
Wire.write(0x3B);
Wire.endTransmission();
Wire.requestFrom(0b1101000,6);
while(Wire.available() < 6);
gyroX = Wire.read()<<8|Wire.read();
gyroY = Wire.read()<<8|Wire.read();
gyroZ = Wire.read()<<8|Wire.read();
}
void recordAccelRegisters() {
Wire.beginTransmission(0b1101000);
Wire.write(0x43);
Wire.endTransmission();
Wire.requestFrom(0b1101000,6);
while(Wire.available() < 6);
accelX = Wire.read()<<8|Wire.read();
accelY = Wire.read()<<8|Wire.read();
accelZ = Wire.read()<<8|Wire.read();
}
void printData() {
Serial.print("Gyro (deg)");
Serial.print(" X=");
Serial.print(gyroX);
Serial.print(" Y=");
Serial.print(gyroY);
Serial.print(" Z=");
Serial.print(gyroZ);
Serial.print(" Accel (g)");
Serial.print(" X=");
Serial.print(accelX);
Serial.print(" Y=");
Serial.print(accelY);
Serial.print(" Z=");
Serial.println(accelZ);
}
在那个 while 循环中没有可以改变 gyroX 值的命令。所以一旦你在里面,它就变成了一个无限循环。您需要在 while 循环内调用 recordGyroRegisters 函数,以便在循环期间更新变量。或者您可以利用 loop 函数已经在循环这一事实,简单地摆脱 for 循环并选择基于 if 语句的东西。