Reset arduino mega on power

I'm working on a device which takes inputs from a GPS chip and log the Latitude and longitudes in a SD card, everything is working fine when i connects the Arduino mega to the computer and checks the Serial monitor, But when i use battery pack to power the Arduino Mega then the problem comes, The system is not creating a File in SD card and also not logging the data, Here i need to manually press the Reset button on Arduino Mega to start the process from the beginning.

Here is my code

#include <TinyGPS++.h>
#include <SPI.h>
#include <SD.h>
#include <EEPROM.h>
int addr = 0;
TinyGPSPlus gps;
File dataFile;
char abc[32];
char def[32];
char aaa[32];
int dat = 0, v = 0;
float lat0 = 0.0, lat1 = 0.0, lng0 = 0.0, lng1 = 0.0, dis = 0.0;
String kk;
int LED = 5;
int LEDD = 6;
void setup()
{
  pinMode(LED, OUTPUT);
  pinMode(LEDD, OUTPUT);

  Serial.begin(9600);
  Serial2.begin(9600);
  pinMode(7, INPUT_PULLUP);
  if (!SD.begin(53))
  {
    Serial.println("Card failed");
    return;
  }
  Serial.println("card initialized.");
  v = EEPROM.read(0);
  v++;
  EEPROM.write(0, v);
}


void loop()
{

  printInt(gps.satellites.value(), gps.satellites.isValid(), 5);
  printInt(gps.hdop.value(), gps.hdop.isValid(), 5);
  printFloat(gps.location.lat(), gps.location.isValid(), 11, 6);
  printFloat(gps.location.lng(), gps.location.isValid(), 12, 6);
  printDateTime(gps.date, gps.time);
  printFloat(gps.altitude.meters(), gps.altitude.isValid(), 7, 2);
  Serial.println();

  smartDelay(1000);

  if (millis() > 5000 && gps.charsProcessed() < 10)
    Serial.println(F("No GPS data received: check wiring"));
  Serial.println(dat);
  if (dat == 1)
  {
    Serial.println("date got1");

    kk = String(v) + ".txt";
    Serial.println(kk);
    dataFile = SD.open(kk, FILE_WRITE);
    if (dataFile) {
      Serial.println("File created");
      dataFile.println(F("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
      dataFile.println(F("<TrainingCenterDatabase xmlns=\"http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2\">"));
      dataFile.println(F("<Activities>"));
      dataFile.println(F("<Activity Sport=\"Other\">"));
      Serial.println(F("done 1st part"));
      dataFile.println("<Id>");
      dataFile.print(String(def));
      dataFile.print("T");
      dataFile.print(String(abc));
      dataFile.print(".000Z");
      dataFile.println("</Id>");
      dataFile.println("<Lap StartTime=\"");
      dataFile.print(String(def));
      dataFile.print("T");
      dataFile.print(String(abc));
      dataFile.print(".000Z\">");
      dataFile.println("<Track>");

      do {
        lat0 = (String(gps.location.lat(), 6)).toFloat();
        Serial.println("aquisition doing");
        lng0 = (String(gps.location.lng(), 6)).toFloat();
        setup();
      } while (lat0 == 0.0);
      if (lat0 == 0.0)
      {
        digitalWrite(LED, LOW);
      }
      else
      {
        digitalWrite(LED, HIGH);
      }
      logging();
    }
    else {
      Serial.println("CANT OPEN FILE");
    }

  }
}
void logging()
{
  Serial.println("came in loop 1");
  while (1)
  {
    if (digitalRead(7) == LOW)
      endtask();
    printDateTime(gps.date, gps.time);
    if (millis() > 5000 && gps.charsProcessed() < 10)
      Serial.println(F("No GPS data received: check wiring"));

    dataFile.println("<Trackpoint>");
    dataFile.println("<Time>");
    dataFile.print(String(def));
    dataFile.print("T");
    dataFile.print(String(abc));
    dataFile.print(".000Z");
    dataFile.print("</Time>");
    dataFile.println("<Position>");
    {
      lat1 = (String(gps.location.lat(), 6)).toFloat();
      lng1 = (String(gps.location.lng(), 6)).toFloat();

      float distLat = abs(lat0 - lat1) * 111194.9;
      float distLong = 111194.9 * abs(lng0 - lng1) * cos(radians((lat0 + lat1) / 2));
      dis = dis + sqrt(pow(distLat, 2) + pow(distLong, 2));
      Serial.println(dis, 6);
      lat0 = lat1;
      lng0 = lng1;
    }
    {
      dataFile.print("<LatitudeDegrees>");
      dataFile.print(String(lat1, 6));
      Serial.println(String(lat1, 6));
      dataFile.println("</LatitudeDegrees>");
      dataFile.print("<LongitudeDegrees>");
      dataFile.print(String(lng1, 6));
      Serial.println(String(lng1, 6));
      dataFile.println("</LongitudeDegrees>");
      dataFile.println("</Position>");
      dataFile.print("<AltitudeMeters>");
      dataFile.print((String(gps.altitude.meters(), 2)));
      dataFile.println("</AltitudeMeters>");
      dataFile.println("<DistanceMeters>");
      dataFile.println(String(dis, 6));
      dataFile.println("</DistanceMeters>");
      dataFile.println("</Trackpoint>");
      Serial.print("Recorded");
    }
    def[0] = NULL;
    abc[0] = NULL;
    smartDelay(1000);
  }
}


static void printFloat(float val, bool valid, int len, int prec)
{
  if (!valid)
  {
    while (len-- > 1)
      Serial.print('*');
    Serial.print(' ');
  }
  else
  {
    Serial.print(val, prec);
    int vi = abs((int)val);
    int flen = prec + (val < 0.0 ? 2 : 1); // . and -
    flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
    for (int i = flen; i < len; ++i)
      Serial.print(' ');
  }
  smartDelay(0);
}






void endtask()
{
  dataFile.println("</Track>");
  dataFile.println("</Lap>");
  dataFile.println("</Activity>");
  dataFile.println("</Activities>");
  dataFile.println("</TrainingCenterDatabase>");
  dataFile.close();
  Serial.print("file close");
  digitalWrite(LEDD, HIGH);

  while (1);
}







static void printInt(unsigned long val, bool valid, int len)
{
  char sz[32] = "*****************";
  if (valid)
    sprintf(sz, "%ld", val);
  sz[len] = 0;
  for (int i = strlen(sz); i < len; ++i)
    sz[i] = ' ';
  if (len > 0)
    sz[len - 1] = ' ';
  Serial.print(sz);
  smartDelay(0);
}






static void printDateTime(TinyGPSDate &d, TinyGPSTime &t)
{
  if (!d.isValid())
  {
    Serial.print(F("********** "));
    dat = 0;
  }
  else
  {

    sprintf(def, "%02d-%02d-%02d", d.year(), d.month(), d.day());
    Serial.print(def);
    dat = 1;
  }

  if (!t.isValid())
  {
    Serial.print(F("******** "));
  }
  else
  {

    sprintf(abc, "%02d:%02d:%02d", t.hour(), t.minute(), t.second());
    Serial.print(abc);
    sprintf(aaa, "%02d%02d%02d", t.hour(), t.minute(), t.second());
    Serial.print(aaa);

  }
  smartDelay(0);
}

static void smartDelay(unsigned long ms)
{
  unsigned long start = millis();
  do
  {
    while (Serial2.available())
      gps.encode(Serial2.read());
  } while (millis() - start < ms);
}

Please Help.

Thanks

Hi,
How are you powering the mega and with what voltage?

Thanks.. Tom... :slight_smile:

How are you powering the mega and with what voltage?

Tried using 9Vbattery,1.5A 12V and 9V Power Adaptor

Hi,
What are the ratings of the power supplies and how are you connecting them to the controller?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
A picture of your project would help as well.
Thanks Tom.... :slight_smile: