autonomous car using arduino, 6m neo gps, hmc5883l compass

hi, I’m using tinygpsplus library.I’m making a project “gps controlled rc car ” using ublox neo 6m gps sensor and hmc5883l compass. my problem is that when I connect the arduino with computer everything works fine and gets gps coordinate and compass heading bt when I disconnected usb and connect with 9v battery to arduino, the car does move. rx signal receiving and tx not indicating.when I connect with usb to arduino, the car moving. I’m also using hmc5883l library. and not using this function “while(!serial). in the loop I’m using “while(ss.available ()>o)”. what’s happening please help me Your comment is awaiting moderation.

Please post your whole program and a schematic of your circuit…
Are you sure that the 9V battery (PP3 ?) can deliver the current required ?
How is the car powered ?

Your comment is awaiting moderation.

Wuh?

Your account is pending suspension for cross-posting.

DO NOT CROSS-POST, CROSS-POSTING WASTES TIME.

Duplicate deleted.

#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <HMC5883L.h>
#include <math.h>
/*
   This sample sketch demonstrates the normal use of a TinyGPS++ (TinyGPSPlus) object.
   It requires the use of SoftwareSerial, and assumes that you have a
   4800-baud serial GPS device hooked up on pins 4(rx) and 3(tx).
*/
static const int RXPin = 0, TXPin = 1;
static const uint32_t GPSBaud = 9600;
double compass_headingDegrees;
int L1=4;
int L2=5;
int L3=6;
int L4=7;


// The TinyGPS++ object
TinyGPSPlus gps;
// the HMC5883L object
HMC5883L compass;

// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);

void setup()
{
  Serial.begin(115200);
  ss.begin(GPSBaud);
  pinMode(13,OUTPUT);
  pinMode(L1,OUTPUT);
   pinMode(L2,OUTPUT);
  pinMode(L3,OUTPUT);
  pinMode(L4,OUTPUT);
  // Set measurement range
  compass.setRange(HMC5883L_RANGE_1_3GA);

  // Set measurement mode
  compass.setMeasurementMode(HMC5883L_CONTINOUS);

  // Set data rate
  compass.setDataRate(HMC5883L_DATARATE_30HZ);

  // Set number of samples averaged
  compass.setSamples(HMC5883L_SAMPLES_8);

  // Set calibration offset. See HMC5883L_calibration.ino
  compass.setOffset(0, 0);
}

void loop()
{
  // This sketch displays information every time a new sentence is correctly encoded.
  while (ss.available() > 0)
    if (gps.encode(ss.read()))
    gps_track();
  if (millis() > 5000 && gps.charsProcessed() < 10)
  {
    Serial.println(F("No GPS detected: check wiring."));
    while(true);
  }

}

void gps_track()
{
    //*******************compasss data ****************************//
   Vector norm = compass.readNormalize();

  // Calculate heading
  double heading = atan2(norm.YAxis, norm.XAxis);

  // Set declination angle on your location and fix heading
  // You can find your declination on: http://magnetic-declination.com/
  // (+) Positive or (-) for negative
  // For Bytom / Poland declination angle is 4'26E (positive)
  // Formula: (deg + (min / 60.0)) / (180 / M_PI);
  double declinationAngle = (4.0 + (26.0 / 60.0)) / (180 / M_PI);
  heading += declinationAngle;

  // Correct for heading < 0deg and heading > 360deg
  if (heading < 0)
  {
    heading += 2 * PI;
  }

  if (heading > 2 * PI)
  {
    heading -= 2 * PI;
  }

  // Convert to degrees
 compass_headingDegrees = heading * 180/M_PI; 
  if (gps.location.isValid())
  {  
    double gps_headingDeg;
    double distance_cal;
    double headingRad;
    double long1=gps.location.lng() ;
    double lat1= gps.location.lat();
    double long2=87.2345  ;
    double lat2=22.1167   ;
    double diflong;
    double diflat;
    double a;
    double c;
    double error;
    lat1=radians(lat1);
    long1=radians(long1);
    lat2=radians(lat2);
    long2=radians(long2);
     diflong=long1-long2;
    diflat=lat1-lat2;
    headingRad=atan2((sin(diflong)*cos(lat2)),((cos(lat1)*sin(lat2))-(sin(lat1)*cos(lat2)*cos(diflong))));
    gps_headingDeg=(180*headingRad)/3.14159;
    
    if(gps_headingDeg>0){
     gps_headingDeg=gps_headingDeg;
    }else{
      gps_headingDeg=gps_headingDeg+360;
    }
      
      error =(gps_headingDeg-compass_headingDegrees);
      if(error < -180){
        error=error+360;
      
      }
      if(error>180){
        error =error-360;
     
     
    }
    
    //*******distance calculation
    a=(sin(diflat/2)*sin(diflat/2))+(cos(lat1)*cos(lat2)*(sin(diflong/2)*sin(diflong/2)));
    c=2*atan2(sqrt(a),sqrt(1-a));
    distance_cal=c*6371000.0;// gives in meters
    //////////****************************error calculation
      Serial.print("gps heading");
      Serial.println(gps_headingDeg);
      Serial.print("magnetic heading");
      Serial.println(compass_headingDegrees);
          Serial.print("error");
      Serial.println(error);
        Serial.print("distance");
      Serial.println(distance_cal);
        bool condition=((compass_headingDegrees-10)<gps_headingDeg)&&((compass_headingDegrees+10)>gps_headingDeg);
        if(condition){
         
          fwr();
        }else{
          if(error>10){
            rgt();
          }
          if(error<-10){
            left();
          }
        }
  }
}
void fwr()
{
  digitalWrite(L1,HIGH);
  digitalWrite(L2,LOW);
  digitalWrite(L3,HIGH);
  digitalWrite(L4,LOW);
}
void left(){
   digitalWrite(L1,LOW);
  digitalWrite(L2,HIGH);
  digitalWrite(L3,HIGH);
  digitalWrite(L4,LOW);
}
void rgt(){
   digitalWrite(L1,HIGH);
  digitalWrite(L2,LOW);
  digitalWrite(L3,LOW);
  digitalWrite(L4,HIGH);
}

plz help me what's going wrong.this is my final year project

the car is powered by 2 9v battery. one for arduino and other car motors... I'm using l293d motor diver ic for providing left or right turn.

Please edit your post and add code tags.

9V batteries are useless for motors.

anything wrong in my plz… mention it

surajit11111995: anything wrong in my plz... mention it

No code tags, inadequate power supply.

in arduino 9v to 5v voltage regulator which converts 5v.the l293d motor diver ic have two supply one for own ic 5v and other for motors. here I am using 9v for motor

If you are using small 9V batteries with snap on connections on top, they can supply only limited current for a short period of time.

have two supply one for own ic 5v and other for motors.

Have they got a common GND connection ?

yes, they have common ground.. 9v using just test the car further I will use 9v rechargeable battery.. motor are small voltage rating 9v.... plz help me what's wrong my code.. when I opened the arduino ide serial port they work fine.everything give reading... bt when I connected 9v that not working. it's possible the arduino needs to communicate to arduino ide to open serial port and start gps and compass and when I connected external supply the arduino don't have usb connection to communicate with arduino ide... that's possible?? that's cause the prb?