Advertisement
otakus

Untitled

Nov 9th, 2012
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. // I2C device class (I2Cdev) demonstration Arduino sketch for HMC5883L class
  2. // 10/7/2011 by Jeff Rowberg <jeff@rowberg.net>
  3. // Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
  4. //
  5. // Changelog:
  6. // 2011-10-07 - initial release
  7.  
  8. /* ============================================
  9. I2Cdev device library code is placed under the MIT license
  10. Copyright (c) 2011 Jeff Rowberg
  11.  
  12. Permission is hereby granted, free of charge, to any person obtaining a copy
  13. of this software and associated documentation files (the "Software"), to deal
  14. in the Software without restriction, including without limitation the rights
  15. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  16. copies of the Software, and to permit persons to whom the Software is
  17. furnished to do so, subject to the following conditions:
  18.  
  19. The above copyright notice and this permission notice shall be included in
  20. all copies or substantial portions of the Software.
  21.  
  22. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  28. THE SOFTWARE.
  29. ===============================================
  30. */
  31.  
  32. // Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation
  33. // is used in I2Cdev.h
  34. #include "Wire.h"
  35.  
  36. // I2Cdev and HMC5883L must be installed as libraries, or else the .cpp/.h files
  37. // for both classes must be in the include path of your project
  38. #include "I2Cdev.h"
  39. #include "HMC5883L.h"
  40.  
  41. // class default I2C address is 0x1E
  42. // specific I2C addresses may be passed as a parameter here
  43. // this device only supports one I2C address (0x1E)
  44. HMC5883L mag;
  45.  
  46. int16_t mx, my, mz;
  47.  
  48. #define LED_PIN 13
  49. bool blinkState = false;
  50.  
  51. void setup() {
  52. // join I2C bus (I2Cdev library doesn't do this automatically)
  53. Wire.begin();
  54.  
  55. // initialize serial communication
  56. // (38400 chosen because it works as well at 8MHz as it does at 16MHz, but
  57. // it's really up to you depending on your project)
  58. Serial.begin(38400);
  59.  
  60. // initialize device
  61. Serial.println("Initializing I2C devices...");
  62. mag.initialize();
  63.  
  64. // verify connection
  65. Serial.println("Testing device connections...");
  66. Serial.println(mag.testConnection() ? "HMC5883L connection successful" : "HMC5883L connection failed");
  67.  
  68. // configure Arduino LED for
  69. pinMode(LED_PIN, OUTPUT);
  70. }
  71.  
  72. void loop() {
  73. // read raw heading measurements from device
  74. mag.getHeading(&mx, &my, &mz);
  75.  
  76. float heading = atan2(mx, my);
  77. if(heading < 0) heading += 2*PI;
  78. float headingDegrees = heading * 180/M_PI;
  79.  
  80. // display tab-separated gyro x/y/z values
  81. /*
  82. Serial.print("mag:\t");
  83. Serial.print(mx); Serial.print("\t");
  84. Serial.print(my); Serial.print("\t");
  85. */
  86. Serial.print(mx);
  87. Serial.print(",");
  88. Serial.print(my);
  89. Serial.print(",");
  90. Serial.println(mz);
  91.  
  92. // blink LED to indicate activity
  93. blinkState = !blinkState;
  94. digitalWrite(LED_PIN, blinkState);
  95. delay(10);
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement