My project idea was to make a simple compass for my car.
I had just gotten an OLED display and a couple of cheap HMC5883L and this seemed like something I might be able to put together.
Eventually the plan is using a nano and packaging it for permanent car installation.
But currently it is still at the breadboard stage.
My current sketch compassBeta.iso basically reads the data from the HMC5883L every 500ms and updates the OLED display. The display shows 1 of 8 possible headings such as north, northeast, east etc plus a numeric value in degrees of the heading. The code is ugly and probably inefficient but when uploaded to the Uno from the PC and left plugged into the USB it works.
#include "HCuOLED.h"
#include "SPI.h"
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>
const byte CS_DI = 10;
const byte DC_DI = 9;
const byte RST_DI = 8;
Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
HCuOLED HCuOLED(SH1106, CS_DI, DC_DI, RST_DI);
void setup()
{
//Serial.begin(9600);
HCuOLED.Reset();
}
void loop()
{
sensors_event_t event;
mag.getEvent(&event);
float heading = atan2(event.magnetic.y, event.magnetic.x);
float declinationAngle = 0.056;
heading += declinationAngle;
if (heading < 0)
heading += 2 * PI;
if (heading > 2 * PI)
heading -= 2 * PI;
float headingDegrees = heading * 180 / M_PI;
//Serial.println(headingDegrees);
eightPoint(headingDegrees);
}
void eightPoint(float hD) {
HCuOLED.SetFont(LCDLarge_24pt);
if (hD > 337 || hD <= 22) {
HCuOLED.Cursor(35, 10);
HCuOLED.Print("North");
}
if (hD > 22 && hD <= 67) {
HCuOLED.Cursor(2, 10);
HCuOLED.Print("NorthEast");
}
if (hD > 67 && hD <= 112) {
HCuOLED.Cursor(45, 10);
HCuOLED.Print("East");
}
if (hD > 112 && hD <= 157) {
HCuOLED.Cursor(2, 10);
HCuOLED.Print("SouthEast");
}
if (hD > 157 && hD <= 202) {
HCuOLED.Cursor(35, 10);
HCuOLED.Print("South");
}
if (hD > 202 && hD <= 247) {
HCuOLED.Cursor(2, 10);
HCuOLED.Print("SouthWest");
}
if (hD > 247 && hD <= 292) {
HCuOLED.Cursor(45, 10);
HCuOLED.Print("West");
}
if (hD > 292 && hD <= 337) {
HCuOLED.Cursor(2, 10);
HCuOLED.Print("NorthWest");
}
HCuOLED.Cursor(50, 50);
HCuOLED.SetFont(MedProp_11pt);
HCuOLED.Print(hD);
HCuOLED.Print("*");
//Serial.println(hD);
HCuOLED.Refresh();
delay(500);
HCuOLED.Erase(0, 0, 127, 63);
}
At this point I discovered I was not happy with the performance of the HMC5883L. East and west are not too bad. Usually only off by a couple of degrees. But north and south can vary from actual by up to 45 degrees.
This led me to want to take the prototype outside to test if all the electronics and speakers in my house could be partially responsible for the poor performance. Plus I want to see how visible the display is in direct sunlight while I'm wearing sunglasses.
So I use a Duracell 9v alkaline battery with a connector for the barrel jack on the Uno so the prototype will be portable.
Prototype displays an initial heading and then promptly stops updating.
I check connections and everything still seems to be attached correctly to the breadboard and the Uno.
I plug the Uno back into the PC. Still does not update.
I re-upload the compassBeta sketch. The display still does not update past the initial directional value.
I upload the magsensor.ino sketch which is the example provided for my HMC5883L. Serial monitor shows the heading values updating.
I upload one of the example sketches for the OLED that does an animation. OLED is working.
I upload my compassBeta sketch. It now works again.
So I disconnect my UNO from the PC and plug it into a USB powerbank.
Prototype displays an initial heading and then promptly stops updating.
I reattach Uno to PC USB. Still doesn't update.
I re-upload my sketch. Still doesn't update.
I upload magsensor sketch and then immediately upload compassBeta sketch and it begins to work again.
I unplug Uno from PC then plug it back in to PC.
Prototype displays an initial heading and then promptly stops updating.
I upload a blank sketch and then compassBeta sketch. OLED display does not update.
I upload magsensor sketch then upload compassBeta sketch. Prototype starts working fine again.
TLDR; My sketch only works if I first upload the magsensor sketch. I can upload any other sketch after magsensor sketch and then my sketch and my sketch runs fine until I disconnect USB. Then it will not work until magsensor sketch is uploaded again.
magsensor.ino (4.05 KB)