Hi I'm having a bit of a problem with this code
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>
Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
int power = 10000;
int power2 = 10;
int power3= 10000;
int power4 = 1000;
int nopower = 1000;
const float baselineTemp = 00.0;
void setup(void)
{
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
Serial.begin(9600);
sensors_event_t event;
mag.getEvent(&event);
float heading = atan2(event.magnetic.y, event.magnetic.x);
}
void loop(void){
sensors_event_t event;
mag.getEvent(&event);
float heading = atan2(event.magnetic.y, event.magnetic.x);
if (heading < 130){
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
delay(power);
digitalWrite(13, LOW);
digitalWrite(12, LOW);
delay(nopower);
}else if (heading < 160) {
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
delay(power2);
digitalWrite(13, LOW);
digitalWrite(12, LOW);
delay(nopower);
}else if (heading < 177) {
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
delay(power3);
digitalWrite(13, LOW);
digitalWrite(12, LOW);
delay(nopower);
}else if (heading < 183) { //middle or power surge
digitalWrite(13, LOW);
digitalWrite(12, LOW);
}else if (heading < 190) {
digitalWrite(13, LOW);
digitalWrite(12, HIGH);
delay(power3);
digitalWrite(13, LOW);
digitalWrite(12, LOW);
delay(nopower);
}else if (heading < 220) {
digitalWrite(13, LOW);
digitalWrite(12, HIGH);
delay(power2);
digitalWrite(13, LOW);
digitalWrite(12, LOW);
delay(nopower);
}else if (heading < 360) {
digitalWrite(13, LOW);
digitalWrite(12, HIGH);
delay(power);
digitalWrite(13, LOW);
digitalWrite(12, LOW);
delay(nopower);
}
delay(1);
}
13 and 12 are both led outputs
I am trying to get the compass to change the leds pattern depending on its direction
My problem is that the led is only flashing the first if statement no matter what direction??
Im really new to coding so sorry if its a simple solution
Thanks in advance