For what it's worth, the full code listing is provided at the bottom of this post.
When I compile it I get the following:
Sketch uses 5582 bytes (17%) of program storage space. Maximum is 32256 bytes.
Global variables use 450 bytes (21%) of dynamic memory, leaving 1598 bytes for local variables. Maximum is 2048 bytes.
It doesn't appear that lack of dynamic memory is the problem given that it only takes up 21%. However I still tried enclosing the quoted text in the F() macro as suggested by @david_2018 but alas it doesn't seem to resolve it.
The following is a small excerpt of the serial monitor when using this code:
Pitch: -1.06 Roll: -1.47 Yaw: 0.23
Pitch: -1.09 Roll: -1.40 Yaw: 0.22
Pitch: -123.51 Roll: -46.56 Yaw: -156.27
Pitch: -1.11 Roll: -1.45 Yaw: 0.17
Pitch: -0.98 Roll: -1.47 Yaw: 0.20
Pitch: -1.14 Roll: -1.46 Yaw: 0.19
Pitch: -1.14 Roll: -1.44 Yaw: 0.17
Pitch: -1.11 Roll: -1.40 Yaw: 0.18
Pitch: -123.51 Roll: -46.56 Yaw: -156.27
Pitch: -1.05 Roll: -1.40 Yaw: 0.21
Pitch: -1.06 Roll: -1.37 Yaw: 0.23
I know from a separate application that the correct readings are :
Pitch: -1.1 ish,
Roll: -1.45 ish, &
Yaw: 0.2.
You can see that occasionally a grossly incorrect reading of "Pitch: -123.51 Roll: -46.56 Yaw: -156.27" is provided.
Noting that it could also be an issue with the sensor, or an electrical problem, grounding, etc, but is there anything in the code that is a bit sus and could explain this issue?
FULL CODE LISTING
#include <Wire.h>
int IMU_Address = 104; //decimal conversion of B1101000
int cal_int;
int8_t X0, X2, X4 = 0;
uint8_t X1, X3, X5 = 0;
byte gyro_mask = B00000000;
float gyro_pitch, gyro_roll, gyro_yaw = 0;
float gyro_pitch_cal, gyro_roll_cal, gyro_yaw_cal = 0;
#define GYRO_CONFIG 0x1B //Hex address of gyro config register
#define GYRO_XOUT_H 0x43 //Starting register of gyro readings
#define XG_OFFSET_H 0x13
void setup() {
Serial.begin(9600);
Wire.begin();
Wire.beginTransmission(IMU_Address);
Wire.write(GYRO_CONFIG);
Wire.write(gyro_mask);
Wire.endTransmission();
}
void loop() {
read_gyro();
gyro_pitch /= 131;
gyro_roll /= 131;
gyro_yaw /= 131;
Serial.print("Pitch: ");
Serial.print(gyro_pitch);
Serial.print("\t");
Serial.print("Roll: ");
Serial.print(gyro_roll);
Serial.print("\t");
Serial.print("Yaw: ");
Serial.println(gyro_yaw);
delay(100);
}
void read_gyro(){
Wire.beginTransmission(IMU_Address);
Wire.write(GYRO_XOUT_H);
Wire.endTransmission();
Wire.requestFrom(IMU_Address, 6);
if(Wire.available() <= 6)
{
X0 = Wire.read();
X1 = Wire.read();
X2 = Wire.read();
X3 = Wire.read();
X4 = Wire.read();
X5 = Wire.read();
}
gyro_pitch = X0 << 8 | X1;
gyro_roll = X2 << 8 | X3;
gyro_yaw = X4 << 8 | X5;
}