controlling servo with compass

|Im just new to the arduino world. but having lots of fun! im trying to turn a servo based on compass orientation. I've been trying to get this bit of code i found to work but no luck! servo just twitches every once in a while whether compass moves or not. I've tested servo it works fine and compass works fine. but with this code i don't even get any serial data streaming. anybody have any ideas to help out a newbie?

#include <Wire.h>
#include <HMC5883L.h>
#include <Servo.h> 

#define SERVO  9
#define CENTER 100
#define GOAL   0

HMC5883L compass;
int      goal = GOAL;
float    declinationAngle = 0.212;    // Radians
Servo    myservo;


void setup() {
 
 
 Serial.begin(9600);
 myservo.attach(SERVO);
 Wire.begin();
 compass = HMC5883L();
 // Set scale to +/- 1.3 Ga
 int error = compass.SetScale(1.3);
 if (error != 0)
   Serial.println(compass.GetErrorText(error));
 // Set measurement mode to continous
 error = compass.SetMeasurementMode(Measurement_Continuous);
 if (error != 0)
   Serial.println(compass.GetErrorText(error));
}

void loop() {
 
int angle = getDegrees();
 int error = goal - angle;
 if (error >= 180)
   error -= 360;
 if (error <= -180)
   error += 360;
 // Update servo and keep with range of +/- 60
 if (error > 60)
   error = 60;
 if (error < -60)
   error = -60;
 myservo.write(CENTER + error);
 delay(10);
}

int getDegrees () {
 MagnetometerScaled scaled = compass.ReadScaledAxis();
  // Calculate heading when the magnetometer is level, then correct for signs of axis.
 float heading = atan2(scaled.YAxis, scaled.XAxis) + declinationAngle;
 // Correct for when signs are reversed.
 if(heading < 0)
   heading += 2 * PI;
 // Check for wrap due to addition of declination.
 if(heading > 2 * PI)
   heading -= 2 * PI;
 // Convert radians to degrees for readability.
 return (int)  (heading * 180 / M_PI); 
}
int angle = getDegrees();

What IS angle?

int error = goal - angle;

What IS error?

but with this code i don't even get any serial data streaming.

Because the ONLY Serial.print() statements are in setup(), and only for errors, which, presumably, are not happening.

Hi,

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Also how are you powering your project, via the USB or externally.

Tom.... :slight_smile:

Why don't you print the values from the compass so you can see if that is working properly ?

How many degrees can your servo rotate ?
Most of them only do about 180 deg. What should happen if the compass is pointing in a direction that the servo cannot reach ?

...R

I started off by powering the servo from the arduino. Which worked fine with other sketches. But just to eliminate that as a issue I powered it with a 5v adapter and connected the negative to the Arduino ground as well. Same results as before.

I apologize I will post code proper way when back on the computer.

This code is to keep servo pointed north. So a full revolution shouldn't be a problem. For now I would be happy to have any movement

Thanks

greenkarson:
For now I would be happy to have any movement

Are you getting valid data from the compass?

If not, there is no point worrying about anything else.

...R

im getting good data from compass with other sketches without changing wires so i know that its not the compass

It might also be reasonable to give your servo more than 10 milliseconds to move to its new position. I would be inclined to set that delay to 1000 and see what happens.

greenkarson:
im getting good data from compass with other sketches

Are you SURE you are getting good data with the sketch that has a problem ?

...R