Dear Arduino Community,
i've a question about calling a function from a tab by another tab.
The HMC5883L compass module is used (library: GitHub - jarzebski/Arduino-HMC5883L: HMC5883L Triple Axis Digital Compass Arduino Library).
At startup (setup()) the init. is done an the sensor is recognised.
Calling the function getHeading() from main loop() is working (MainProject) and returning the heading
(serial output).
But trying to call the function getHeading() in another Tab-function (setupView.initView() -> display.viewDisplay()) crashes the ATmega. Am i missing a scope problem?
Thank you very much for your help!.
Regards,
Phi
p.s.: IDE 1.8.1 actual libs on macOS, ATmega 2650
- Tab: compass.ino
#include <Arduino.h>
#include <Wire.h>
#include <HMC5883L.h>
HMC5883L compassX; // static, volatile already tested
void init_compassX(){
while (!compassX.begin())
{
Serial.println("Could not find a valid HMC5883L sensor, check wiring!");
delay(500);
}
debug("compassX initiated");
// Set measurement range
compassX.setRange(HMC5883L_RANGE_1_3GA);
// Set measurement mode
compassX.setMeasurementMode(HMC5883L_CONTINOUS);
// Set data rate
compassX.setDataRate(HMC5883L_DATARATE_30HZ);
// Set number of samples averaged
compassX.setSamples(HMC5883L_SAMPLES_8);
// Set calibration offset. See HMC5883L_calibration.ino
compassX.setOffset(0, 0);
}
void getHeading(){
Vector norm = compassX.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 Bytom / Poland declination angle is 4'26E (positive)
// Formula: (deg + (min / 60.0)) / (180 / M_PI);
float declinationAngle = (4.0 + (26.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;
debug("compass:");
char headingstr[10];
dtostrf(headingDegrees, 8, 4, headingstr);
debug(headingstr);
//return headingDegrees;
}
- Tab: MainProject.ino
void setup() {
init_compassX();
debug("finishedInit");
}
/*
Main Loop
*/
void loop() {
getHeading(); // working
initView(); //error!
}
-
Tab: display.ino
calling:void viewDisplay() { getHeading(); //not working, crashes the ATmega 2560 }
-
Tab: setupView.ino
calling:void initView() { viewDisplay(); }