MrLunk

HMC5883L compass / Arduino, start and remember 1st head...

Mar 25th, 2021 (edited)
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. // HMC5883L-COMPASS-1st-Angle-stored
  2.  
  3. #include <Arduino.h>
  4. #include <Wire.h>
  5. #include <HMC5883L_Simple.h>
  6.  
  7. // Create a compass
  8. HMC5883L_Simple Compass;
  9. int Read1stAngle = 0;
  10. int StartAngle = 0;
  11.  
  12. void setup()
  13. {
  14. Serial.begin(115200);
  15. Wire.begin();
  16.  
  17. // The declination for your area can be obtained from http://www.magnetic-declination.com/
  18. Compass.SetDeclination(-0, 23, 'W');
  19.  
  20. // The device can operate in SINGLE (default) or CONTINUOUS mode
  21. // SINGLE simply means that it takes a reading when you request one
  22. // CONTINUOUS means that it is always taking readings
  23. // for most purposes, SINGLE is what you want.
  24. Compass.SetSamplingMode(COMPASS_SINGLE);
  25.  
  26. // The scale can be adjusted to one of several levels, you can probably leave it at the default.
  27. // Essentially this controls how sensitive the device is.
  28. // Options are 088, 130 (default), 190, 250, 400, 470, 560, 810
  29. // Specify the option as COMPASS_SCALE_xxx
  30. // Lower values are more sensitive, higher values are less sensitive.
  31. // The default is probably just fine, it works for me. If it seems very noisy
  32. // (jumping around), incrase the scale to a higher one.
  33. Compass.SetScale(COMPASS_SCALE_130);
  34.  
  35. // The compass has 3 axes, but two of them must be close to parallel to the earth's surface to read it,
  36. // (we do not compensate for tilt, that's a complicated thing) - just like a real compass has a floating
  37. // needle you can imagine the digital compass does too.
  38. //
  39. // To allow you to mount the compass in different ways you can specify the orientation:
  40. // COMPASS_HORIZONTAL_X_NORTH (default), the compass is oriented horizontally, top-side up. when pointing North the X silkscreen arrow will point North
  41. // COMPASS_HORIZONTAL_Y_NORTH, top-side up, Y is the needle,when pointing North the Y silkscreen arrow will point North
  42. // COMPASS_VERTICAL_X_EAST, vertically mounted (tall) looking at the top side, when facing North the X silkscreen arrow will point East
  43. // COMPASS_VERTICAL_Y_WEST, vertically mounted (wide) looking at the top side, when facing North the Y silkscreen arrow will point West
  44. Compass.SetOrientation(COMPASS_HORIZONTAL_Y_NORTH);
  45.  
  46. }
  47.  
  48. // Our main program loop.
  49. void loop()
  50. {
  51. float heading = Compass.GetHeadingDegrees();
  52.  
  53. Serial.print("Heading: \t");
  54. Serial.print( heading );
  55. Serial.print(" / StartAngle: ");
  56. Serial.print(StartAngle);
  57. Serial.print(" / Read1stAngle: ");
  58. Serial.print(Read1stAngle);
  59. Serial.println();
  60.  
  61. //Store start angle
  62. if ( Read1stAngle == 0 ) {
  63. StartAngle = heading;
  64. Read1stAngle = 1;
  65. }
  66. // END store startangle
  67.  
  68. delay(100);
  69. }
Add Comment
Please, Sign In to add comment