Hi all,
the Servo is misbehaving,
I have tried powering it up from external source, yet it continues to move (in a sweep motion) between 80-110 deg !
Could you advice on what is going on ?
#include <SoftwareSerial.h>
#include <TinyGPS++.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_LSM303_U.h>
#include <Adafruit_BMP085_U.h>
#include <Adafruit_L3GD20_U.h>
#include <Adafruit_10DOF.h>
#include <Servo.h>
#define RXPin 10
#define TXPin 11
#define GPSBaud 9600
#define ConsoleBaud 115200
#define PI 3.1415926535897932384626433832795
SoftwareSerial ss(RXPin, TXPin);
TinyGPSPlus gps;
Servo myservo;
unsigned long lastUpdateTime = 0;
int servoPin = 22;
#define EIFFEL_LAT 14.770775
#define EIFFEL_LNG 57.056785
/* Assign a unique ID to the sensors */
Adafruit_10DOF dof = Adafruit_10DOF();
//Adafruit_LSM303_Accel_Unified accel = Adafruit_LSM303_Accel_Unified(30301);
Adafruit_LSM303_Mag_Unified mag = Adafruit_LSM303_Mag_Unified(30302);
//Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(18001);
////////////////////////////////////////////////////////////////////////////////////////
void initSensors()
{
/* if(!accel.begin())
{
// There was a problem detecting the LSM303 ... check your connections
Serial.println(F("Ooops, no LSM303 detected ... Check your wiring!"));
while(1);
} */
if(!mag.begin())
{
// There was a problem detecting the LSM303 ... check your connections
Serial.println("Ooops, no LSM303 detected ... Check your wiring!");
while(1);
}
/* if(!bmp.begin())
{
// There was a problem detecting the BMP180 ... check your connections
Serial.println("Ooops, no BMP180 detected ... Check your wiring!");
while(1);
}*/
}
////////////////////////////////////////////////////////////////
void setup()
{
myservo.attach(servoPin);
myservo.write(90);
Serial.begin(ConsoleBaud);
ss.begin(GPSBaud);
//Serial.println("Starting the I2C interface.");
Wire.begin(); // Start the I2C interface.
initSensors();
}
///////////////////////////////////////////////////////////////////
void loop()
{
// If any characters have arrived from the GPS,
// send them to the TinyGPS++ object
while (ss.available() > 0)
gps.encode(ss.read());
// Every 5 seconds, do an update.
if (millis() - lastUpdateTime >= 5000)
{
lastUpdateTime = millis();
Serial.println();
sensors_vec_t orientation;
sensors_event_t mag_event;
/* Calculate the heading using the magnetometer */
mag.getEvent(&mag_event);
if (dof.magGetOrientation(SENSOR_AXIS_Z, &mag_event, &orientation))
{
/* 'orientation' should have valid .heading data now */
Serial.print(F("Heading: "));
Serial.print(orientation.heading);
Serial.print(F("; "));
}
// Establish our current status
// Find distance from current location, to Target
double distanceToDestination = TinyGPSPlus::distanceBetween(
gps.location.lat(), gps.location.lng(),EIFFEL_LAT, EIFFEL_LNG);
// Find heading in degrees, from current location, to target
double courseToDestination = TinyGPSPlus::courseTo(
gps.location.lat(), gps.location.lng(), EIFFEL_LAT, EIFFEL_LNG);
const char *directionToDestination = TinyGPSPlus::cardinal(courseToDestination);
int courseChangeNeeded = (int)(360 + courseToDestination - gps.course.deg()) % 360;
// debug
Serial.print("DISTANCE: ");
Serial.print(distanceToDestination);
Serial.print(" , Angle Course2Dest: ");
Serial.print(courseToDestination);
// Serial.print(" CurCourse: ");
// Serial.print(gps.course.deg());
// bearing by GPS , need to check true bearing by compas.
// Bearing set (which will actually be compass heading)
Serial.print(" , Cardinal Dir2Dest: ");
Serial.print(directionToDestination);
Serial.println();
// Serial.print(" RelCourse: ");
// Serial.print(courseChangeNeeded);
// Serial.print(" CurSpd: ");
// Serial.println(gps.speed.kmph());
// Within 20 meters of destination? We're here!
if (distanceToDestination <= 20.0)
{
Serial.println("CONGRATULATIONS: You've arrived! ");
exit(1);
}
}
}
Moderator edit: CODE TAGS, ALWAYS CODE TAGS