interface gps with motors

Im using the follwoing code which exctrat th gps data

#include <NewSoftSerial.h>
#include <TinyGPS.h>

/* This sample code demonstrates the normal use of a TinyGPS object.
   It requires the use of NewSoftSerial, and assumes that you have a
   4800-baud serial GPS device hooked up on pins 2(rx) and 3(tx).
*/

TinyGPS gps;
NewSoftSerial nss(2, 3);

void gpsdump(TinyGPS &gps);
bool feedgps();
void printFloat(double f, int digits = 2);

void setup()
{
  Serial.begin(115200);
  nss.begin(4800);
  
  Serial.print("Testing TinyGPS library v. "); Serial.println(TinyGPS::library_version());
  Serial.println("by Mikal Hart");
  Serial.println();
  Serial.print("Sizeof(gpsobject) = "); Serial.println(sizeof(TinyGPS));
  Serial.println();
}

void loop()
{
  bool newdata = false;
  unsigned long start = millis();

  // Every 5 seconds we print an update
  while (millis() - start < 5000)
  {
    if (feedgps())
      newdata = true;
  }
  
  if (newdata)
  {
    Serial.println("Acquired Data");
    Serial.println("-------------");
    gpsdump(gps);
    Serial.println("-------------");
    Serial.println();
  }
}

void printFloat(double number, int digits)
{
  // Handle negative numbers
  if (number < 0.0)
  {
     Serial.print('-');
     number = -number;
  }

  // Round correctly so that print(1.999, 2) prints as "2.00"
  double rounding = 0.5;
  for (uint8_t i=0; i<digits; ++i)
    rounding /= 10.0;
  
  number += rounding;

  // Extract the integer part of the number and print it
  unsigned long int_part = (unsigned long)number;
  double remainder = number - (double)int_part;
  Serial.print(int_part);

  // Print the decimal point, but only if there are digits beyond
  if (digits > 0)
    Serial.print("."); 

  // Extract digits from the remainder one at a time
  while (digits-- > 0)
  {
    remainder *= 10.0;
    int toPrint = int(remainder);
    Serial.print(toPrint);
    remainder -= toPrint; 
  } 
}

void gpsdump(TinyGPS &gps)
{
  long lat, lon;
  float flat, flon;
  unsigned long age, date, time, chars;
  int year;
  byte month, day, hour, minute, second, hundredths;
  unsigned short sentences, failed;

  gps.get_position(&lat, &lon, &age);
  Serial.print("Lat/Long(10^-5 deg): "); Serial.print(lat); Serial.print(", "); Serial.print(lon); 
  Serial.print(" Fix age: "); Serial.print(age); Serial.println("ms.");
  
  feedgps(); // If we don't feed the gps during this long routine, we may drop characters and get checksum errors

  gps.f_get_position(&flat, &flon, &age);
  Serial.print("Lat/Long(float): "); printFloat(flat, 5); Serial.print(", "); printFloat(flon, 5);
  Serial.print(" Fix age: "); Serial.print(age); Serial.println("ms.");

  feedgps();

  gps.get_datetime(&date, &time, &age);
  Serial.print("Date(ddmmyy): "); Serial.print(date); Serial.print(" Time(hhmmsscc): "); Serial.print(time);
  Serial.print(" Fix age: "); Serial.print(age); Serial.println("ms.");

  feedgps();

  gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
  Serial.print("Date: "); Serial.print(static_cast<int>(month)); Serial.print("/"); Serial.print(static_cast<int>(day)); Serial.print("/"); Serial.print(year);
  Serial.print("  Time: "); Serial.print(static_cast<int>(hour)); Serial.print(":"); Serial.print(static_cast<int>(minute)); Serial.print(":"); Serial.print(static_cast<int>(second)); Serial.print("."); Serial.print(static_cast<int>(hundredths));
  Serial.print("  Fix age: ");  Serial.print(age); Serial.println("ms.");
  
  feedgps();

  Serial.print("Alt(cm): "); Serial.print(gps.altitude()); Serial.print(" Course(10^-2 deg): "); Serial.print(gps.course()); Serial.print(" Speed(10^-2 knots): "); Serial.println(gps.speed());
  Serial.print("Alt(float): "); printFloat(gps.f_altitude()); Serial.print(" Course(float): "); printFloat(gps.f_course()); Serial.println();
  Serial.print("Speed(knots): "); printFloat(gps.f_speed_knots()); Serial.print(" (mph): ");  printFloat(gps.f_speed_mph());
  Serial.print(" (mps): "); printFloat(gps.f_speed_mps()); Serial.print(" (kmph): "); printFloat(gps.f_speed_kmph()); Serial.println();

  feedgps();

  gps.stats(&chars, &sentences, &failed);
  Serial.print("Stats: characters: "); Serial.print(chars); Serial.print(" sentences: "); Serial.print(sentences); Serial.print(" failed checksum: "); Serial.println(failed);
}
  
bool feedgps()
{
  while (nss.available())
  {
    if (gps.encode(nss.read()))
      return true;
  }
  return false;
}

What i wiant to do if i got certain coordination (lat ) move the motors (for testing purpose), so where i have to put the following code in previous code??? becuase i tray to put it in void loop() but it does not work!!!

 if (lat>=53.00000 && lat<=54.00000 ) {
  

    motor.motor0Forward(0x30);
     
     
 }
 else 
 {
  
motor.motor1Reverse(0x30);
    
     
 }

so where i have to put the following code in previous code??? becuase i tray to put it in void loop() but it does not work!!!

Where did you try to put it?
Do you know for certain that lat is either 53 or 54? The lat variable is a long, so it would have to be one of those values in order to trigger the motor forward function.
How does it "not work!!!!"? Does the motor not move in the correct direction? Does it not move at all? Does the code not compile?

if (lat>=53.00000 && lat<=54.00000 ) {
  

    motor.motor0Forward(0x30);
    
    
 }
 else
 {

I put not specific (lat) i put rang to see the motor is running or not. i try it to put (53.00000 and 54.00000) or (5300000 and 5400000) but the motor workin in the (else condition) while the gps is read between the range i put.

It is difficult to understand you.

First make sure the motors work correctly by replacing it if condition with a simple reading of a button.
Then when you know the motors work have a print statement see what sort of values you are getting.

You don't post all your code so it is difficult to see where you might be going wrong. For example is lat defined as a float?
Try putting 53.5 in as a value.

Here:

  Serial.print("Lat/Long(10^-5 deg): "); Serial.print(lat); Serial.print(", "); Serial.print(lon);

you print out the values of lat and lon. But, you didn't tell us what those values are. So, whether lat is 53 or 54 is still unknown.

Because you defined lat to be a long, it can only hold integer values 0, 1, 2, ... 53, 54, ...

The motor is supposed to turn on when lat is greater than or equal 53 and less than or equal 54. There are only two integer values that fall in that range (53 and 54).

Show us two things. Where in the original code did you put the code to turn the motor on? What output is being printed?

You can in the follwoing code where i put the if statement

 if (lat>=5300000) {


    motor.motor0Forward(0x05);


 }
 else
 {

motor.motor1Reverse(0x05);


 }

acuttly im reading value of gps (lat) > 5300000 , so the motor should run !!!

#include <CompactQik2s9v1.h>
#include <NewSoftSerial.h>
#include <TinyGPS.h>
#include <Wire.h>

#define rxPin 5
#define txPin 1
#define rstPin 2

TinyGPS gps;
NewSoftSerial nss(4, 3);
CompactQik2s9v1 motor = CompactQik2s9v1(&nss,rstPin);
  float flat, flon, x2lat, x2lon;
void gpsdump(TinyGPS &gps);
bool feedgps();
void printFloat(double f, int digits = 2);

void setup()
{
  Serial.begin(115200);
  nss.begin(4800);
  delay(600);
  motor.begin();

  
}

void loop()
{
  bool newdata = false;
  unsigned long start = millis();

  // Every fourth of a second we print an update
  while (millis() - start < 250)
  {
    if (feedgps())
      newdata = true;
  }
  
  if (newdata)
  {
    Serial.println("Acquired Data");
    Serial.println("-------------");
    gpsdump(gps);
    Serial.println("-------------");
    Serial.println();
  }
}

void printFloat(double number, int digits)
{
  // Handle negative numbers
  if (number < 0.0)
  {
     Serial.print('-');
     number = -number;
  }

  // Round correctly so that print(1.999, 2) prints as "2.00"
  double rounding = 0.5;
  for (uint8_t i=0; i<digits; ++i)
    rounding /= 10.0;
  
  number += rounding;

  // Extract the integer part of the number and print it
  unsigned long int_part = (unsigned long)number;
  double remainder = number - (double)int_part;
  Serial.print(int_part);

  // Print the decimal point, but only if there are digits beyond
  if (digits > 0)
    Serial.print("."); 

  // Extract digits from the remainder one at a time
  while (digits-- > 0)
  {
    remainder *= 10.0;
    int toPrint = int(remainder);
    Serial.print(toPrint);
    remainder -= toPrint; 
  } 
  

}

void gpsdump(TinyGPS &gps)
{
  long lat, lon;

  unsigned long age, date, time, chars;
  unsigned short sentences, failed;

  gps.get_position(&lat, &lon, &age);
  Serial.print("Lat/Long(10^-5 deg): "); Serial.print(lat); Serial.print(", "); Serial.print(lon); 
  Serial.print(" Fix age: "); Serial.print(age); Serial.println("ms.");
    if (lat>=5300000) {
  

    motor.motor0Forward(0x05);
     
     
 }
 else 
 {
  
motor.motor1Reverse(0x05);
    
     
 }
  
  feedgps(); // If we don't feed the gps during this long routine, we may drop characters and get checksum errors

  gps.f_get_position(&flat, &flon, &age);
  Serial.print("Lat/Long(float): "); printFloat(flat, 5); Serial.print(", "); printFloat(flon, 5);
  Serial.print(" Fix age: "); Serial.print(age); Serial.println("ms.");

  feedgps();

  gps.stats(&chars, &sentences, &failed);
  Serial.print("Stats: characters: "); Serial.print(chars); Serial.print(" sentences: "); Serial.print(sentences); Serial.print(" failed checksum: "); Serial.println(failed);

}
  
bool feedgps()
{
  while (nss.available())
  {
    if (gps.encode(nss.read()))
      return true;
  }
  return false;
}

Is it safe to assume that you KNOW that the motor works and is wired up correctly? That is, have you a simple sketch that just turns the motor on forward for a period of time, then stops it, and then runs it in reverse for a period of time?

And, you still haven't shown any output...

I tested the motors they are ok, the porblem was one of serial configration, i want to operate one motor if the robot in defined coordination and if not both motors working (for testing) , the problem is one motor working all the time even if i change the position, down is the code im using, i think maybe i put the (if statemnetn in wrong place) anybody help me please…

#include <CompactQik2s9v1.h>
#include <NewSoftSerial.h>
#include <TinyGPS.h>
#include <Wire.h>

#define rxPin 5
#define txPin 1
#define rstPin 2

TinyGPS gps;
NewSoftSerial nss(4, 3);
NewSoftSerial mySerial =  NewSoftSerial(rxPin, txPin);
CompactQik2s9v1 motor = CompactQik2s9v1(&mySerial,rstPin);
  float flat, flon, x2lat, x2lon;
void gpsdump(TinyGPS &gps);
bool feedgps();
void printFloat(double f, int digits = 2);

void setup()
{
  mySerial.begin(9600);
  nss.begin(4800);
  delay(600);
  motor.begin();

  
}

void loop()
{
  bool newdata = false;
  unsigned long start = millis();

  // Every fourth of a second we print an update
  while (millis() - start < 250)
  {
    if (feedgps())
      newdata = true;
  }
  
  if (newdata)
  {
    Serial.println("Acquired Data");
    Serial.println("-------------");
    gpsdump(gps);
    
    Serial.println("-------------");
    Serial.println();
  }
  
}

void printFloat(double number, int digits)
{
  // Handle negative numbers
  if (number < 0.0)
  {
     Serial.print('-');
     number = -number;
  }

  // Round correctly so that print(1.999, 2) prints as "2.00"
  double rounding = 0.5;
  for (uint8_t i=0; i<digits; ++i)
    rounding /= 10.0;
  
  number += rounding;

  // Extract the integer part of the number and print it
  unsigned long int_part = (unsigned long)number;
  double remainder = number - (double)int_part;
  Serial.print(int_part);

  // Print the decimal point, but only if there are digits beyond
  if (digits > 0)
    Serial.print("."); 

  // Extract digits from the remainder one at a time
  while (digits-- > 0)
  {
    remainder *= 10.0;
    int toPrint = int(remainder);
    Serial.print(toPrint);
    remainder -= toPrint; 
  } 
  

}

void gpsdump(TinyGPS &gps)
{
  long lat, lon;

  unsigned long age, date, time, chars;
  unsigned short sentences, failed;

  gps.get_position(&lat, &lon, &age);
  Serial.print("Lat/Long(10^-5 deg): "); Serial.print(lat); Serial.print(", "); Serial.print(lon); 
  Serial.print(" Fix age: "); Serial.print(age); Serial.println("ms.");
     
     
     if (((lat>=5378958)||(lat<=5378960))&&(lon>=-177364 || lon<=-177358)) {
  

    motor.motor1Forward(0x05);
       
 }
 
 else
 {
   motor.motor0Forward(0x05);
   motor.motor1Forward(0x05);

 }
   
  
  
  
  feedgps(); // If we don't feed the gps during this long routine, we may drop characters and get checksum errors

  gps.f_get_position(&flat, &flon, &age);
  Serial.print("Lat/Long(float): "); printFloat(flat, 5); Serial.print(", "); printFloat(flon, 5);
  Serial.print(" Fix age: "); Serial.print(age); Serial.println("ms.");

  feedgps();

  gps.stats(&chars, &sentences, &failed);
  Serial.print("Stats: characters: "); Serial.print(chars); Serial.print(" sentences: "); Serial.print(sentences); Serial.print(" failed checksum: "); Serial.println(failed);

}
  
bool feedgps()
{
  while (nss.available())
  {
    if (gps.encode(nss.read()))
      return true;
  }
  return false;
}
Serial.print("Lat/Long(10^-5 deg): "); Serial.print(lat); Serial.print(", "); Serial.print(lon);
  Serial.print(" Fix age: "); Serial.print(age); Serial.println("ms.");
    
    
     if (((lat>=5378958)||(lat<=5378960))&&(lon>=-177364 || lon<=-177358)) {
  

    motor.motor1Forward(0x05);
      
 }

 else
 {
   motor.motor0Forward(0x05);
   motor.motor1Forward(0x05);

 }

Other than readability, there is nothing apparently wrong with this code. However, we have no idea where you are, so, we can’t tell if one motor or two should turn on.

Add some serial print statements inside each block where you turn a motor on.

Then, show us the output. This guessing is getting old.

I make many trials today, but still the problem same, i put the code inside the loop but only the first condition work, if i move somewhere elese the other (else) not working!!!!

im want simple code tell the motors run if the robot in certain corrdinations and if not stop the motors. please if you can help me in this.

May be im using two Newsoftserial??? thats why is not wroking proprly??

im want simple code tell the motors run if the robot in certain corrdinations and if not stop the motors. please if you can help me in this.

You already have that. That it doesn’t work the way you want means that the coordinates you are using are wrong.

May be im using two Newsoftserial??? thats why is not wroking proprly??

Are you or aren’t you using NewSoftSerial? That is not the source of your problem.

I’ll make one last attempt to help.

Show us the output.

Thank you PaulS for keeping touch with me, i found the problem, for each if statment i have to define two motors states like

motor.motor1Forward(0x05);
motor.motor0Forward(0x05);

My mistake was im just define one motor state (so i did not metion what should be happening for each of two motors each time, the motors will run as what was before.