Okay hackscribble, I took your advice and brought the whole gpsRead function into the main loop. It now reads from software serial every time it loops, and only outputs when the button is pressed. It now works every single time I press the button. Only problem now is that it works every time... twice. On button push equals two loops through the gps output code. This would be funny if I were someone else looking at me. ![]()
void loop()
{
while (mySerial.available())
{
int c = mySerial.read();if (gps.encode(c))
{
gps.f_get_position(&flat, &flon, &fix_age);
}
}debouncer.update();
buttonValue = debouncer.read();if (buttonValue == LOW)
{
digitalWrite(ledPin, HIGH);// Handle if the GPS data is out of date or no satellite fix has been made.
if (fix_age == TinyGPS::GPS_INVALID_AGE)
{
Serial.println("No satellite fix detected");
}
else if (fix_age > 5000)
{
Serial.println("Warning: no recent satellite fix. GPS data may be stale.");
}Serial.print("Latitude:\t");
Serial.println(flat, 4);
Serial.print("Longitude:\t");
Serial.println(flon, 4);/* Caution. This is the pythagorean method of calculating distance.
It will become inaccurate at distances beyond about 20 miles
due to the curvature of the Earth.
*/
distanceToTarget = sqrt(pow((targetLat - flat),2) + pow((targetLon - flon), 2));// Compare the current coordinates against the target coordinates and target size(circle around the target)
// If you are within that circle, open the solenoid, if not, give the distance to target.
if (flat > (targetLat - targetSize) && flat < (targetLat + targetSize) && flon > (targetLon - targetSize) && flon < (targetLon + targetSize))
{
Serial.println("You've reached the target!");
openSolenoid();
}
else
{
Serial.print("Distance to target: ");
Serial.print(distanceToTarget, 4);
Serial.println(" kilometers.");
}
}
}