hi i am new to coding i have this code working i am trying to add a servo to take the Error with when the button is pressed it goes to 0 i would like to center the servo and have it move about 25 degrees each way from center depending on the error from the compass i have been trying but i just cant get servo to work
thanks
#include <LSM303.h>
#include <Wire.h>
#include <Servo.h>
#define SERVO 9
#define CENTER 100
#define GOAL 0
// int angle = getDegrees();
int Error;
const int buttonPin = 2; // Used to start comparations
int storedHeading; // Desired course
int deltaHeading; // +/- Difference (Course +/- Desired Course)
boolean buttonState; // Initiates comparations
LSM303 compass; // Output from LSM303
Servo myservo;
void setup ()
{
Serial.begin(9600); // Initiates debuging serial COM
myservo.attach(SERVO);
Wire.begin(); // Initiates I2C comunication
compass.init(); // Initiates LSM303
compass.enableDefault(); // Enable dafault parameters on LSM303
compass.m_min = (LSM303::vector<int16_t>){-361, -424, -268}; // Last parameter calibration readings made AUG-2014
compass.m_max = (LSM303::vector<int16_t>){+371, +287, +378}; // Last parameter calibration readings made AUG-2014
}
void loop()
{
compass.read();
int heading = compass.heading();
Serial.print("Heading: ");
Serial.println(heading);
int buttonState = digitalRead(buttonPin);
if (buttonState == HIGH)
{
storedHeading = heading;
Serial.print("Stored Heading: ");
Serial.println(storedHeading);
}
int myHeading = storedHeading + 360;
deltaHeading = heading - myHeading;
if (deltaHeading < 180) deltaHeading += 360;
if (deltaHeading < 180) deltaHeading += 360;
if (deltaHeading > 180) deltaHeading -= 360;
Serial.print("Error: ");
Serial.println(deltaHeading);
delay (1000);
}