Hello everyone.
// Using gyro IXZ500 and Arduino UNO //
Here is my problem with this sensor. While I read a values from serial monitor, gyroscope automatically increases angle values, like its starts from zero and goes till 360 deg (as keep angle between -360 and 360) and repeat growing again. Otherwise, sensor read the values while i moving it. For example, it is growing and the value is 200, after i move it it changes to 30 deg, and then again growing from this value.
I dunno what to do for know cause i tried 2 different codes and its still growing. cannot make a screenshot for now, but will upload it tomorrow.
The connection - 5v VCC; x-out - analog pin 0, gnd - gnd.
The code:
float gyroVoltage = 5;
float gyroZeroVoltage = 1.35;
float gyroSensitivity = 0.002;
float rotationThreshold = 1;
float readError = (gyroVoltage/1023)*2;
float currentAngle = 0;
unsigned long time;
void setup() {
Serial.begin(9600);
time = micros();
}
unsigned long time2;
long loopNumber = 0;
void loop() {
time2 = micros();
loopNumber++;
int gyroValue = analogRead(0);
float gyroActualVoltage = (gyroValue * gyroVoltage) / 1023;
float gyroActualZero = gyroActualVoltage - gyroZeroVoltage;
if (gyroActualZero <= readError && gyroActualZero >= -readError) {
gyroActualZero = 0;
}
float gyroRate = gyroActualZero / gyroSensitivity;
float gyroRateTime = 0;
if (gyroRate >= rotationThreshold || gyroRate <= -rotationThreshold) {
gyroRateTime = gyroRate/( 1000000.0/(time2-time) );
currentAngle += gyroRateTime;
}
if (currentAngle < 0)
currentAngle += 360;
else if (currentAngle > 359)
currentAngle -= 360;
if (time2-time > 1000000L) {
Serial.println(currentAngle, 10);
loopNumber = 0;
time = micros();
}
delay (100);
}