Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Marek July 30, 2012
file 94 lines (74 sloc) 1.792 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
/* -*-mode:c; -*- */
#include <Wire.h>
#include <hmc5883L.h>

//#define SERIAL_DO
#define BUZZER_PIN 5

HMC5883L compass;



void setupHMC5883L(){
compass.SetScale(8.1);
compass.SetRegisterA(0, 5, 0);
compass.SetMeasurementMode(Measurement_Continuous); // Set the measurement mode to Continuous
}

float getHeading(){
//Get the reading from the HMC5883L and calculate the heading
MagnetometerScaled scaled = compass.ReadScaledAxis(); //scaled values from compass

#ifdef SERIAL_DO
Serial.print(scaled.XAxis);
Serial.print(" ");
Serial.print(scaled.YAxis);
Serial.print(" ");
Serial.print(scaled.ZAxis);
Serial.print(" ");
#endif

/* float heading = atan2(scaled.YAxis, scaled.XAxis); */

// Correct for when signs are reversed.
/* if(heading < 0) heading += 2*PI; */
/* if(heading > 2*PI) heading -= 2*PI; */

/* return heading * RAD_TO_DEG; //radians to degrees */

return sqrt(scaled.XAxis * scaled.XAxis +
scaled.YAxis * scaled.YAxis +
scaled.ZAxis * scaled.ZAxis);
}

int buzz_prev = 0;
void buzzer(int v) {
if (v) {
if (v < 0) v = 0;
if (v > 10) v = 10;
analogWrite(BUZZER_PIN, 16 * (6 + v) - 1);
if (!buzz_prev) {
buzz_prev = 1;
}
} else {
if (buzz_prev) {
analogWrite(BUZZER_PIN, 0);
buzz_prev = 0;
}
}
}

void setup(){
#ifdef SERIAL_DO
Serial.begin(115200);
  Serial.println("abc");
#endif
  Serial.println("abc");

Wire.begin();
  pinMode(5, OUTPUT);
delay(1);

compass = HMC5883L(); //new instance of HMC5883L library
setupHMC5883L(); //setup the HMC5883L
}

// Our main program loop.
void loop(){
float delta = getHeading();

#ifdef SERIAL_DO
  Serial.println(delta);
#endif

if (delta > 700.0) {
if (delta > 2700) delta = 2700;
// 1..10
buzzer(1 + (delta - 700.)/200.);
} else {
buzzer(0);
}

delay(34);
}
Something went wrong with that request. Please try again.