   motors.setLeftSpeed(-SPEED*adjust - 100);
 }
 
-// Heading gives you degrees starting clockwise away from the from vector v
+// Converts x and y components of a vector to a heading in degrees
 int heading(LSM303::vector v)
 {
-  int x = (int)v.x;
-  int y = (int)v.y;
-  float xScaled =  2.0*(float)(x - compass.m_min.x) / ( compass.m_max.x - compass.m_min.x) - 1.0;
-  float yScaled =  2.0*(float)(y -  compass.m_min.y) / (compass.m_max.y - compass.m_min.y) - 1.0;
+  float xScaled =  2.0*(float)(v.x - compass.m_min.x) / ( compass.m_max.x - compass.m_min.x) - 1.0;
+  float yScaled =  2.0*(float)(v.y -  compass.m_min.y) / (compass.m_max.y - compass.m_min.y) - 1.0;
   
   int angle = round(atan2(yScaled, xScaled)*180 / M_PI);
   if (angle < 0)


// The closer we get to our driving angle, the slower we will turn.
// We do not want to overshoot our direction.
// turnRight(), takes in the ideal speed and refactors it accordingly to how close
// the zumo is pointing towards the direction we want to drive.
void turnRight(int refactor)
{

#include <LSM303.h>

/* This example uses ZumoMotors, Wire, and LSM303 (compass) library to 
 * drive the Zumo in squares. To do this, the Zumo first calibrates 
 * the compass to account for offsets in its output. Calibration is
 * accomplished in setup().
 *
 * In loop(), The driving angle then changes its offset by 90 degrees 
 * from the heading every second. Essentially, this navigates the
 * Zumo to drive in square patterns.
 *
 * It is important to note that stray magnetic fields from electric
 * current (including from the Zumo's own motors) and the environment
 * (for example, steel rebar in a concrete floor) might adversely
 * affect readings from the LSM303 compass and make them less
 * reliable.
 */
 
#define SPEED 200 // maximum motor speed when turning or going straight

#define CALIB_SAMPLES 70
#define CRA_REG_M_220HZ 0x1C // CRA_REG_M value for magnetometer 220 Hz update rate
