LSM303 heading readings of almost 380 degrees issue

i am using the LSM303 board from polo

I am using the lib. from the playground area for this board.
heres a bit of the code:

int LSM303DLH::heading(vector from)
{
	// shift and scale
    m.x = (m.x - m_min.x) / (m_max.x - m_min.x) * 2 - 1.0;
    m.y = (m.y - m_min.y) / (m_max.y - m_min.y) * 2 - 1.0;
    m.z = (m.z - m_min.z) / (m_max.z - m_min.z) * 2 - 1.0;

	vector temp_a = a;
    // normalize
    vector_normalize(&temp_a);
    //vector_normalize(&m);

    // compute E and N
    vector E;
    vector N;
    vector_cross(&m,&temp_a,&E);
    vector_normalize(&E);
    vector_cross(&temp_a,&E,&N);
	
    // compute heading
    int heading = round(atan2(vector_dot(&E,&from), vector_dot(&N,&from)) * 180/M_PI);
    if(heading < 0) heading += 360;
	return heading;
}

I am calibrating it to get the max and min values using this code:

void LSM303DLH::calibration(void)
	{

	int respond = 0;
	
	Serial.println("Hit any key when ready to calibrate.");
	//wait till R is pressed
	while ( respond == 0)
		{
		while (Serial.available()==0);; 


		while (Serial.available()>0)
			{
		 	respond = Serial.read();
 			}
		}
       

	Serial.println("Rotate board until Min and Max values are reched for all axis.");
        Serial.println("WHen calibration done press D...");
		 delay(3000);

	while (respond != 100) 	
	    {
	
                 while (Serial.available()==0)
                   {
                     
		 //Loop till D is pressed

		 read();
  	
   		if (m.x > m_max.x)
    	 	 {
     	 	  m_max.x = m.x;
   	 	 }
  		if (m.y > m_max.y)
    	 	 {
      	 	  m_max.y = m.y;
    	 	 }
   		if (m.z > m_max.z)
    		 {
    	 	  m_max.z = m.z;
    	 	 }
   		if (m.x < m_min.x)
    	 	 {
     	 	  m_min.x = m.x;
    	 	 }
    
    		if (m.y < m_min.y)
    	 	 {
      		  m_min.y = m.y;
    	 	 }
    
    		if (m.z < m_min.z)
    	 	 {
      		 m_min.z = m.z;
    	 	 }
		//Display results
  
  	 	
	 	Serial.print("Maxx: ");
  		Serial.print(m_max.x);
  		Serial.print(" MaxY: ");
  		Serial.print(m_max.y);
 		Serial.print(" MaxZ: ");
  	 	Serial.print(m_max.z);
   
  	 	Serial.print("Minx: ");
 	 	Serial.print(m_min.x);
  	 	Serial.print(" MinY: ");
  	 	Serial.print(m_min.y);
  	 	Serial.print(" MinZ:: ");
 	 	Serial.println(m_min.z);

	 
             }
               while (Serial.available()>0)
			{
		 	respond = Serial.read();
   			}
               
         }

	//D pressed -> end loop
	Serial.println("DONE");
	delay(2000);

	}

i am using a 10 as a sample rate for the readings at the moment

My issue is i get heading readings above 360 degrees…which doesn’t make sense.

any ideas?

well seems like if i do not sample the readings and take direct, the values are fine...soo possible to many samples or not dividing properly,

anyways its fixed

^

so can you tell me if you changed the sample readings? or did you just take them out?

what is your new code look like?