Serial.print() issue or maybe something else

Hi everyone. I am running a fairly complicated sketch. It is supposed to take a few pictures with the VC0706, then send some of those pictures over the Arduino GSM module. The Arduino is then supposed to sleep for 60 seconds before waking up and repeating the process again. However the program seems to crash and burn right at the beginning. The first two characters of my first Serial.print are sent, then nothing seems to happen after that. On my serial monitor I just get "He" every time the program is run.

Here is my code. I can post the functions too if you guys want but I verified they work OK in a separate sketch (except for sleep(int time). As you may be able to tell from looking at my code, I don't really know what I'm doing but I am doing it anyway

/* This program will take take x sets of 1 small and 1 big picture, then transmit the small
 pictures over GSM. It will then sleep for X seconds before taking another set of pictures.
 This process will repeat, once this is working the next step will be to add external
 commands */

#include <GSM.h>
#include <Adafruit_VC0706.h>
#include <SD.h>
Adafruit_VC0706 cam = Adafruit_VC0706(&Serial1);

//define and initialize some GSM stuff
#define GPRS_APN  "haha"
#define GPRS_LOGIN  "lol"
#define GPRS_PASSWORD "teehee"

GSMClient client;
GPRS gprs;
GSM gsmAccess;

char server[] = "what security";
int port = 69;

//set up some sleep stuff
#include <avr/sleep.h>
#include <avr/power.h>

volatile int flag = 1;

void setup(){
    
  Serial.begin(9600);
  delay(1000);
  Serial.println("Hello i am watching you");
  
  pinMode(53,OUTPUT);
  Serial.println("...");

  //set up timing interrupts
  cli();
  TCCR2B = 0x07;
  TCCR2A = 0x00;
  TIMSK1 = 0x01;

  //sleeping interval in seconds
  int sleeptime = 60;
  //number of sets of 1 big 1 small pictures to take in between each sleep
  int numPics = 2;
  //number of sleep/ take pictures cycles to run through, setting to 100 
  //so it pretty much goes forever
  //int cycles = 100;

  //this loop will do one full sleep and take pictures cycle
  //simplifying this a bit so it actually just runs forever  
  while(1){

    if (!SD.begin(53)) {
      Serial.println("Card failed, or not present");
      // don't do anything more:
      return;
    }

    cam.begin(115200);

    //an array of filenames that will later be transmitted over GSM
    char files[numPics][13];

    //this loop will take a small picture then a big picture and store them
    //to SD
    for(int k=0; k < numPics; k++){
      char filename[13];
      strcpy(filename, "IMAGE00.JPG");

      for (int i = 0; i < 100; i++) {
        filename[5] = '0' + i/10;
        filename[6] = '0' + i%10;
        // create if does not exist, do not open existing, write, sync after write
        if (! SD.exists(filename)) {
          break;
        }
      }

      for(int i=0; i <=13; i++){
        files[k][i] = filename[i];
      }

      cam.setImageSize(VC0706_160x120);  // small
      cam.reset();
      delay(1100);

      int32_t time = millis();

      if (! cam.takePicture()) 
        Serial.println("Failed to snap!");
      else 
        Serial.println("Picture taken!");

      writepic(filename);

      cam.setImageSize(VC0706_640x480);  // biggest
      cam.reset();
      delay(1100);

      time=millis() - time;

      if (! cam.takePicture()) 
        Serial.println("Failed to snap!");
      else 
        Serial.println("Picture taken!");

      Serial.print(time); 
      Serial.println(" ms elapsed between shots.");

      filename[7] = 'b';
      filename[8] = '.';
      filename[9] = 'j';
      filename[10] = 'p';
      filename[11] = 'g';

      writepic(filename);
    }

    //hopefully this will send the filenames stored in the array files
    //not too sure what I am doing with these char arrays
    Connect();
    for(int k=0; k < numPics; k++){      
      
      char filename[13];
      for(int i=0; i < 13; i++){
        filename[i] = files[k][i];
      }
      
      sendpic(filename);
      xeosrefresh();    
    }
    Disconnect();
    
    Serial.print("Sleeping for");
    Serial.print(sleeptime);
    Serial.println("seconds.");
    
    //sleep for preselected time
    sleep(sleeptime);
  }


}

void loop(){
}

are you running out of memory?
google for "freeram arduino"

Aside from other things, you might be running out of memory. Put all your serial monitor strings in the Flash memory with F(), like so:
Serial.println(F("Hello i am watching you"));

Yes the memory thing seems like a likely cause, thanks guys. I tried your suggestion HazardsMind, just on the first print statement. It still had the same output as before, just the first two characters "He".

I am looking at the freeram thing curreently

check - Arduino Playground - AvailableMemory -

Ok so I tried commenting out all of my sleep functionality and the problem is still the same. I would have thought removing those extra functions would free up some memory, correct?

Also I am curious, how could all the RAM get used up by the third line of setup()? If this is indeed a memory problem wouldn't the program make it a bit further before breaking? Like all I have done at the point of crashing is define and include some stuff. After thinking about it it doesn't seem to me like running out of memory is the problem, just because the program crashes right at the beginning.

Any other ideas anyone?

Thanks!

One other thing...
How were you guys thinking I would test my available memory? Doesn't seem like adding that in would do anything because the program dies at startup anyways

I'm curious why you are sleeping. Sleeping doesn't cut power to the GSM shield, which is using FAR more power than the Arduino, and continues to use power while the Arduino sleeps.

Unless you aren't using a shield. If you are not using a shield, how are you powering the GSM device, and how are you shutting off, and restoring, power when sleeping/waking?

Hey Paul thanks for the response. I am just using the regular Arduino GSM Shield. At this point in time I am really just sleeping for academic purposes. You are right it barely saves any current. Down the line I will be running both the GSM shield and camera on a FET I guess, so I can cut power before sleeping. That is the plan anyways maybe a more complicated solution is needed.

you disable the interrupts

cli();

but where do you enable them again?

They might be needed ....

First of all take HazardsMind's suggestion for ALL constant strings used with the .print family of functions.
robtillaart is right. The Serial. functions are interrupt driven I believe.

Ok I tried your suggestion Mr Arduino of placing all strings in flash memory. Not sure if it changed anything but it doesn't seem like it.

I also looked into the interrupt thing. To answer your question Rob I was re-enabling interrupts before each time the board goes to sleep, however this seems foolish now. If I run the program with interrupts enabled I get the following repeating output on the serial monitor. It just keeps sending the same gibberish indefinitely:

HelHeª!eª!eªeª!?!eª!eª!eªeHeª!eª!eª!eª!eª!eª!eª!eª!eª

Looks like it tries to send the first message three times in total before repeating, not sure if that means anything to anyone...

Also at the moment I have my sleep functionality commented out.

Actually upon looking closer at my serial output I noticed that "He" keeps showing up again and again. This leads me to believe the program is somehow getting stuck on the first Serial.print statement and attempting to complete it repeatedly. Maybe that is a more common problem?

What is the baud rate your serial monitor is set to, 9600? You can find the number in the lower right corner when the serial monitor is open.

Yep haha its at 9600 thanks though

Well the only thing left to do now is to start commenting out larger portions. Then once the serial monitor is working again, then you can add things in little by little.

Added:
Were you able to get the Snapshot example sketch to work?

Yep the camera works fine. I have been able to transmit JPEGS over GSM already too, in a simpler testing sketch. Thanks for your suggestion about removing portions of the sketch until serial works. I'll try that later but I noticed all my GSM code that was working last week is now broken.

One weird symptom I noticed is that whenever the program reaches the GSM method client.connect() the arduino IDE loses serial connectivity. The arduino still shows up in device manager but the IDE just says COMPORT not found. Also client.connect() seems to successfully connect to the server but still returns false in the program. It just tries to connect again and again even though it is succeeding.

maybe i should just make a new post about this but whatev...

I notice in your code Adafruit_VC0706 cam = Adafruit_VC0706(&Serial1);... Which Arduino are you using? You never mentioned.

Also,

One weird symptom I noticed is that whenever the program reaches the GSM method client.connect() the arduino IDE loses serial connectivity. The arduino still shows up in device manager but the IDE just says COMPORT not found. Also client.connect() seems to successfully connect to the server but still returns false in the program. It just tries to connect again and again even though it is succeeding.

This almost sounds like the GSM module is trying to pull too much current, pulling down your voltage source causing a brownout. How do you have everything powered?

I'm using the arduino mega2560. I'm powering everything with a lab style DC power supply that can supply 3A or something. Now that you mention I have the knob turned down pretty low, maybe that is the issue. I will test tomorrow, at least it's an easy fix if that does end up being the problem. The voltage does dip from 5.0 to 4.9 when the GSM module activates, that doesn't really seem significant though does it?