Step 1
#include <Wire.h> //Wire Librarey for I2C communication
#include <MechaQMC5883.h> //QMC5883 Librarey is added since mine is QMC583 and not HMC5883
MechaQMC5883 qmc;
int ledPins[] = {2,3,4,5,6,7,8,9};
char led_count = 7;
void setup() {
Wire.begin();
Serial.begin(9600);
qmc.init();
for (int thisPin=0; thisPin <= led_count; thisPin++){
pinMode(ledPins[thisPin],OUTPUT);
}
}
void loop() {
int x,y,z;
qmc.read(&x,&y,&z);
int heading=atan2(x, y)/0.0174532925;
if(heading < 0)
heading+=360;
heading = 360-heading;
Serial.println(heading);
if (heading > 338 || heading < 22)
{
Serial.println("NORTH");
digitalWrite(ledPins[0],HIGH);
}
if (heading > 22 && heading < 68)
{
Serial.println("NORTH-EAST");
digitalWrite(ledPins[7],HIGH);
}
if (heading > 68 && heading < 113)
{
Serial.println("EAST");
digitalWrite(ledPins[6],HIGH);
}
if (heading > 113 && heading < 158)
{
Serial.println("SOUTH-EAST");
digitalWrite(ledPins[5],HIGH);
}
if (heading > 158 && heading < 203)
{
Serial.println("SOUTH");
digitalWrite(ledPins[4],HIGH);
}
if (heading > 203 && heading < 248)
{
Serial.println("SOTUH-WEST");
digitalWrite(ledPins[3],HIGH);
}
if (heading > 248 && heading < 293)
{
Serial.println("WEST");
digitalWrite(ledPins[2],HIGH);
}
if (heading > 293 && heading < 338)
{
Serial.println("NORTH-WEST");
digitalWrite(ledPins[1],HIGH);
}
delay(500);
for (int thisPin=0; thisPin <= led_count; thisPin++){
digitalWrite(ledPins[thisPin],LOW);
}
}
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.