Not declared in this scope error

Ultimately, I am trying to program a robot that can follow my phone using GPS Latitude and Longitude(but this code is not completed yet as I am yet to program the motors).

For this code, I am trying to get the GPS Latitude and Longitude of a phone using the Blynk software which helps to connect the phone to arduino. Then I am trying to get the GPS Latitude and Longitude of the Arduino using a GPS NEO 6M. After that I calculate the distance between the 2 GPS points and the degrees I need to make my robot turn(using the bearings value) and face in the direction of the phone.

However, I am getting error: 'gps' was not declared in this scope.

The code is this:

#include <SoftwareSerial.h>
#include <BlynkSimpleSerialBLE.h>
#include <TinyGPS++.h>
#include <math.h>

#define TxD 8
#define RxD 9

SoftwareSerial serial_connection(0,1);
SoftwareSerial bluetoothSerial(TxD, RxD);
TinyGPSPlus arduino;

float lat2 = gps.getLat();
float lat1 = arduino.location.lat();
float lon2 = gps.getLon();
float lon1 = arduino.location.lng();


double latR1;
double latR2;
double lonR1;
double lonR2;
double dlon;
double dlat;
double a;
double e;
double d;
double R = 6371.00;
double toDegrees = 57.295779;

char sb[10];
char auth[] = "b3a6efffe4424417b71b3184fc24c61b";


BLYNK_WRITE(V2) 
{
  GpsParam gps(param);
  Serial.println("Received remote GPS: ");
  Serial.print(gps.getLat(), 7); 
  Serial.print(", "); 
  Serial.println(gps.getLon(), 7);
  
}


void setup()
{
  //calcDist();
  Serial.begin(9600);
  bluetoothSerial.begin(9600);
  Blynk.begin(bluetoothSerial, auth);
  serial_connection.begin(9600);
  Serial.println("GPS Start");
}


void loop()
{
  Blynk.run();
  
  while(serial_connection.available())
  {
    Serial.println("There is connection to GPS!");
    arduino.encode(serial_connection.read());
  }
  if(arduino.location.isUpdated())
  {
    Serial.println("Satellite Count:");
    Serial.println(arduino.satellites.value());
    Serial.println("Latitude:");
    Serial.println(arduino.location.lat(), 6);
    Serial.println("Longitude:");
    Serial.println(arduino.location.lng(), 6);
    Serial.println("Speed MPH:");
    Serial.println(arduino.speed.mph());
    Serial.println("Altitude Feet:");
    Serial.println(arduino.altitude.feet());
    Serial.println("");
  }
}

void calcDist()
{ 
  Serial.begin(9600);
  float delLat = abs(lat1-lat2)*111194.9;
  float delLong = 111194.9*abs(lon1-lon2)*cos(radians((lat1+lat2)/2));
  float distance = sqrt(pow(delLat,2)+pow(delLong,2));
  Serial.print("Distance to destination(KM): ");
  Serial.println(distance,7);

  lonR1 = lon1*(PI/180);
  lonR2 = lon2*(PI/180);
  latR1 = lat1*(PI/180);
  latR2 = lat2*(PI/180);
  
  dlon = lonR2 - lonR1;
  dlat = latR2 - latR1;

  double x = cos(latR2)*sin(lonR2-lonR1); 
  double y = cos(latR1)*sin(latR2)-sin(latR1)*cos(latR2)*cos(lonR2-lonR1);

  float brRad = atan2(x, y);
  float reqBear = toDegrees*brRad;
  
  Serial.print("Bearing(°): ");
  Serial.println(reqBear, 7);
}

I need help in making gps.getLat() and gps.getLon() in the correct scope so that I am able to access it without errors coming up.

The part of the code which has gps.getLat() and gps.getLon() is here:

float lat2 = gps.getLat();
float lat1 = arduino.location.lat();
float lon2 = gps.getLon();
float lon1 = arduino.location.lng();

and

BLYNK_WRITE(V2) 
{
  GpsParam gps(param);
  Serial.println("Received remote GPS: ");
  Serial.print(gps.getLat(), 7); 
  Serial.print(", "); 
  Serial.println(gps.getLon(), 7);
}

So once again, I need help in making(or declaring) gps.getLat() and gps.getLon() in the correct scope so that I am able to access it without errors coming up.

Would anyone be able to help me?

could you make gps global, e.g. define it before the call to gps.getLat()

GpsParam gps(param);
float lat2 = gps.getLat();
...

and remove all the other definitions of gps

horace:
could you make gps global, e.g. define it before the call to gps.getLat()

GpsParam gps(param);

float lat2 = gps.getLat();
...



and remove all the other definitions of gps

Yes I have tried to do so but it shows the following error:

GPS_final3:17: error: 'param' was not declared in this scope

GpsParam gps(param);

^

exit status 1
'param' was not declared in this scope

you need to define param before that statement

horace:
you need to define param before that statement

Ok, I just did it and this is the error now:

GPS_final3:19: error: request for member 'getLat' in 'gps', which is of non-class type 'GpsParam()'

float lat2 = gps.getLat();

^

GPS_final3:21: error: request for member 'getLon' in 'gps', which is of non-class type 'GpsParam()'

float lon2 = gps.getLon();

^

C:\Users\Ronith\Desktop\Capstone Project\GPS\FINAL\GPS_final3\GPS_final3.ino: In function 'void BlynkWidgetWrite2(BlynkReq&, const BlynkParam&)':

GPS_final3:45: error: request for member 'getLat' in 'gps', which is of non-class type 'GpsParam()'

Serial.print(gps.getLat(), 7);

^

GPS_final3:47: error: request for member 'getLon' in 'gps', which is of non-class type 'GpsParam()'

Serial.println(gps.getLon(), 7);

^

exit status 1
request for member 'getLat' in 'gps', which is of non-class type 'GpsParam()'

Time to post your code.

AWOL:
Time to post your code.

I have posted the code on the first post. Thanks.

Time to post your modified code.
Thanks.

This is the same code that has the same problems as pointed out here:
https://forum.arduino.cc/index.php?topic=518983.msg3537722#msg3537722

It's also a duplicate thread:
https://forum.arduino.cc/index.php?topic=518983.0

AWOL:
Time to post your modified code.
Thanks.

Oh sorry for my naivety.

Code:

#include <SoftwareSerial.h>
#include <BlynkSimpleSerialBLE.h>
#include <TinyGPS++.h>
#include <math.h>

#include "TinyGPS++.h" /* try to remove these 2 includes */
#include "SoftwareSerial.h"


#define TxD 8
#define RxD 9
#define param

SoftwareSerial serial_connection(0,1);
SoftwareSerial bluetoothSerial(TxD, RxD);
TinyGPSPlus arduino;

GpsParam gps(param);
float lat2 = gps.getLat();
float lat1 = arduino.location.lat();
float lon2 = gps.getLon();
float lon1 = arduino.location.lng();


double latR1;
double latR2;
double lonR1;
double lonR2;
double dlon;
double dlat;
double a;
double e;
double d;
double R = 6371.00;
double toDegrees = 57.295779;

char sb[10];
char auth[] = "b3a6efffe4424417b71b3184fc24c61b";


BLYNK_WRITE(V2) 
{
  GpsParam gps(param);
  Serial.println("Received remote GPS: ");
  Serial.print(gps.getLat(), 7); 
  Serial.print(", "); 
  Serial.println(gps.getLon(), 7);
  
}


void setup()
{
  //calcDist();
  Serial.begin(9600);
  bluetoothSerial.begin(9600);
  Blynk.begin(bluetoothSerial, auth);
  serial_connection.begin(9600);
  Serial.println("GPS Start");
}


void loop()
{
  Blynk.run();
  
  while(serial_connection.available())
  {
    Serial.println("There is connection to GPS!");
    arduino.encode(serial_connection.read());
  }
  if(arduino.location.isUpdated())
  {
    Serial.println("Satellite Count:");
    Serial.println(arduino.satellites.value());
    Serial.println("Latitude:");
    Serial.println(arduino.location.lat(), 6);
    Serial.println("Longitude:");
    Serial.println(arduino.location.lng(), 6);
    Serial.println("Speed MPH:");
    Serial.println(arduino.speed.mph());
    Serial.println("Altitude Feet:");
    Serial.println(arduino.altitude.feet());
    Serial.println("");
  }
}

void calcDist()
{ 
  Serial.begin(9600);
  float delLat = abs(lat1-lat2)*111194.9;
  float delLong = 111194.9*abs(lon1-lon2)*cos(radians((lat1+lat2)/2));
  float distance = sqrt(pow(delLat,2)+pow(delLong,2));
  Serial.print("Distance to destination(KM): ");
  Serial.println(distance,7);

  lonR1 = lon1*(PI/180);
  lonR2 = lon2*(PI/180);
  latR1 = lat1*(PI/180);
  latR2 = lat2*(PI/180);
  
  dlon = lonR2 - lonR1;
  dlat = latR2 - latR1;

  double x = cos(latR2)*sin(lonR2-lonR1); 
  double y = cos(latR1)*sin(latR2)-sin(latR1)*cos(latR2)*cos(lonR2-lonR1);

  float brRad = atan2(x, y);
  float reqBear = toDegrees*brRad;
  
  Serial.print("Bearing(°): ");
  Serial.println(reqBear, 7);
}

@horace
@AWOL
@gfvalvo

would you be able to help me?

GpsParam gps(param);
float lat2 = gps.getLat();

What do you expect the getLat() method to return here, before you've even called setup()?

AWOL:

GpsParam gps(param);

float lat2 = gps.getLat();



What do you expect the getLat() method to return here, before you've even called setup()?

I do not expect the getLat() to return anything here. I just want to state that lat2=gps.getLat()

I expect to recieve the GPS Latitude value of my phone by gps.getLat() from this following code

BLYNK_WRITE(V2) 
{
  GpsParam gps(param);
  Serial.println("Received remote GPS: ");
  Serial.print(gps.getLat(), 7); 
  Serial.print(", "); 
  Serial.println(gps.getLon(), 7);
  
}

I do not expect the getLat() to return anything here. I just want to state that lat2=gps.getLat()

So why bother calling the method?, before setup() has run?
Why not just leave lat2 with its default value of 0.0?

GPS_final3:17: error: 'gps' was not declared in this scope

float lat2 = gps.getLat();

^

GPS_final3:19: error: 'gps' was not declared in this scope

float lon2 = gps.getLon();

Please post all the error messages.

There is no point calling these functions before setup as been called.

AWOL:
Please post all the error messages.

There is no point calling these functions before setup as been called.

GPS_final3:17: error: 'gps' was not declared in this scope

float lat2 = gps.getLat();

^

GPS_final3:19: error: 'gps' was not declared in this scope

float lon2 = gps.getLon();

Please note that only this part of the code is causing errors:

float lat2 = gps.getLat();
float lat1 = arduino.location.lat();
float lon2 = gps.getLon();
float lon1 = arduino.location.lng();
BLYNK_WRITE(V2) 
{
  GpsParam gps(param);
  Serial.println("Received remote GPS: ");
  Serial.print(gps.getLat(), 7); 
  Serial.print(", "); 
  Serial.println(gps.getLon(), 7);
}
void calcDist()
{ 
  Serial.begin(9600);
  float delLat = abs(lat1-lat2)*111194.9;
  float delLong = 111194.9*abs(lon1-lon2)*cos(radians((lat1+lat2)/2));
  float distance = sqrt(pow(delLat,2)+pow(delLong,2));
  Serial.print("Distance to destination(KM): ");
  Serial.println(distance,7);

  lonR1 = lon1*(PI/180);
  lonR2 = lon2*(PI/180);
  latR1 = lat1*(PI/180);
  latR2 = lat2*(PI/180);
  
  dlon = lonR2 - lonR1;
  dlat = latR2 - latR1;

  double x = cos(latR2)*sin(lonR2-lonR1); 
  double y = cos(latR1)*sin(latR2)-sin(latR1)*cos(latR2)*cos(lonR2-lonR1);

  float brRad = atan2(x, y);
  float reqBear = toDegrees*brRad;
  
  Serial.print("Bearing(°): ");
  Serial.println(reqBear, 7);
}

I played the code without the void calcDist() and floats(lon1/lon2/lat1/lat2) and it worked.

I already pointed-out that calling those functions before setup() is called, is pointless, yet you're still doing it.
What happens when you change the definitions to this?

float lat2;
float lat1;
float lon2;
float lon1;

AWOL:
I already pointed-out that calling those functions before setup() is called, is pointless, yet you're still doing it.
What happens when you change the definitions to this?

float lat2;

float lat1;
float lon2;
float lon1;

This way, there is no error. The Arduino serial prints out the GPS lat/lon that I need.

However, I need to make

lat2 = gps.getLat();
lat1 = arduino.location.lat();
lon2 = gps.getLon();
lon1 = arduino.location.lng();

so that later on in void calcDist, the calculation gives me the reading(distance and bearing) that I need.

Is there a way to make
lat2 = gps.getLat();
lat1 = arduino.location.lat();
lon2 = gps.getLon();
lon1 = arduino.location.lng();
?