Hi
This is the sensor I'm using.
Here is the code (taken from the kas' balancing robots for dummies)
// Main module K_bot angles in Quids, 10 bit ADC -------------
// 4 - Checking sensor data format display raw sensors data
#include <math.h>
#define GYR_Y 0 // Gyro Y (IMU pin #4)
#define ACC_Z 1 // Acc Z (IMU pin #7)
#define ACC_X 2 // Acc X (IMU pin #9)
int STD_LOOP_TIME = 9;
int sensorValue[3] = { 0, 0, 0};
int sensorZero[3] = { 0, 0, 0};
int lastLoopTime = STD_LOOP_TIME;
int lastLoopUsefulTime = STD_LOOP_TIME;
unsigned long loopStartTime = 0;
void setup() {
analogReference(EXTERNAL); // Aref 3.3V
Serial.begin(115200);
delay(100);
calibrateSensors();
}
void loop() {
// ********************* Sensor aquisition & filtering *******************
updateSensors();
// ********************* print Debug info *************************************
serialOut_raw();
// *********************** loop timing control **************************
lastLoopUsefulTime = millis()-loopStartTime;
if(lastLoopUsefulTime<STD_LOOP_TIME) delay(STD_LOOP_TIME-lastLoopUsefulTime);
lastLoopTime = millis() - loopStartTime;
loopStartTime = millis();
}
void serialOut_raw() {
static int skip=0;
if(skip++==40) {
skip = 0;
Serial.print("ACC_X:"); Serial.print(sensorValue[ACC_X]);
Serial.print(" ACC_Z:"); Serial.print(sensorValue[ACC_Z]);
Serial.print(" GYR_Y:"); Serial.println(sensorValue[GYR_Y]);
}
}
// Sensors Module ---------------------------------------------------------------------
void calibrateSensors() { // Set zero sensor values
long v;
for(int n=0; n<3; n++) {
v = 0;
for(int i=0; i<50; i++) v += readSensor(n);
sensorZero[n] = v/50;
}
sensorZero[ACC_Z] -= 103;
}
void updateSensors() { // data acquisition
long v;
for(int n=0; n<3; n++) {
v = 0;
for(int i=0; i<5; i++) v += readSensor(n);
sensorValue[n] = v/5 - sensorZero[n];
}
}
int readSensor(int channel){
return (analogRead(channel));
}
I got the sensor about three weeks ago, I'm planning on building a balancing robot to help me learn more. Problem is when I first got the board I was excited, quickly soldered everything together and used this code to test it out, it worked perfectly.
Right side up and level: X = 0, Z = 104, Y = 0
Upside down X = 0, Z = - 104, Y = 0
90 degrees to the right X = -104, Z = 0 Y = 4 etc.
This was pretty much what the post said to expect. I've had exams for the past three weeks, and finally thought tonight would be the night I could get a good start on my project and bam, guess what? No such luck. Using The EXACT same code and the EXACT same wiring I no longer get the same readings, if any. The readings now remain constant:
X= 0, Z= 104, Y = 0
Sometimes if I upload the sketch to the board again the values change, but were X should be - 104, X = -36 or similar.
Sometimes movement makes the values jump e.g Z = 247.
My initial thought a damaged wire/s ?(Someone else used the board)
I have tried different sketches. The problem sticks.
Any ideas? (The is no way I'll be able to afford another board soon, the are really expensive in my country)