Hello,

I'm connecting Adafruit's Triple-axis Accelerometer+Magnetometer (Compass) Board ( LSM303 ) to Teensy 3.0 via the I2C protocol.

I've wired up 4.7 K resistors to the 3.3V pins and connected SCL and SDA pins 19 and 18 respectively.

It works but.....

The problem is the CC messages are sending out only one value and repeating that value.

However when I recompile the same sketch or press the reset button, the values are updated and it is working properly.

I unplug / replug the USB cable, and CC messages are stuck again! It only gets unstuck when I recompile the code or press the reset button.

The CC values are printing out but not updating. It is basically getting stuck on one value. Any suggestions? See code.



#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_LSM303.h>

/* Assign a unique ID to this sensor at the same time */
Adafruit_LSM303_Mag mag = Adafruit_LSM303_Mag(12345);

int current_magneto;
int previous_magneto;



void setup(void)
{

/* Initialise the sensor */
if(!mag.begin())
{
/* There was a problem detecting the LSM303 ... check your connections */
Serial.println("Ooops, no LSM303 detected ... Check your wiring!");
while(1);
}
}

void loop(void)
{
/* Get a new sensor event */
sensors_event_t event;
mag.getEvent(&event);

float Pi = 3.14159;

// Calculate the angle of the vector y,x
int current_magneto = (atan2(event.magnetic.y,event.magnetic.x) * 180) / Pi;

// Normalize to 0-360

if (current_magneto < 0)

{
current_magneto = 360 + current_magneto;
}

if (current_magneto != previous_magneto)
{
usbMIDI.sendControlChange(1, current_magneto / 2.8, 1);
}
current_magneto= previous_magneto;
delay(50);

}