Im tying to make an autonomous car. I finished my gps class to calculate a desired heading and I finished a class to take a heading and using a compass direct the steering servo toward that heading. Problem is when I merged the two the steering servo went bazerk. I tried commenting out bit by bit to identify the problem but nothing helped until commenting out the servo attach. As a test I just took my gps class and added
#include <Servo.h>
Servo steering;
steering.attach(10);
and it already started making the servo go baser without any command even being sent to it...So here is the code that works great for heading degree calculation but screws up Servo. Anybody know what the problem is or how to solve it? The gps is the adafruit gps shield using pins 8,7 on top of arduino uno
#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(8, 7);
Adafruit_GPS GPS(&mySerial);
#define GPSECHO false
boolean usingInterrupt = false;
void useInterrupt(boolean); // Func prototype keeps Arduino 0023 happy
int triggercountWaypoint;
long waypointlat[]= {40143318, 40142854, 40142450, 40142775, 40142306};
long waypointlon[]= {-83060232, -83059787, -83060160, -83060669, -83060700};
long currentlat;
long currentlon;
long londif;
long latdif;
int headingdegree;
long waypointDistance;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("GPS heading each waypoint!");
GPS.begin(9600);
GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); // 1 Hz update rate
GPS.sendCommand(PGCMD_ANTENNA);
useInterrupt(true);
delay(1000);
// Ask for firmware version
mySerial.println(PMTK_Q_RELEASE);
triggercountWaypoint=0;
}
SIGNAL(TIMER0_COMPA_vect) {
char c = GPS.read();
#ifdef UDR0
if (GPSECHO)
if (c) UDR0 = c;
#endif
}
void useInterrupt(boolean v) {
if (v) {
OCR0A = 0xAF;
TIMSK0 |= _BV(OCIE0A);
usingInterrupt = true;
} else {
TIMSK0 &= ~_BV(OCIE0A);
usingInterrupt = false;
}
}
uint32_t timer = millis();
void loop() {
// put your main code here, to run repeatedly:
long currentlat = (GPS.latitudeDegrees*1000000);
long currentlon = (GPS.longitudeDegrees*1000000);
if (GPS.newNMEAreceived()) {
if (!GPS.parse(GPS.lastNMEA())) // this also sets the newNMEAreceived() flag to false
return; // we can fail to parse a sentence in which case we should just wait for another
}
if (timer > millis()) timer = millis();
// approximately every 2 seconds or so, print out the current stats
if (millis() - timer > 2000) {
timer = millis(); // reset the timer
if (GPS.fix) {
latdif = (waypointlat[triggercountWaypoint]- currentlat);
londif = (waypointlon[triggercountWaypoint]- currentlon);
waypointDistance = sqrt(latdif*latdif + londif*londif);
Serial.print("Current location: ");
Serial.print(currentlat);
Serial.print(", ");
Serial.println(currentlon);
Serial.print("Current Waypoint: ");
Serial.print(waypointlat[triggercountWaypoint]);
Serial.print(", ");
Serial.println(waypointlon[triggercountWaypoint]);
Serial.print("Lat/Lon difference: ");
Serial.print(latdif);
Serial.print(", ");
Serial.println(londif);
headingdegree = round(atan2(londif, latdif)*180/3.14159265);
if (headingdegree < 0) headingdegree = headingdegree + 360;
Serial.println(headingdegree);
Serial.println(waypointDistance);
}
//if waypointDistance< 5; triggercountWaypoint++
while (!Serial.available());
Serial.read();{
triggercountWaypoint ++;
}
}
}