Problem with implement infinite loop

Hi I have attached Sparkfun infrared camera with Arduino. This code saves the image in the SD card and Sends the hex value of the image on the Serial Monitor. Can you help me to put this code in an infinite loop so that I can receive image after every 5 minutes?

#include <SoftwareSerial.h>
#include <SD.h>

byte ZERO = 0x00;

byte incomingbyte;
SoftwareSerial mySerial(5, 6);
         // Set Arduino pin 4 and 5 as softserial <<<< actually pins 5 and 6

long int a = 0x0000, j = 0, k = 0, count = 0, i = 0;
uint8_t MH, ML;
boolean EndFlag = 0;

File  myFile;

void SendResetCmd();
void SetBaudRateCmd();
void SetImageSizeCmd();
void SendTakePhotoCmd();
void SendReadDataCmd();
void StopTakePhotoCmd();

void setup()
{
  Serial.begin(38400);
  while (!Serial)
  {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  mySerial.begin(38400);
  Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
  pinMode(10, OUTPUT);
  if (!SD.begin(4))
  {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
}

void loop()
{
  byte a[32];
  int ii;
  SendResetCmd();
  delay(4000);
                             //Wait 2-3 second to send take picture command
  SendTakePhotoCmd();
  while (mySerial.available() > 0)
  {
    incomingbyte = mySerial.read();
  }
  myFile = SD.open("pic.jpg", FILE_WRITE); //<strong><span style="color: #ff0000;">The file name should not be too long</span></strong>
  while (!EndFlag)
  {
    j = 0;
    k = 0;
    count = 0;
    SendReadDataCmd();
    delay(20); //250 for regular
    while (mySerial.available() > 0)
    {
      incomingbyte = mySerial.read();
      k++;
      if ((k > 5) && (j < 32) && (!EndFlag))
      {
        a[j] = incomingbyte;
        if ((a[j - 1] == 0xFF) && (a[j] == 0xD9))     //tell if the picture is finished
          EndFlag = 1;
        j++;
        count++;
      }
    }
    for (j = 0; j < count; j++)
    {
      if (a[j] < 0x10)
        Serial.print("0");
      Serial.print(a[j], HEX);
                // observe the image through serial port
      Serial.print(" ");
    }
    for (ii = 0; ii < count; ii++)
      myFile.write(a[ii]);
    Serial.println();
    i++;
  }
  myFile.close();
  Serial.print("Finished writing data to file");
  while (1);
}

void SendResetCmd()
{
  mySerial.write(0x56);
  mySerial.write(ZERO);
  mySerial.write(0x26);
  mySerial.write(ZERO);
}

void SetImageSizeCmd()
{
  mySerial.write(0x56);
  mySerial.write(ZERO);
  mySerial.write(0x31);
  mySerial.write(0x05);
  mySerial.write(0x04);
  mySerial.write(0x01);
  mySerial.write(ZERO);
  mySerial.write(0x19);
  mySerial.write(0x11);
}

void SetBaudRateCmd()
{
  mySerial.write(0x56);
  mySerial.write(ZERO);
  mySerial.write(0x24);
  mySerial.write(0x03);
  mySerial.write(0x01);
  mySerial.write(0x2A);
  mySerial.write(0xC8);
}

void SendTakePhotoCmd()
{
  mySerial.write(0x56);
  mySerial.write(ZERO);
  mySerial.write(0x36);
  mySerial.write(0x01);
  mySerial.write(ZERO);
}

void SendReadDataCmd()
{
  MH = a / 0x100;
  ML = a % 0x100;
  mySerial.write(0x56); //56 00 32 0C 00 0A 00 00 MH ML 00 00 KH KL XX XX
  mySerial.write(ZERO);
  mySerial.write(0x32);
  mySerial.write(0x0c);
  mySerial.write(ZERO);
  mySerial.write(0x0a);
  mySerial.write(ZERO);
  mySerial.write(ZERO);
  mySerial.write(MH);
  mySerial.write(ML);
  mySerial.write(ZERO);
  mySerial.write(ZERO);
  mySerial.write(ZERO);
  mySerial.write(0x20);
  mySerial.write(ZERO);
  mySerial.write(0x0a);
  a += 0x20;
}

void StopTakePhotoCmd()
{
  mySerial.write(0x56);
  mySerial.write(ZERO);
  mySerial.write(0x36);
  mySerial.write(0x01);
  mySerial.write(0x03);
}

First, please go back and edit your post so your request is not inside the code tags. But thanks for using them.

loop() is an infinite loop. Just comment out the part at the end where you close the file and freeze with a
'while(1);'.

Code tags corrected in the original post

DO NOT double post. Use code tags when posting.

Duplicate topics merged

Why did you start a second one ?

That's was my first post, I'm a little bit unaware of the interface. Sorry about that. I'm trying to delete duplicate one.

I'm trying to delete duplicate one.

It has been merged with this one. I suggest that you leave things as they are

aarg:
First, please go back and edit your post so your request is not inside the code tags. But thanks for using them.

loop() is an infinite loop. Just comment out the part at the end where you close the file and freeze with a
'while(1);'.

Thanks for your reply. It won't work in my case since Arduino has a limited size and the image size is more than 10-30kb. while(1); in my code help Arduino to send hex value on the serial monitor in parts unless it's reached at FFD9. Actually hex value has a pattern like FFD8.....FFD9.

So this code practically captures only one image. To get another image you have to re-upload the code. But I want an image every 5 minutes.

To get another image you have to re-upload the code

Of course not. You simply have to correctly reinitialize the already loaded code.

That will include rethinking how to store images on the SD card, because it does not make much sense to keep writing to the same file.

jremington:
Of course not. You simply have to correctly reinitialize the already loaded code.

Thanks for your response. I'll rethink about reinitializing it although I'm finding it hard. Actually an I'm no pro in Arduino programming and I did some changing in Sparfkfun to get the full image(you can say by hit and trial).

And I'm not worried about file storage in SD card, I'm reading the hex value from serial monitor and directly saving the image in computer hard disk. I succeed to store 1 image in the laptop via python but I'm not understanding how to get new hex value for new image after 5 minutes.

And I must say that your advice ring a bell in my mind, I'll try to solve it via hit and trial method.

I started to explain this to you, then I realized what a waste of time it is. There is no mystery to it, you just need to examine step by step, what needs to be done before, what needs to be done during, and what needs to be done after looping for images.

For example, open the file before and close it after. Another example, at the end of the picture loop, wait 5 minutes. And so on...

Your style is not terrible, but really could use some improvement. Doing so would encourage people to volunteer to help you. Use a standard indentation to clearly show the code blocks. Never put more than one statement per line. Place any brackets by themselves on a separate line. Use blank lines sparingly, no more than one at a time. Before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read. Another is to give things descriptive names. Use descriptive variable names, for example "temperature" instead of "t". You can name numerical constants, pin numbers, variables and many other things in this way. For example, you can refer to a pin and an output level by number, like digitalWrite(3,0). But such a statement doesn't reveal anything about the purpose. digitalWrite(hornRelayPin, LOW) does. You can do that by declaring const byte hornRelayPin = 3; before setup() in your program. Many such names are already defined for you by the compiler and the Arduino IDE. Here are some:

#define HIGH 0x1
#define LOW  0x0
#define PI 3.1415926535897932384626433832795

Use them. There are many more. Use compiler math to compute values so you can see where they came from (or at least document them). For example, if you see the number 73, you would be hard put to explain the significance of it. But if you see "daysPerYear/5", it is obvious. One more thing. When you work on program continuously, you become familiar with it. So many things seem obvious even if they are not spelled out explicitly. But try looking at your own code six months later. It will be as if a stranger wrote it. So write for strangers, not yourself.

+1, and this bears repeating:

you just need to examine step by step, what needs to be done before, what needs to be done during, and what needs to be done after looping for images.