Beginner has Problems with HMC5883L steering one (large) DC motor

Hello,

First I want to say sorry for my possibly bad english, but I'm from Germany and school time is long, long, (maybe not too long, but you know...) ago!

I'm a beginner in working with Arduino and I want to build a digital compass with OLED Display, a few buttons and a possibility to connect a large 36V DC motor (or maybe a 36V 3A HARL 3612+, which is, in principe, a DC motor), which acts as a Autopilot for my small Sailing Yacht.

The plan is to mount the magnetometer module (later a kind of IMU) in the middle of the Mast (3m above the steeldeck (I know, a long distance, but propably I solve this making one Unit including the IMU, a Arduino Nano(or similar) and a to it connected Network Module that sends data to another module with the display and buttons etc) which sends data to the Arduino with display and buttons, which can send, after pressing a button, commands to another arduino which steers the Motor via two 12V relais (one forward, one backward).

How I already told you, I'm really new to programming and I just tried to change a few lines in an online available code which lights up a ring of LEDs depending on the compass heading (of HMC 5883L) or tried to combine it with a also online available code which steers two DC motors depending on the Heading of the LSM303 compass module.
!!!BUT I'VE GOT STUCK!!! REALLY!!!

I already got the Magnetometer module working with the OLED (with a different code and library for the HMC-module) , but when I try to steer the motor or right now, two LEDs,
one of the LEDs starts shining and at this point, nothing happens any more.

Could anyone please help me finding the right way to get the whole thing working?
Otherwise my plans for the winter are in danger...

HMC5883LLED.txt (2.24 KB)

#include <Wire.h>
#include <2HMC5883L.h>

int Left=11;
int Right=10;

HMC5883L compass;
int fixedHeadingDegrees; // Used to store Heading value
int gotoHeading;

void setup()
{
pinMode(Left, OUTPUT);
pinMode(Right, OUTPUT);

digitalWrite(Left, LOW);
digitalWrite(Right, LOW);

Serial.begin(9600);

Wire.begin();
gotoHeading= fixedHeadingDegrees;

// Set measurement range
compass.setRange(HMC5883L_RANGE_1_3GA);

// Set measurement mode
compass.setMeasurementMode(HMC5883L_CONTINOUS);

// Set data rate
compass.setDataRate(HMC5883L_DATARATE_30HZ);

// Set number of samples averaged
compass.setSamples(HMC5883L_SAMPLES_8);

// Set calibration offset. See HMC5883L_calibration.ino
compass.setOffset(41, -196);
}

void loop()
{
Vector norm = compass.readNormalize();

// Calculate heading
float heading = atan2(norm.YAxis, norm.XAxis);

// Set declination angle on your location and fix heading
// You can find your declination on: http://magnetic-declination.com/
// (+) Positive or (-) for negative
// For Montreal,QC declination angle is -14'35W (negative)
// Formula: (deg + (min / 60.0)) / (180 / M_PI);
float declinationAngle = (14.0 - (35.0 / 60.0)) / (180 / M_PI);
heading -= declinationAngle;

// Correct for heading < 0deg and heading > 360deg
if (heading < 0)
{
heading += 2 * PI;
}

if (heading > 2 * PI)
{
heading -= 2 * PI;
}

// Convert to degrees
float headingDegrees = heading * 180/M_PI;

// To Fix rotation speed of HMC5883L Compass module
if (headingDegrees >= 1 && headingDegrees < 240)
{
fixedHeadingDegrees = map (headingDegrees * 100, 0, 239 * 100, 0, 179 * 100) /100.00;
}
else {
if (headingDegrees >= 240)
{
fixedHeadingDegrees = map (headingDegrees * 100, 240 * 100, 360 * 100, 180 * 100, 360 * 100) /100.00;
}
}

int headvalue = fixedHeadingDegrees/18;
int AHeading = map(headvalue, 0, 15, 15, 0);

if (AHeading == headvalue){
digitalWrite(Left, LOW);
digitalWrite(Right, LOW);
}
else {
if (AHeading> headvalue){
digitalWrite(Left, LOW);
digitalWrite(Right, HIGH);
}
else {
digitalWrite(Left, HIGH);
digitalWrite(Right, LOW);
}
}

delay(100);
}

In Principe, I want to have this: Motor Testing using compass module HMC5883L - YouTube

(just ab "bit" bigger and stronger)

What, EXACTLY, is connected to pins 10 and 11? What values of AHeading and headvalue are you actually seeing?

Sorry, I recognized I uploaded the second version of my code. In the first one AHeading was compared with "gotoHeading". But with that it does nothing. So I tried to change that and that resulted in setting Pin 10: "HIGH".

Actually there are two LEDs connected to pin 10 and 11, later they will be replaced through a relais- circuit.

What the program should do is turn on ("Left")LED/Motor, if "AHeading"( which was called LEDHeading or sth similar in the Original Code) is smaller than "gotoHeading" and same in the opposite direction.

Via the Serial Monitor I get absolutely no input, which I get when I use my other code with the display.

Any ideas or tips or maybe some found mistakes?

Or maybe I should formulate my question on an other way:

How can I grab data from my magnetometer module and steer one or two DC motors/LEDs/Relais depending on the output of it?

I really want to understand it, because i would like to use these modules also in other projects.

Via the Serial Monitor I get absolutely no input, which I get when I use my other code with the display.

I don't understand the problem. Not seeing output FROM the Arduino is one thing. Not getting input, on the Arduino, FROM the serial monitor application is another. Which issue are we dealing with? "The other code" doesn't cut it. The two sketches should have names, so you refer to them individually. "My brother Darryl, and my other brother Darryl" is funny in a sit-com. It is not funny in real life.