Wearable digital compass displays the direction in green LEDs.
float headingDegrees = heading * 180/PI; int cur_led = map((int)headingDegrees, 360,0,0,12); //north adjust cur_led = (cur_led+6) % lights; digitalWrite(leds[cur_led], HIGH);First, we adjust from radians to degrees by multiplying by 180 / PI. Then we map from the 360 possible degrees to a number between 0 and 11 which will be the index of the LED we want to light up. Notice that the mapping also reverses the order of Heading Degrees so that 360 gets mapped to 0 and 0 gets mapped to 12. This is necessary because the compass module is on the opposite side of the PCB as the silkscreened directions so to make them match up we have to flip the order. Lastly, we adjust the LED index so that the North LED will correspond to facing north. Finally, we do a digitalWrite to the LED.
/* LucidTronix LED Compass * The HMC 5583 compass mapped to 12 LEDs * on a beautifully silkscreened PCB * For instructions, details and schematic, See: * http://www.lucidtronix.com/tutorials/42 */// Reference the I2C Library#include// Reference the HMC5883L Compass Library#include // Store our compass as a variable.HMC5883L compass;int lights = 12;int leds[] = {0,1,2,3,4,5,6,7,8,9,10,17};int pwm_leds[] = {3,5,6,9,10,11 };int buttonPin = 16;int mode = 0;int modes = 6;int wait = 400;int cur_fade = 0;int delta = 1;int cur_led_chase =0;unsigned int last_press = 0;void setup() // run once, when the sketch starts{ for (int i = 0; i < lights; i++){ pinMode(leds[i], OUTPUT); } pinMode(buttonPin, INPUT); Wire.begin(); // Start the I2C interface. compass = HMC5883L(); // Construct a new HMC5883 compass. float scalar = 1.3; //not checking errors you should if you have issues. compass.SetScale(scalar); // Set the scale of the compass. compass.SetMeasurementMode(Measurement_Continuous); // Set the measurement mode to Continuous for (int i = 0; i < lights; i++){ digitalWrite(leds[i], HIGH); delay(40); } delay(250); for (int i = 0; i < lights; i++){ digitalWrite(leds[i], LOW); delay(30); } delay(100);}void loop() // run over and over again{ compass_mode();}void compass_mode(){ for (int i = 0; i < lights; i++) digitalWrite(leds[i], LOW); MagnetometerRaw raw = compass.ReadRawAxis(); // Retrived the scaled values from the compass (scaled to the configured scale). MagnetometerScaled scaled = compass.ReadScaledAxis(); // Values are accessed like so: int MilliGauss_OnThe_XAxis = scaled.XAxis;// (or YAxis, or ZAxis) // Calculate heading when the magnetometer is level, then correct for signs of axis. float heading = atan2(scaled.YAxis, scaled.XAxis); float declinationAngle = 0.0457; heading += declinationAngle; // heading += northAdjust; // Correct for when signs are reversed. if(heading < 0) heading += 2*PI; // Check for wrap due to addition of declination. if(heading > 2*PI) heading -= 2*PI; float headingDegrees = heading * 180/PI; int cur_led = map((int)headingDegrees, 360,0,0,12); //north adjust cur_led = (cur_led+6) % lights; digitalWrite(leds[cur_led], HIGH); delay(50); }
| Title | Description | # | Cost | Link | Picture |
| HMC5883L-TR | IC COMPASS 3 AXIS I2C 16LCC SMDValue:2.16 V ~ 3.6 V ±8G Compass - Three Axis | 1 | $3.44 | Link |
|
| ATMEGA168A-AU | IC MCU AVR 16K FLASH 32TQFPValue:1.8 V ~ 5.5 V 20MHz | 1 | $2.43 | Link |
|
| Crystal | CRYSTAL 16.00000 MHZ 20PF SMDValue:16MHz | 1 | $0.53 | Link |
|
| Switch | SWITCH SLIDE DPDT 6VDC 0.3A SMTValue:DPDT | 1 | $0.44 | Link |
|
| Button | Tactile switch SMD SPST 0.05A 12VValue:SPST | 1 | $0.2 | Link |
|
| LED SMD | LED 3X1.5MM 568NM Green Clear SMDValue:2.2V Green | 12 | $0.18 | Link |
|
| Resistor | Chip Resistor - Surface Mount RES 10K OHM 1/8W 5% 0805 SMDValue: 10k | 3 | $0.1 | Link |
|
| Capacitor | CAP CER 0.1UF 50V Y5V 0805Value:0.1µF | 1 | $0.1 | Link |
|
| Resistor Array | RES ARRAY 300 OHM 4 RES 1206Value:300 | 3 | $0.02 | Link |
|
Comments: