Пн фев 28, 2022 21:27:18
/*
HMC5883L Triple Axis Digital Compass. Compass Example.
Read more: http://www.jarzebski.pl/arduino/czujniki-i-sensory/3-osiowy-magnetometr-hmc5883l.html
GIT: https://github.com/jarzebski/Arduino-HMC5883L
Web: http://www.jarzebski.pl
(c) 2014 by Korneliusz Jarzebski
*/
#include <StaticThreadController.h>
#include <Thread.h>
#include <ThreadController.h>
#include <Wire.h>
#include "HMC5883L.h"
ThreadController controll = ThreadController();
HMC5883L compass;
//My Thread (as a pointer)
Thread* myThread = new Thread();
//His Thread (not pointer)
Thread hisThread = Thread();
void mycompass (){
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 Bytom / Poland declination angle is 4'26E (positive)
// Formula: (deg + (min / 60.0)) / (180 / M_PI);
// Convert to degrees
float headingDegrees = heading * 180/M_PI;
// Output
Serial.print(" Heading = ");
Serial.print(heading);
Serial.print(" Degress = ");
Serial.print(headingDegrees);
Serial.println();
//delay(100);
}
void setup()
{
Serial.begin(9600);
// Initialize Initialize HMC5883L
Serial.println("Initialize HMC5883L");
while (!compass.begin())
{
Serial.println("Could not find a valid HMC5883L sensor, check wiring!");
delay(500);
}
// Set measurement range
compass.setRange(HMC5883L_RANGE_1_3GA);
// Set measurement mode
compass.setMeasurementMode(HMC5883L_CONTINOUS);
// Set data rate
compass.setDataRate(HMC5883L_DATARATE_15HZ);
// Set number of samples averaged
compass.setSamples(HMC5883L_SAMPLES_8);
// Set calibration offset. See HMC5883L_calibration.ino
compass.setOffset(0, 0);
// Configure myThread
hisThread.onRun(mycompass);
hisThread.setInterval(100);
// Adds both threads to the controller
controll.add(myThread);
controll.add(&hisThread); // & to pass the pointer to it
//---------- 1 for interrupt frequency---------------------------------------
TCCR1A = 0; // set entire TCCR1A register to 0
TCCR1B = 0; // same for TCCR1B
TCNT1 = 0; // initialize counter value to 0
TIFR1 |= (1<<OCF1B); //сброс флагов прерывания timer1
OCR1A = 2000; //
TCCR1B |= (0 << CS12) | (1 << CS11) | (0 << CS10); // Set CS12, CS11 and CS10 bits for 8 prescaler
//TCCR1B = 0; // Stop Timer 1
TCCR1B |= (1 << WGM12);// turn on CTC mode
//TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
//sei(); // allow interrupts
}
void loop()
{
controll.run();
}
Пн фев 28, 2022 21:43:07
Пн фев 28, 2022 21:55:28
Вт мар 01, 2022 10:07:13
Вт мар 01, 2022 10:16:33