gps geofence - compile error

Dear community

I am trying to create a gps alarm (if the item moves the alarm goes on)
So I need to set the initial coordinates as a fix location, after that save them and measure the distance from saved location. if the distance exeeds radius then the alarm goes on.
I have no previous experience in coding and any help is greatly appretiated
I'm trying to compile and get the following errors:

///////////////////////code//////////////////////////////

#include <TinyGPS++.h>

TinyGPSPlus gps;
#define Radius 0.00003 // Alarm Radius = 3.3 m

const int buzzerPin = 7;
const int gpspin = 13;

void setup()
{
Serial.begin(9600);
pinMode(gpspin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
}
void loop()
{
while (Serial.available() > 0)
{
if (gps.encode(Serial.read()))
{
bool status = move();

if (status == true)
{
TurnOnAlarm();
}
else
{
TurnOffAlarm();
}
}
}
}

}

bool move()
{
if (gps.location.isValid())
{
double lat, lng, latmove, lngmove;
// Extract GPS co-ordinates
lat = gps.location.lat();
lng = gps.location.lng();

// Print GPS co-ordinates on Serial monitor
Serial.print(F("Location: "));
Serial.print(lat, 5);
Serial.print(F(","));
Serial.print(lng, 5);
latmove = setLat - lat;
lngmove = setLng - lng;

if( ((latmove >= Radius) && (latmove <= (Radius * -1)))
&& ((lngmove >= Radius) && (lngmove <= (Radius * -1))) )
{
return true;
}
}
else
{
Serial.print(F("GPS Err"));
}
return false;
}
}
void TurnOnAlarm()
{
Serial.println(" \t Alarm ON");
digitalWrite(gpspin, HIGH);
tone(buzzerPin, 1000);
}
}
void TurnOffAlarm()
{
Serial.println(" \t Alarm Off");
}
noTone(buzzerPin);
}

//////////////////////// err msg: /////////////////////////////////

Arduino: 1.8.14 Hourly Build 2020/09/14 03:35 (Mac OS X), Board: "Arduino MKR WiFi 1010"

/Users/yannis/Documents/Arduino/sketch_sep28a_project_geofence_2.1/sketch_sep28a_project_geofence_2.1.ino: In function 'void loop()':
sketch_sep28a_project_geofence_2.1:22:21: error: 'move' was not declared in this scope
bool status = move(); // Check if reached the destination and Print required data.
^~~~
/Users/yannis/Documents/Arduino/sketch_sep28a_project_geofence_2.1/sketch_sep28a_project_geofence_2.1.ino:22:21: note: suggested alternative: 'remove'
bool status = move(); // Check if reached the destination and Print required data.
^~~~
remove
sketch_sep28a_project_geofence_2.1:26:9: error: 'TurnOnAlarm' was not declared in this scope
TurnOnAlarm(); // Trigger the alarm to notify the user
^~~~~~~~~~~
sketch_sep28a_project_geofence_2.1:30:9: error: 'TurnOffAlarm' was not declared in this scope
TurnOffAlarm();
^~~~~~~~~~~~
/Users/yannis/Documents/Arduino/sketch_sep28a_project_geofence_2.1/sketch_sep28a_project_geofence_2.1.ino: At global scope:
sketch_sep28a_project_geofence_2.1:36:1: error: expected declaration before '}' token
}
^
exit status 1
'move' was not declared in this scope

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

If you use the IDE autoformat tool (ctrl-t or Tools, Auto Format) you will see that you have some misplaced and extra closing brackets ('}').
Here is your code autoformatted with the brackets commented:

#include <TinyGPS++.h>

TinyGPSPlus gps;
#define Radius 0.00003 // Alarm Radius = 3.3 m

const int buzzerPin = 7;
const int gpspin = 13;

void setup()
{
   Serial.begin(9600);
   pinMode(gpspin, OUTPUT);
   pinMode(buzzerPin, OUTPUT);
}

void loop()
{
   while (Serial.available() > 0)
   {
      if (gps.encode(Serial.read()))
      {
         bool status = move();

         if (status == true)
         {
            TurnOnAlarm();
         }
         else
         {
            TurnOffAlarm();
         }
      }
   }
}

// }  extra } ************************

bool move()
{
   if (gps.location.isValid())
   {
      double lat, lng, latmove, lngmove;
      // Extract GPS co-ordinates
      lat = gps.location.lat();
      lng = gps.location.lng();
      
      // Print GPS co-ordinates on Serial monitor
      Serial.print(F("Location: "));
      Serial.print(lat, 5);
      Serial.print(F(","));
      Serial.print(lng, 5);
      latmove = setLat - lat;
      lngmove = setLng - lng;

      if ( ((latmove >= Radius) && (latmove <= (Radius * -1)))
            && ((lngmove >= Radius) && (lngmove <= (Radius * -1))) )
      {
         return true;
      }
   }
   else
   {
      Serial.print(F("GPS Err"));
   }
   return false;
}
//}   extra } ************************************

void TurnOnAlarm()
{
   Serial.println(" \t Alarm ON");
   digitalWrite(gpspin, HIGH);
   tone(buzzerPin, 1000);
}
//}   extra }  **************************************

void TurnOffAlarm()
{
   Serial.println(" \t Alarm Off");
//}   extra }  ************************************
noTone(buzzerPin);
}

Then the setLat and setLng variables are not declared.
Fix that and the code compiles.

If you use the IDE autoformat tool (ctrl-t or Tools, Auto Format) you will see that you have some misplaced and extra closing brackets ('}').
Here is your code autoformatted with the brackets commented:

#include <TinyGPS++.h>

TinyGPSPlus gps;
#define Radius 0.00003 // Alarm Radius = 3.3 m

const int buzzerPin = 7;
const int gpspin = 13;

void setup()
{
   Serial.begin(9600);
   pinMode(gpspin, OUTPUT);
   pinMode(buzzerPin, OUTPUT);
}

void loop()
{
   while (Serial.available() > 0)
   {
      if (gps.encode(Serial.read()))
      {
         bool status = move();

         if (status == true)
         {
            TurnOnAlarm();
         }
         else
         {
            TurnOffAlarm();
         }
      }
   }
}

// }  extra } ************************
// that extra bracket is why the move() function is out of scope

bool move()
{
   if (gps.location.isValid())
   {
      double lat, lng, latmove, lngmove;
      // Extract GPS co-ordinates
      lat = gps.location.lat();
      lng = gps.location.lng();
      
      // Print GPS co-ordinates on Serial monitor
      Serial.print(F("Location: "));
      Serial.print(lat, 5);
      Serial.print(F(","));
      Serial.print(lng, 5);
      latmove = setLat - lat;
      lngmove = setLng - lng;

      if ( ((latmove >= Radius) && (latmove <= (Radius * -1)))
            && ((lngmove >= Radius) && (lngmove <= (Radius * -1))) )
      {
         return true;
      }
   }
   else
   {
      Serial.print(F("GPS Err"));
   }
   return false;
}
//}   extra } ************************************

void TurnOnAlarm()
{
   Serial.println(" \t Alarm ON");
   digitalWrite(gpspin, HIGH);
   tone(buzzerPin, 1000);
}
//}   extra }  **************************************

void TurnOffAlarm()
{
   Serial.println(" \t Alarm Off");
//}   extra }  ************************************
   noTone(buzzerPin);
}

Then the setLat and setLng variables are not declared.
Fix that and the code compiles.

Read the how to use this forum-please read sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

Hello groundFungus,

thank you very much for your reply. I followed your suggestions and the code compiles.
I will follow the forus rules from now on.
Please note that the auto formatting tool does not work correctly for me. I tried to add extra brackets then run the auto format and the brackets would stay there.

Kind regards,

Yannis

The autoformat does not remove or fix extra brackets, but it does show you that they are there.

Oh ok Thank you!! :slight_smile:

Hello, I am trying to set initial lat & lng in order to compare it with current lat & lng using the tinygps++ lib. Any assistance is greatly appreciated

#include <TinyGPS++.h> // Include the TinyGPS++ library
TinyGPSPlus gps; // Create a TinyGPSPlus object

#define GPS_BAUD 9600 // GPS module baud rate

#define ARDUINO_GPS_RX 14 // GPS TX, Arduino RX pin
#define ARDUINO_GPS_TX 13 // GPS RX, Arduino TX pin


#define SerialMonitor Serial

int initialLat = 0;
int initialLng = 0;

void setup()
{
  SerialMonitor.begin(9600);
  Serial1.begin(GPS_BAUD);
  initialGPSlock();
}

void loop()
{

  SerialMonitor.println();
  SerialMonitor.print("Initial Lat: "); SerialMonitor.println(initialLat, 6);
  SerialMonitor.print("Initial Lng: "); SerialMonitor.println(initialLng, 6);
  SerialMonitor.println();


  delay(1000);

}

int initialGPSlock()
{


  while (Serial1.available() > 0) {
    gps.encode(Serial1.read());
  }

  if ( gps.satellites.value() >= 5 && gps.hdop.hdop() <= 5 && gps.sentencesWithFix() > 5) {

    int initialLat;
    int initialLng;

    initialLat = (gps.location.lat(), 6);
    initialLng = (gps.location.lng(), 6);
  }

  return  initialLat;  initialLng;
}

Its not clear what you mean by 'set initial lat & lng' ?

Do you want to save the GPSs first fix, from power on for instance, and then compare it with subsequent fixes, and if so for what reason ?

yantypas:
Dear community

I am trying to create a gps alarm (if the item moves the alarm goes on)
So I need to set the initial coordinates as a fix location, after that save them and measure the distance from saved location. if the distance exeeds radius then the alarm goes on.

TinyGPSPlus gps;
#define Radius 0.00003 // Alarm Radius = 3.3 m

Using the first fix as the location is not a good idea, you likley need to wait a good minute or more for the location to stabilise. And then detecting a movement of as little as 3.3m might be ambitious. Variations of 3-10m in position are not unusual for a stationary GPS.

Also depends where the GPS actually is, what is the application ?

yantypas:
Hello, I am trying to set initial lat & lng in order to compare it with current lat & lng using the tinygps++ lib.

Do you think your GPS may be giving the wrong position? That's the route to madness :slight_smile:

...R

Dear
Robin2,

thank you for your reply.
No I would like to save the first good GPS fix :

[color=#222222]if ( gps.satellites.value() >= 5 && gps.hdop.hdop() <= 5 && gps.sentencesWithFix() > 5)[/color]

and then move away from that point and calc the distance between 1st and 2nd fix(current location).
I am later going to display the difference in meters. For now I just need to find a way to save the first good fix.
Any ideas?

Use a bool variable;

bool havefirstfix = false;

Then when the program starts and you get the first fix set the varaible true, havefirstfix = true;

So in psuedo code you would do something like this every time you get a fix;

if (havefirstfix)
{
docalcdistance();
{
else
{
havefirstfix = true;
}

yantypas:
No I would like to save the first good GPS fix :

if ( gps.satellites.value() >= 5 && gps.hdop.hdop() <= 5 && gps.sentencesWithFix() > 5)

and then move away from that point and calc the distance between 1st and 2nd fix(current location).

Thanks. That was not obvious from your Original Post or your Title. I think it would have been clearer if you had said
Trying to set save initial GPS data in order to compare it with current subsequent location

...R

long lGPS_Lat0 = 0;
long lGPS_Lon0 = 0;
int lDistance = 0;
#define DEGREE 6000000
#define DistaceResetPin 11
#define StartStopDistanceCounterPin 10
////
void fCalDistance()
{


  if (digitalReadDirect(StartStopDistanceCounterPin) == 0)
  {
    //get lat lon values if lat is 0
    StartStopDistanceCounter();

    //doing the * 60 * 100 here is faster then using a function for the math
    long tempLat = round(GPS.latitude * 100) * 60;
    long tempLon = round(GPS.longitude * 100) * 60;
    //
    //add new distance to previous distance
    lDistance = lDistance + DistanceBetween(lGPS_Lat0, lGPS_Lon0, tempLat, tempLon);
    //reset to last used measuring point
    lGPS_Lat0 = tempLat;
    lGPS_Lon0 = tempLon;

    //Serial.println(round(GPS.latitude * 100));
    //Serial.println(round(GPS.latitude * 100) * 60);

    //Serial.println(round(GPS.longitude * 100));
    //Serial.println(round(GPS.longitude * 100) * 60);
    //Serial.println("");
  }

}// end void fCalDistance()
////****************************************************************************************
unsigned int CosFix (long angle)
{
  long u = labs(angle) >> 16;
  u = (u * u * 6086) >> 24;
  return 246 - u;
}

long Diff (long deg1, long deg2)
{
  long result = deg2 - deg1;
  if (result > 180 * DEGREE) return result - 360 * DEGREE;
  else if (result < -180 * DEGREE) return result + 360 * DEGREE;
  else return result;
}
//calculates distance between 2 points . using lat/lon. ignores earth curve
//valid for distances of several 100K
//returns meters
//www.technoblogy.com/show?LNQ
unsigned int DistanceBetween (long lat1, long long1, long lat2, long long2)
{
  long dx = (Diff(long2, long1) * CosFix((lat1 + lat2) / 2)) / 256;
  long dy = Diff(lat2, lat1);
  unsigned long adx = labs(dx);
  unsigned long ady = labs(dy);
  unsigned long b = max(adx, ady);
  unsigned long a = min(adx, ady);
  if (b == 0) return 0;
  return 95 * (b + (110 * a / b * a + 128) / 256) / 512;
}
////*****************************************************************************************
////long fDegreeToDecimal(float floatDegree)
////{
////  return floatDegree  * 60 * 1000;
////}
void StartStopDistanceCounter()
{
  if (lGPS_Lat0 == 0)
  {
    //save starting lat n lon if needed
    lGPS_Lat0 = round(GPS.latitude * 100) * 60;
    lGPS_Lon0 = round(GPS.longitude * 100) * 60;

  }
}
//
void ResetDistanceCounter()
{

  //byteUpdateDistanceInterval = 0;
  lDistance = 0;
  lGPS_Lat0 = 0;
  lGPS_Lon0 = 0;
}//end  void SetResetDistanceCounter()

The more frequent the updates the better the accuracy of the distance moved. The calculation is based on straight line travel. To plot the output creates a stair step instead of a curved line.


The code will work with an Uno but a Mega is better. The code works really well with a Due, STM32 Bluepill, and ESP32.

This works on the Earth's equator, but nowhere else.

#define Radius 0.00003 // Alarm Radius = 3.3 m

Please do not submit multiple posts on the same topic.

srnet:
Using the first fix as the location is not a good idea, you likley need to wait a good minute or more for the location to stabilise. And then detecting a movement of as little as 3.3m might be ambitious. Variations of 3-10m in position are not unusual for a stationary GPS.

Also depends where the GPS actually is, what is the application ?

Hello from Athens, Greece
thank you for your answers.
I got my post removed because of submitting "multible posts" (gps geofence - compile error - #16 by jremington - Programming Questions - Arduino Forum) on the same issue so I will continue here.

I am trying to set save initial GPS data in order to compare it with current subsequent location

you suggested the boolian method but I cannot see how this works. I need the saved coords in order to display them later on the web. I would like to build something for marine applications

will this work?

float initialGPSlock()[color=#222222][/color]
{[color=#222222][/color]
[color=#222222][/color]
[color=#222222][/color]
  while (Serial1.available() > 0) {[color=#222222][/color]
    gps.encode(Serial1.read());[color=#222222][/color]
  }[color=#222222][/color]
[color=#222222][/color]
  if ( gps.satellites.value() >= 5 && gps.hdop.hdop() <= 5 && gps.sentencesWithFix() > 5) {[color=#222222][/color]
[color=#222222][/color]
    float initialLat;[color=#222222][/color]
    float initialLng;[color=#222222][/color]
[color=#222222][/color]
    initialLat = (gps.location.lat(), 6);[color=#222222][/color]
    initialLng = (gps.location.lng(), 6);[color=#222222][/color]
  }[color=#222222][/color]
[color=#222222][/color]
  return  initialLat;  initialLng;[color=#222222][/color]
}

jremington:
Please do not submit multiple posts on the same topic.

I am sorry for that.
You may delete the post if you wish.
I will continue from my older post.
Thank you

Please edit your post and repost the code. In the Arduino IDE, hit CTRL-t to properly format the code, then select all and copy/paste. Always post ALL the code.

This is wrong. Get rid of the outer parentheses and the ",6":

initialLat = (gps.location.lat(), 6);

Rather than delete the topic with a duplicate question (and replies) they have been merged here