Devices & Components
Ring with 60 RGB WS2812 LEDs and integrated driver
Arduino Nano
HMC5883L Magnetic Sensor
SSD1306 OLED Display
Hardware & Tools
Soldering kit
Software & Tools
Arduino IDE
Project description
Code
Code
cpp
Arduino code
1/* Start of Code */ 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 // Number of LEDs on Ring 13#define DATA_PIN_RING 3 // Pin 3 connected to RGB Ring 14 15CRGB leds_RING[NUM_LEDS]; 16 17HMC5883L compass; 18int fixedHeadingDegrees; // Used to store Heading value 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");// Print text 32 display.setCursor(22,40); 33 display.println("COMPASS"); 34 display.display(); 35 delay(2000); 36 37// Set measurement range 38compass.setRange(HMC5883L_RANGE_1_3GA); 39 40// Set measurement mode 41compass.setMeasurementMode(HMC5883L_CONTINOUS); 42 43// Set data rate 44compass.setDataRate(HMC5883L_DATARATE_30HZ); 45 46// Set number of samples averaged 47compass.setSamples(HMC5883L_SAMPLES_8); 48 49// Set calibration offset. See HMC5883L_calibration.ino 50compass.setOffset(27, 200); 51} 52 53void loop() 54{ 55Vector norm = compass.readNormalize(); 56 57// Calculate heading 58float heading = atan2(norm.YAxis, norm.XAxis); 59 60// Set declination angle on your location and fix heading 61// You can find your declination on: http://magnetic-declination.com/ 62// (+) Positive or (-) for negative 63// For Montreal,QC declination angle is -14'35W (negative) 64// Formula: (deg + (min / 60.0)) / (180 / M_PI); 65float declinationAngle = (5.0 + (3.0 / 60.0)) / (180 / M_PI); //for Ohrid 66heading -= declinationAngle; 67 68// Correct for heading < 0deg and heading > 360deg 69if (heading < 0) 70{ 71heading += 2 * PI; 72} 73 74if (heading > 2 * PI) 75{ 76heading -= 2 * PI; 77} 78 79// Convert to degrees 80float headingDegrees = heading * 180/M_PI; 81 82// To Fix rotation speed of HMC5883L Compass module 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/* End of Code */
Downloadable files
Schematic
Circuit diagram
Schematic.jpg

Comments
Only logged in users can leave comments