code not working

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

Moderator: if it is really urgent you would follow the posting guidelines as that is the best way to get most feedback

float heading = atan2(event.magnetic.y, event.magnetic.x);

what are the values atan2() return?

  1. Are those degrees or radians?
  2. Are these values always greater than zero or ?
  1. degrees

  2. dont understand

What value do you see if you print the heading variable ?

sorry i do not no how to do that but when i run the compass using an other program it reads out perfectly

bret1campbell:
1 degrees

Try again

Add

Serial.begin(9600);

to setup() and

Serial.println(heading);

after

float heading = atan2(event.magnetic.y, event.magnetic.x);

-3.06 is that what you meant?
thanks

I won't be as subtle:
http://www.cplusplus.com/reference/cmath/atan2/

Returns the principal value of the arc tangent of y/x, expressed in radians.

KeithRB:
I won't be as subtle:
http://www.cplusplus.com/reference/cmath/atan2/

i dont understand?

What he's saying is, atan2() function returns radians not degrees. As in 2*PI per whole as opposed to 360 degrees.

tammytam:
What he's saying is, atan2() function returns radians not degrees. As in 2*PI per whole as opposed to 360 degrees.

so do i change my values after heading or my atan2() section ??

The right answers were:

  1. radians
  2. values from atan2 can be negative

Can you tell us why it is urgent? Is it a school project due for tomorrow?

in the meanwhile try this fix

float heading = atan2(event.magnetic.y, event.magnetic.x) / (2*PI) + 180;

its a project for going away on holiday i put urgent as i didn't no answers were given so quickly. Removed it now. sorry
That line now makes the less not go on at all now
Thanks

try this one

float heading = atan2(event.magnetic.y, event.magnetic.x) * 360 / (2*PI) + 180;

bret1campbell:
-3.06 is that what you meant?
thanks

You have other answers now which explain what was going on but you should be able to see from your answer (-3.06) that comparing heading values such as 150 was never going to work.

As a general rule print the value that is being tested to see if it is reasonable otherwise you are just guessing.

robtillaart:
try this one

float heading = atan2(event.magnetic.y, event.magnetic.x) * 360 / (2*PI) + 180;

thanks again but its still not working its back to the original problem of not going past the first if statement???
heres where i put the code

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) * 360 / (2*PI) + 180;
}

void loop(void){
sensors_event_t event;
mag.getEvent(&event);
float heading = atan2(event.magnetic.y, event.magnetic.x) * 360 / (2*PI) + 180;

thanks again but its still not working its back to the original problem of not going past the first if

What do you get when you print the heading variable now, and where is the rest of your program ?