1
2#include <SPI.h>
3#include <Wire.h>
4#include "FastLED.h"
5#include <HMC5883L.h>
6#include <Adafruit_GFX.h>
7#include <Adafruit_SH1106.h>
8#define OLED_RESET 4
9#define SCREEN_ADDRESS 0x3C
10Adafruit_SH1106 display(OLED_RESET);
11
12#define NUM_LEDS 60
13#define DATA_PIN_RING 3
14
15CRGB leds_RING[NUM_LEDS];
16
17HMC5883L compass;
18int fixedHeadingDegrees;
19
20void setup()
21{
22Serial.begin(9600);
23Wire.begin();
24FastLED.addLeds<NEOPIXEL,DATA_PIN_RING>(leds_RING, NUM_LEDS);
25
26 display.begin(SH1106_SWITCHCAPVCC, SCREEN_ADDRESS);
27 display.clearDisplay();
28 display.setTextSize(2);
29 display.setTextColor(WHITE);
30 display.setCursor(22,10);
31 display.println("mircemk");
32 display.setCursor(22,40);
33 display.println("COMPASS");
34 display.display();
35 delay(2000);
36
37
38compass.setRange(HMC5883L_RANGE_1_3GA);
39
40
41compass.setMeasurementMode(HMC5883L_CONTINOUS);
42
43
44compass.setDataRate(HMC5883L_DATARATE_30HZ);
45
46
47compass.setSamples(HMC5883L_SAMPLES_8);
48
49
50compass.setOffset(27, 200);
51}
52
53void loop()
54{
55Vector norm = compass.readNormalize();
56
57
58float heading = atan2(norm.YAxis, norm.XAxis);
59
60
61
62
63
64
65float declinationAngle = (5.0 + (3.0 / 60.0)) / (180 / M_PI);
66heading -= declinationAngle;
67
68
69if (heading < 0)
70{
71heading += 2 * PI;
72}
73
74if (heading > 2 * PI)
75{
76heading -= 2 * PI;
77}
78
79
80float headingDegrees = heading * 180/M_PI;
81
82
83if (headingDegrees >= 1 && headingDegrees < 240)
84{
85fixedHeadingDegrees = map (headingDegrees * 100, 0, 239 * 100, 0, 179 * 100) /100.00;
86}
87else {
88if (headingDegrees >= 240)
89{
90fixedHeadingDegrees = map (headingDegrees * 100, 240 * 100, 360 * 100, 180 * 100, 360 * 100) /100.00;
91}
92}
93
94int headvalue = fixedHeadingDegrees/4.88;
95int ledtoheading = map(headvalue, 0, 59, 59, 0);
96
97Serial.print("Angle:");
98Serial.print(headingDegrees);
99Serial.println();
100
101 display.clearDisplay();
102 display.setTextSize(3);
103 display.setTextColor(WHITE);
104 display.setCursor(20,10);
105 display.println("Angle");
106
107 display.setCursor(10,40);
108 display.println(headingDegrees);
109 display.display();
110
111FastLED.clear();
112
113if (ledtoheading == 0){
114leds_RING[59] = CRGB::Red;
115leds_RING[0] = CRGB::Green;
116leds_RING[58] = CRGB::Green;
117}
118else {
119if (ledtoheading == 59){
120leds_RING[0] = CRGB::Red;
121leds_RING[59] = CRGB::Green;
122leds_RING[1] = CRGB::Green;
123}
124else {
125leds_RING[ledtoheading] = CRGB::Red;
126leds_RING[ledtoheading+1] = CRGB::Green;
127leds_RING[ledtoheading-1] = CRGB::Green;
128}
129}
130
131FastLED.setBrightness(50);
132FastLED.show();
133delay(100);
134}
135
136
dl3jan
6 months ago
which exact hmc5883 lib are u using? got an error during compile saying that compass.setOffset requires 3 parameters. added a 0 as 3rd parameter and then compilation worked