Go Down

Topic: sending data using start and end makers with packet identifiers (Read 489 times) previous topic - next topic

alexmunala

The auto-format does not itself give you success. But if you post the code after using Auto Format I will find it much easier to help you.

The whole point of Auto Format is to make code easier to read.


Also (referring to my Reply #10) you have not explained what you want to achieve so that I can understand where in the program the sending code should go.

...R
the program after AutoFormat is here below
Code: [Select]
//Connect with pin 18 and 19
#include <TinyGPS++.h>
//long   lat,lon; // create variable for latitude and longitude object
float lat, lon;
TinyGPSPlus gps; // create gps object
struct GeoTurn {
  float lat;
  float lon;
};
struct GeoStop {
  float lat;
  float lon;
};
#define GPS_UPDATE_INTERVAL 1000
void setup() {
  Serial.begin(57600); // connect serial
  Serial.println("The GPS Received Signal:");
  Serial1.begin(9600); // connect gps sensor
  Serial2.begin(9600); // connect gps sensor

}

void loop()
{
  GeoTurn checkGPS() {
    unsigned long start = millis();
    while (millis() - start < GPS_UPDATE_INTERVAL) {
      // If we recieved new location then take the coordinates and pack them into a struct
      if (feedgps())
        return gpsdump();
    }


    GeoLoc waypointLoc;
    waypointLoc.lat = 0.0;
    waypointLoc.lon = 0.0;

    return waypointLoc;
  }


  // Get and process GPS data
  GeoTurn gpsdump()
  {
    GeoLoc waypointLoc;
    waypointLoc.lat = gps.location.lat();
    waypointLoc.lon = gps.location.lng();

    Serial.print("Position: ");

    //Latitude
    Serial.print("Latitude: ");
    Serial.print(lat, 7);

    Serial.print(",");

    //Longitude
    Serial.print("Longitude: ");
    Serial.println(lon, 7);
    return waypointLoc;
  }
  bool feedgps()
  {
    while (Serial1.available() > 0) {
      if (gps.encode(Serial1.read()))
        return true;
    }
    return false;
  }

  GeoStop checkGPS()
  {
    unsigned long start = millis();
    while (millis() - start < GPS_UPDATE_INTERVAL) {
      // If we recieved new location then take the coordinates and pack them into a struct
      if (feedgps())
        return gpsdump();
    }


    GeoLoc destinationLoc;
    destinationLoc.lat = 0.0;
    destinationLoc.lon = 0.0;

    return destinationLoc;
  }
  GeoStop gpsdump()
  {
    GeoLoc destinationLoc;
    destinationLoc.lat = gps.location.lat();
    destinationLoc.lon = gps.location.lng();

    Serial.print("Position: ");

    //Latitude
    Serial.print("Latitude: ");
    Serial.print(lat, 7);

    Serial.print(",");

    //Longitude
    Serial.print("Longitude: ");
    Serial.println(lon, 7);
    return destinationLoc;
  }

  // Feed data as it becomes available

  //connect pin 16 and 17

  // Feed data as it becomes available
  bool feedgps()
  {
    while (Serial2.available() > 0) {
      if (gps.encode(Serial2.read()))
        return true;
    }
    return false;
  }
}



what I need to achieve:
I need to get the two GPS coordinates ( the GPS modules are attached to one Mega 2560, separated around 5m apart), send them via Bluetooth module to another module to be used as a waypoint and destination on an autonomous car.

TheMemberFormerlyKnownAsAWOL

Code: [Select]
void loop()
{
  GeoTurn checkGPS() {
Right there.
You can't embed a function definition inside another function
Please don't PM technical questions - post them on the forum, then everyone benefits/suffers equally

alexmunala


TheMemberFormerlyKnownAsAWOL

#18
Feb 15, 2020, 02:30 pm Last Edit: Feb 15, 2020, 02:31 pm by TheMemberFormerlyKnownAsAWOL
Don't embed a function inside another.
Move checkGPS outside of loop.

In all the code you've read or written so far you have never seen one function definition embedded in another, so why did you think it was OK here?

sp. "you"
Please don't PM technical questions - post them on the forum, then everyone benefits/suffers equally

alexmunala

Code: [Select]
//Connect with pin 18 and 19
#include <TinyGPS++.h>
//long   lat,lon; // create variable for latitude and longitude object
float lat, lon;
TinyGPSPlus gps; // create gps object
struct GeoTurn {
  float lat;
  float lon;
};
struct GeoStop {
  float lat;
  float lon;
};
#define GPS_UPDATE_INTERVAL 1000
void setup() {
  Serial.begin(57600); // connect serial
  Serial.println("The GPS Received Signal:");
  Serial1.begin(9600); // connect gps sensor
  Serial2.begin(9600); // connect gps sensor

}
void loop()
{
  while (Serial2.available() > 0) {
    if (gps.encode(Serial2.read()))
      return true;


    GeoTurn waypointLoc;
    waypointLoc.lat = 0.0;
    waypointLoc.lon = 0.0;

    return waypointLoc;
    // Get and process GPS data

    waypointLoc.lat = gps.location.lat();
    waypointLoc.lon = gps.location.lng();

    Serial.print("Position: ");

    //Latitude
    Serial.print("Latitude: ");
    Serial.print(lat, 7);

    Serial.print(",");

    //Longitude
    Serial.print("Longitude: ");
    Serial.println(lon, 7);
    return waypointLoc;
  }

  while (Serial2.available() > 0) {
    if (gps.encode(Serial2.read()))
      return true;

    GeoStop destinationLoc;
    destinationLoc.lat = 0.0;
    destinationLoc.lon = 0.0;

    return destinationLoc;

    destinationLoc.lat = gps.location.lat();
    destinationLoc.lon = gps.location.lng();

    Serial.print("Position: ");

    //Latitude
    Serial.print("Latitude: ");
    Serial.print(lat, 7);

    Serial.print(",");

    //Longitude
    Serial.print("Longitude: ");
    Serial.println(lon, 7);
    return destinationLoc;
  }


}


I've eliminated the CheckGPS function and done some editings...
Above is a program i have edited..please go through it
i have Auto Formatted and am receiving an error that (return statement with a value, in function returning void [-fpermisive]
what could be the issue?

TheMemberFormerlyKnownAsAWOL

#20
Feb 15, 2020, 02:57 pm Last Edit: Feb 15, 2020, 03:17 pm by TheMemberFormerlyKnownAsAWOL
Code: [Select]
return waypointLoc;
loop() is a void function; it makes no sense to return a value from it.
Edit: you've got multiple returns, not even of the same types.

Quote
I've eliminated the CheckGPS function
Why?
Please don't PM technical questions - post them on the forum, then everyone benefits/suffers equally

Robin2

Look at this version of loop() and your functions.  Note how loop() is now empty and all the functions are defined outside of it.

Next thing is to figure out the code you need to put into loop() to use your functions. I suggest you first get it to show the data on the Serial Monitor from the code in loop() rather than from the individual functions. Comment out all the printing lines within the functions.


Code: [Select]
void loop() {
   
}


GeoTurn checkGPS() {
    unsigned long start = millis();
    while (millis() - start < GPS_UPDATE_INTERVAL) {
        // If we recieved new location then take the coordinates and pack them into a struct
        if (feedgps())
            return gpsdump();
    }


    GeoLoc waypointLoc;
    waypointLoc.lat = 0.0;
    waypointLoc.lon = 0.0;

    return waypointLoc;
}


// Get and process GPS data
GeoTurn gpsdump()
{
    GeoLoc waypointLoc;
    waypointLoc.lat = gps.location.lat();
    waypointLoc.lon = gps.location.lng();

    Serial.print("Position: ");

    //Latitude
    Serial.print("Latitude: ");
    Serial.print(lat, 7);

    Serial.print(",");

    //Longitude
    Serial.print("Longitude: ");
    Serial.println(lon, 7);
    return waypointLoc;
}
bool feedgps()
{
    while (Serial1.available() > 0) {
        if (gps.encode(Serial1.read()))
            return true;
    }
    return false;
}

GeoStop checkGPS()
{
    unsigned long start = millis();
    while (millis() - start < GPS_UPDATE_INTERVAL) {
        // If we recieved new location then take the coordinates and pack them into a struct
        if (feedgps())
            return gpsdump();
    }


    GeoLoc destinationLoc;
    destinationLoc.lat = 0.0;
    destinationLoc.lon = 0.0;

    return destinationLoc;
}
GeoStop gpsdump()
{
    GeoLoc destinationLoc;
    destinationLoc.lat = gps.location.lat();
    destinationLoc.lon = gps.location.lng();

    Serial.print("Position: ");

    //Latitude
    Serial.print("Latitude: ");
    Serial.print(lat, 7);

    Serial.print(",");

    //Longitude
    Serial.print("Longitude: ");
    Serial.println(lon, 7);
    return destinationLoc;
}

// Feed data as it becomes available

//connect pin 16 and 17

// Feed data as it becomes available
bool feedgps()
{
    while (Serial2.available() > 0) {
        if (gps.encode(Serial2.read()))
            return true;
    }
    return false;
}



...R
Two or three hours spent thinking and reading documentation solves most programming problems.

alexmunala

Look at this version of loop() and your functions.  Note how loop() is now empty and all the functions are defined outside of it.

Next thing is to figure out the code you need to put into loop() to use your functions. I suggest you first get it to show the data on the Serial Monitor from the code in loop() rather than from the individual functions. Comment out all the printing lines within the functions.


Code: [Select]
void loop() {
    
}


GeoTurn checkGPS() {
    unsigned long start = millis();
    while (millis() - start < GPS_UPDATE_INTERVAL) {
        // If we recieved new location then take the coordinates and pack them into a struct
        if (feedgps())
            return gpsdump();
    }


    GeoLoc waypointLoc;
    waypointLoc.lat = 0.0;
    waypointLoc.lon = 0.0;

    return waypointLoc;
}


// Get and process GPS data
GeoTurn gpsdump()
{
    GeoLoc waypointLoc;
    waypointLoc.lat = gps.location.lat();
    waypointLoc.lon = gps.location.lng();

    Serial.print("Position: ");

    //Latitude
    Serial.print("Latitude: ");
    Serial.print(lat, 7);

    Serial.print(",");

    //Longitude
    Serial.print("Longitude: ");
    Serial.println(lon, 7);
    return waypointLoc;
}
bool feedgps()
{
    while (Serial1.available() > 0) {
        if (gps.encode(Serial1.read()))
            return true;
    }
    return false;
}

GeoStop checkGPS()
{
    unsigned long start = millis();
    while (millis() - start < GPS_UPDATE_INTERVAL) {
        // If we recieved new location then take the coordinates and pack them into a struct
        if (feedgps())
            return gpsdump();
    }


    GeoLoc destinationLoc;
    destinationLoc.lat = 0.0;
    destinationLoc.lon = 0.0;

    return destinationLoc;
}
GeoStop gpsdump()
{
    GeoLoc destinationLoc;
    destinationLoc.lat = gps.location.lat();
    destinationLoc.lon = gps.location.lng();

    Serial.print("Position: ");

    //Latitude
    Serial.print("Latitude: ");
    Serial.print(lat, 7);

    Serial.print(",");

    //Longitude
    Serial.print("Longitude: ");
    Serial.println(lon, 7);
    return destinationLoc;
}

// Feed data as it becomes available

//connect pin 16 and 17

// Feed data as it becomes available
bool feedgps()
{
    while (Serial2.available() > 0) {
        if (gps.encode(Serial2.read()))
            return true;
    }
    return false;
}



...R
ive changed to the way you changed the code leaving the loop() empty
i receive an error
[error: ambiguating new declaration of 'GeoStop checkGPS()'

 GeoStop checkGPS()

and

note: old declaration 'GeoTurn checkGPS()'

 GeoTurn checkGPS() {

]

TheMemberFormerlyKnownAsAWOL

It couldn't be clearer - you've got two checkGPS functions.
Please don't PM technical questions - post them on the forum, then everyone benefits/suffers equally

alexmunala

It couldn't be clearer - you've got two checkGPS functions.
WOW!!!!!!
it compilled after specifying GeoTurn checkGPS1  and GeoStop checkGPS2.

alexmunala

I can't make sense of the program in your Original Post because it is so poorly formatted - I find it impossible to see where parts begin and end. Please use the Auto Format tool to indent it consistently and re-post it in your next Reply.

If you want to send two values to be compatible with the style in the 3rd example in Serial Input Basics then do it like this
Code: [Select]
Serial.print('<');
Serial.print(waypointLoc.lat);
Serial.print(','); // a comma
Serial.print(waypointLoc.lon);
Serial.println('>');


You can do the same with SoftwareSerial

...R
i have tried and compilled the program.I now intend to place the bluetooth code to send the coordinates in the loop() .Do you think its appropriate? And how can i do that?

Code: [Select]
//Connect with pin 18 and 19
#include <TinyGPS++.h>
//long   lat,lon; // create variable for latitude and longitude object
float lat, lon;
TinyGPSPlus gps; // create gps object

struct GeoTurn {

  float lat;
  float lon;
};
struct GeoStop {
  float lat;
  float lon;
};

const byte maxDataLength = 80;
const byte numChars = 32;
char receivedChars[numChars];

boolean newData = false;

#define GPS_UPDATE_INTERVAL 1000

void setup() {
  Serial.begin(38400); // connect serial
  Serial.println("The GPS Received Signal:");
  Serial1.begin(115200); // connect gps sensor
  Serial2.begin(115200); // connect gps sensor
  Serial3.begin(57600);

}

GeoTurn checkGPS1() {
  unsigned long start = millis();
  while (millis() - start < GPS_UPDATE_INTERVAL) {
    // If we recieved new location then take the coordinates and pack them into a struct
    if (feedgps())
      return gpsdump1();
  }


  GeoTurn waypointLoc;
  waypointLoc.lat = 0.0;
  waypointLoc.lon = 0.0;

  return waypointLoc;
}


// Get and process GPS data
GeoTurn gpsdump1()
{
  GeoTurn waypointLoc;
  waypointLoc.lat = gps.location.lat();
  waypointLoc.lon = gps.location.lng();

  Serial.print("Position: ");

  //Latitude
  Serial.print("Latitude: ");
  Serial.print(lat, 7);

  Serial.print(",");

  //Longitude
  Serial.print("Longitude: ");
  Serial.println(lon, 7);
  return waypointLoc;
}

//connect pin 16 and 17
GeoStop checkGPS2()
{
  unsigned long start = millis();
  while (millis() - start < GPS_UPDATE_INTERVAL) {
    // If we recieved new location then take the coordinates and pack them into a struct
    if (feedgps())
      return gpsdump2();
  }


  GeoStop destinationLoc;
  destinationLoc.lat = 0.0;
  destinationLoc.lon = 0.0;

  return destinationLoc;
}
GeoStop gpsdump2()
{
  GeoStop destinationLoc;
  destinationLoc.lat = gps.location.lat();
  destinationLoc.lon = gps.location.lng();

  Serial.print("Position: ");

  //Latitude
  Serial.print("Latitude: ");
  Serial.print(lat, 7);

  Serial.print(",");

  //Longitude
  Serial.print("Longitude: ");
  Serial.println(lon, 7);
  return destinationLoc;
}

// Feed data as it becomes available



// Feed data as it becomes available
bool feedgps()
{
  while (Serial2.available() > 0) {
    if (gps.encode(Serial2.read()))
      return true;
  }
  return false;

  while (Serial1.available() > 0) {
    if (gps.encode(Serial1.read()))
      return true;
  }
  return false;
}

void loop() {

}


the bluetooth module is to be attached to Serial3 of the Mega 2560

jremington

Get the Bluetooth module working separately, and make sure you understand how it works, before attempting to add code to your current program.

Robin2

i have tried and compilled the program.I now intend to place the bluetooth code to send the coordinates in the loop() .Do you think its appropriate? And how can i do that?
It will be much easier to help if you make an attempt at the program yourself because then we will understand how you are thinking about the problem.

However, before bothering with sending data please do like I suggested in Reply #21 and write code in loop() to get the values that you will eventually be sending to the other Arduino and just display them on the Serial monitor.

...R

PS ... what I suggested in the second paragraph of this Reply is entirely consistent with what @jremington said in Reply #26
Two or three hours spent thinking and reading documentation solves most programming problems.

alexmunala

It will be much easier to help if you make an attempt at the program yourself because then we will understand how you are thinking about the problem.

However, before bothering with sending data please do like I suggested in Reply #21 and write code in loop() to get the values that you will eventually be sending to the other Arduino and just display them on the Serial monitor.

...R

PS ... what I suggested in the second paragraph of this Reply is entirely consistent with what @jremington said in Reply #26
Code: [Select]
//Connect with pin 18 and 19
#include <TinyGPS++.h>
//long   lat,lon; // create variable for latitude and longitude object
float lat, lng;
TinyGPSPlus gps; // create gps object

struct GeoTurn {

  float lat;
  float lng;
};
struct GeoStop {
  float lat;
  float lng;
};

#define GPS_UPDATE_INTERVAL 1000

void setup() {
  Serial.begin(38400); // connect serial
  Serial.println("The GPS Received Signal:");
  Serial1.begin(115200); // connect gps sensor
  Serial2.begin(115200); // connect gps sensor
  Serial3.begin(57600);

}

GeoTurn checkGPS1() {
  unsigned long start = millis();
  while (millis() - start < GPS_UPDATE_INTERVAL) {
    // If we recieved new location then take the coordinates and pack them into a struct
    if (feedgps())
      return gpsdump1();
  }


  GeoTurn waypointLoc;
  waypointLoc.lat = 0.0;
  waypointLoc.lng = 0.0;

  return waypointLoc;
}


// Get and process GPS data
GeoTurn gpsdump1()
{
  GeoTurn waypointLoc;
  waypointLoc.lat = gps.location.lat();
  waypointLoc.lng = gps.location.lng();

  Serial.print("Position: ");

  //Latitude
  Serial.print("Latitude: ");
  Serial.print(lat, 7);

  Serial.print(",");

  //Longitude
  Serial.print("Longitude: ");
  Serial.println(lng, 7);
  return waypointLoc;
}

//connect pin 16 and 17
GeoStop checkGPS2()
{
  unsigned long start = millis();
  while (millis() - start < GPS_UPDATE_INTERVAL) {
    // If we recieved new location then take the coordinates and pack them into a struct
    if (feedgps())
      return gpsdump2();
  }


  GeoStop destinationLoc;
  destinationLoc.lat = 0.0;
  destinationLoc.lng = 0.0;

  return destinationLoc;
}
GeoStop gpsdump2()
{
  GeoStop destinationLoc;
  destinationLoc.lat = gps.location.lat();
  destinationLoc.lng = gps.location.lng();

  Serial.print("Position: ");

  //Latitude
  Serial.print("Latitude: ");
  Serial.print(lat, 7);

  Serial.print(",");

  //Longitude
  Serial.print("Longitude: ");
  Serial.println(lng, 7);
  return destinationLoc;
}

// Feed data as it becomes available



// Feed data as it becomes available
bool feedgps()
{
  while (Serial2.available() > 0) {
    if (gps.encode(Serial2.read()))
      return true;
  }
  return false;

  while (Serial1.available() > 0) {
    if (gps.encode(Serial1.read()))
      return true;
  }
  return false;
}

void loop() {
GeoTurn waypointLoc;
  Serial3.print('<');
  Serial3.print(waypointLoc.lat);
  Serial3.print(','); // a comma
  Serial3.print(waypointLoc.lng);
  Serial3.println('>');

  delay(50);
 
GeoStop destinationLoc;
  Serial3.print('<');
  Serial3.print(destinationLoc.lat);
  Serial3.print(','); // a comma
  Serial3.print(destinationLoc.lng);
  Serial3.println('>');

}


done

alexmunala

Any thoughts about what I have done or any additional ideas.

Go Up