my ultimate goal is to be able to move a pan/ tilt system to show the user where to navigate. In order to do this, I would have to periodically update the servo values to direct the user the destination. I wrote this code to move the servos, but the servos do not move if I move the module in a different direction. Do you have any ideas on how I could do this.
thanks for your help
rdeans
#include <SoftwareSerial.h>
#include "TinyGPS.h"
#include <Servo.h>
/* This sample code demonstrates the normal use of a TinyGPS object.
It requires the use of SoftwareSerial, and assumes that you have a
4800-baud serial GPS device hooked up on pins 3(rx) and 4(tx).
*/
Servo panservo;
Servo tiltservo;
TinyGPS gps;
SoftwareSerial ss(3, 4);
float dest_latitude = 42.24313;
float dest_longitude = -83.002853;
float distance;
float distanceangle;
void setup()
{
Serial.begin(115200);
Serial.print("Simple TinyGPS library v. ");
Serial.println(TinyGPS::library_version());
Serial.println("by Mikal Hart");
Serial.println();
}
void loop()
{
ss.begin(9600);
bool newData = false;
unsigned long chars;
unsigned short sentences, failed;
// For one second we parse GPS data and report some key values
for (unsigned long start = millis(); millis() - start < 1000;)
{
while (ss.available())
{
char c = ss.read();
// Serial.write(c); // uncomment this line if you want to see the GPS data flowing
if (gps.encode(c)) // Did a new valid sentence come in?
newData = true;
}
}
if (newData)
{
float flat, flon;
unsigned long age;
gps.f_get_position(&flat, &flon, &age);
Serial.print("LAT=");
Serial.print(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6);
Serial.print(" LON=");
Serial.print(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6);
Serial.print(" SAT=");
Serial.print(gps.satellites() == TinyGPS::GPS_INVALID_SATELLITES ? 0 : gps.satellites());
Serial.print(" PREC=");
Serial.print(gps.hdop() == TinyGPS::GPS_INVALID_HDOP ? 0 : gps.hdop());
gps.stats(&chars, &sentences, &failed);
Serial.print(" CHARS=");
Serial.print(chars);
Serial.print(" SENTENCES=");
Serial.print(sentences);
Serial.print(" CSUM ERR=");
Serial.println(failed);
float course = (TinyGPS::course_to(flat, flon, dest_latitude, dest_longitude ));
float courseangle;
float distance = (TinyGPS::distance_between(flat, flon, dest_latitude, dest_longitude ));
float distanceangle;
ss.end();
delay(4000);
panservo.attach(9);
tiltservo.attach(10);
if (distance <= 100){
distanceangle =int(-0.6*distance + 120);
Serial.print(" distance is less");
Serial.print( distanceangle);
tiltservo.write(distanceangle);
}
else {
distanceangle = 60;
Serial.print(" distance is greater ");
Serial.print( distanceangle);
tiltservo.write(distanceangle);
}
if (270 <= course && course < 360){
courseangle = int(-course + 450);
Serial.print (" course is case 1 ");
Serial.print ( courseangle);
panservo.write(courseangle);
}
else if (0 <= course && course <= 90){
courseangle = int(-course + 90);
Serial.print (" course is case 2 ");
Serial.print ( courseangle);
panservo.write(courseangle);
}
else if (90 < course && course < 180){
courseangle = 0;
Serial.print (" course is case 3 ");
Serial.print ( courseangle);
panservo.write(courseangle);
}
else if (180 <= course && course < 270){
courseangle = 180;
Serial.print (" course is case 4 ");
Serial.print ( courseangle);
panservo.write(courseangle);
}
panservo.detach();
tiltservo.detach();
}
}