I'm trying to calibrate the LSM303 tilt-compensated compass with a button, and then use the calibrated values to grab the compass heading.
Now the problem is, when I add the line of code, which essentially grabs the heading, everything just messes up.
With this it does not calibrate, nor give a heading ( all is 0)
#include <Wire.h>
#include <LSM303.h>
int egoal;
int ogoal;
LSM303 compass;
const int buttonPin = 33;
const int ledPin = 31;
int buttonState;
LSM303::vector running_min = {2047, 2047, 2047}, running_max = {-2048, -2048, -2048};
void setup() {
Serial.begin(9600);
pinMode(19, INPUT);
pinMode(buttonPin, OUTPUT);
pinMode(ledPin, OUTPUT);
Wire.begin();
compass.init();
compass.enableDefault();
attachInterrupt(4, calibrateCompass, LOW); //when button has been pushed,
}
void work() {
compass.read();
int headingz = compass.heading((LSM303::vector){0,-1,0});
Serial.println(headingz);
}
void calibrateCompass() {
compass.m_min.x = (running_min.x = min(running_min.x, compass.m.x));
compass.m_min.y = (running_min.y = min(running_min.y, compass.m.y));
compass.m_min.z = (running_min.z = min(running_min.z, compass.m.z));
compass.m_max.x = (running_max.x = max(running_max.x, compass.m.x));
compass.m_max.y = (running_max.y = max(running_max.y, compass.m.y));
compass.m_max.z = (running_max.z = max(running_max.z, compass.m.z));
digitalWrite(ledPin, HIGH);
}
void loop() {
digitalWrite(buttonPin, HIGH);
buttonState = digitalRead(19);
digitalWrite(ledPin, LOW);
work();
Serial.println(compass.m_min.x);// This is just to see if the values have been calibrated after using the pushbutton
Serial.println(compass.m_min.y);
Serial.println(compass.m_min.z);
Serial.println("----------------------------");
//
delay(250);
}
with this, (note commented lines in void work):
#include <Wire.h>
#include <LSM303.h>
int egoal;
int ogoal;
LSM303 compass;
const int buttonPin = 33;
const int ledPin = 31;
int buttonState;
LSM303::vector running_min = {2047, 2047, 2047}, running_max = {-2048, -2048, -2048};
void setup() {
Serial.begin(9600);
pinMode(19, INPUT);
pinMode(buttonPin, OUTPUT);
pinMode(ledPin, OUTPUT);
Wire.begin();
compass.init();
compass.enableDefault();
attachInterrupt(4, calibrateCompass, LOW); //when button has been pushed,
}
void work() {
compass.read();
//int headingz = compass.heading((LSM303::vector){0,-1,0});
//Serial.println(headingz);
}
void calibrateCompass() {
compass.m_min.x = (running_min.x = min(running_min.x, compass.m.x));
compass.m_min.y = (running_min.y = min(running_min.y, compass.m.y));
compass.m_min.z = (running_min.z = min(running_min.z, compass.m.z));
compass.m_max.x = (running_max.x = max(running_max.x, compass.m.x));
compass.m_max.y = (running_max.y = max(running_max.y, compass.m.y));
compass.m_max.z = (running_max.z = max(running_max.z, compass.m.z));
digitalWrite(ledPin, HIGH);
}
void loop() {
digitalWrite(buttonPin, HIGH);
buttonState = digitalRead(19);
digitalWrite(ledPin, LOW);
work();
Serial.println(compass.m_min.x);// This is just to see if the values have been calibrated after using the pushbutton
Serial.println(compass.m_min.y);
Serial.println(compass.m_min.z);
Serial.println("----------------------------");
//
delay(250);
}
IT gives the calibrated values but not the heading. And on the pololu specifications it says the compass can be calibrated on the go or at the execution of a function.
What's wrong??? =( =( =( =( =( =( =(