Advertisement
What_Ever

Untitled

Mar 3rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.96 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <Adafruit_Sensor.h>
  3. #include <Adafruit_LSM303_U.h>
  4.  
  5. /* Assign a unique ID to this sensor at the same time */
  6. Adafruit_LSM303_Mag_Unified mag = Adafruit_LSM303_Mag_Unified(12345);
  7.  
  8. void setup(void)
  9. {
  10.   Serial.begin(9600);
  11.   Serial.println("Magnetometer Test"); Serial.println("");
  12.  
  13.   /* Initialise the sensor */
  14.   if(!mag.begin())
  15.   {
  16.     /* There was a problem detecting the LSM303 ... check your connections */
  17.     Serial.println("Ooops, no LSM303 detected ... Check your wiring!");
  18.     while(1);
  19.   }
  20. }
  21.  
  22. void loop(void)
  23. {
  24.   /* Get a new sensor event */
  25.   sensors_event_t event;
  26.   mag.getEvent(&event);
  27.  
  28.   float Pi = 3.14159;
  29.  
  30.   // Calculate the angle of the vector y,x
  31.   float heading = (atan2(event.magnetic.y,event.magnetic.x) * 180) / Pi;
  32.  
  33.   // Normalize to 0-360
  34.   if (heading < 0)
  35.   {
  36.     heading = 360 + heading;
  37.   }
  38.   Serial.print("Compass Heading: ");
  39.   Serial.println(heading);
  40.   delay(500);
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement