#include <Wire.h>
#include <QMC5883LCompass.h>
QMC5883LCompass compass;
String direccio = "";
const unsigned long interval = 250;
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
int a;
volatile bool interruptFlag = false;
void compassInterrupt() {
interruptFlag = true;
}
void setup() {
pinMode(23, OUTPUT);
pinMode(22, INPUT);
Serial.begin(115200);
Wire.begin();
compass.init();
// Attach an interrupt to the compass data ready pin
attachInterrupt(digitalPinToInterrupt(22), compassInterrupt, FALLING);
}
void loop() {
// Continuously read compass data
compass.read();
//a = compass.getAzimuth();
a= random(0,360);
if (a < 0) {
a = a + 360;
}
if ((a >= 157.5) && (a < 202.5)) {
direccio = "Sur";
} else if ((a >= 202.5) && (a < 247.5)) {
direccio = "Suroeste";
} else if ((a >= 247.5) && (a < 292.5)) {
direccio = "Oeste";
} else if ((a >= 292.5) && (a < 337.5)) {
direccio = "Noroeste";
} else if ((a >= 337.5) || (a < 22.5)) {
direccio = "Norte";
} else if ((a >= 22.5) && (a < 67.5)) {
direccio = "Noreste";
} else if ((a >= 67.5) && (a < 112.5)) {
direccio = "Este";
} else if ((a >= 112.5) && (a < 157.5)) {
direccio = "Sureste";
}
Serial.println(direccio);
// Check for the specific condition and execute the code without delay
if (interruptFlag && direccio == "Norte") {
digitalWrite(23, LOW); // Turn off the LED
interruptFlag = false; // Reset the flag
} else {
digitalWrite(23, HIGH); // Turn on the LED
}
// Your other non-interrupt, non-blocking code can go here
}