Arduino program occasionally stops running.

Hello everybody, I am using an Arduino UNO as an environmental control system. I have been working on this project for quite some time and I have finally got all the kinks out of the system... except that the program will occasionally cease to execute. (I actually just came home to it not running.) Seems to be more of an issue when the Arduino is not powered by BOTH the USB and 12v power supply.

My question to this community is what would cause the Arduino to stop functioning. Its almost if the Arduino reset itself for some reason and did not boot back up.

UPDATE: I witnessed the Arduino firing all the relays and pulse power at about a 1/2 second interval (while remaining on ) before shutting off completely, rebooting. Pulsing again, turning off and eventually staying off. (while only powered by the external power source)

ADDITIONAL UPDATE: I can recreate the problem at will, and it only happens when the Arduino board is NOT connected to my MacBook via usb. If only connected to an external an external power source and I manually reset the board. The situation above happens. Its almost as if the board "forgets" its sketch.

I can however do the same thing while the Arudino is connected to my Mac and the program will run normally after a hard reset.

I was reading something similar in the trouble shooting guide, please excuse me while I do some research.

Additional info:
The code works. It is a mixture of using Millis() via the "blink without delay" tutorial and if statements with conditions related to the sensor to operate.

Sensors are connected separately using the SDA/SCL and digitalPin 5/6 respectively utilizing I2C.
I am utilizing a 12v 800ma external power supply for the Arduino.

One relay is timed at 15s on 15s off.
The other two are set by parameters via sensors.

Thank you for your time.

#include <Wire.h>
#include <NDIR_I2C.h>
#include <SHT1x.h>

NDIR_I2C mySensor(0x4D); //Adaptor's I2C address (7-bit, default: 0x4D)


// Specify data and clock connections and instantiate SHT1x object
#define dataPin  5
#define clockPin 6
SHT1x sht1x(dataPin, clockPin);

// Environemntal variables
    int humLevel = 96.;
    int ppmLevel = 1500;

    
//Fresh air exchange FAN     
    int ledPin1 =  2;      // the number of the pin for FAN relay1
    int ledState1 = LOW;             // ledState used to set the relay
    unsigned long previousMillis1 = 0;        // will store last time LED was updated
    long OnTime1 = 45000;           // TIME ON RELAY1
    long OffTime1 = 900000;          // TIME OFF RELAY1

// Humidity
    int ledPin2 =  3;      // the number of the HUMIDITY pin relay2
    int ledState2 = LOW;             // ledState used to set the relay 
   // unsigned long previousMillis2 = 0;        // will store last time LED was updated
   // long OnTime2 = 2000;           // TIME ON RELAY2
   // long OffTime2 = 20000;          // TIME OFF RELAY2

// Internal FAN
     int ledPin3 = 4;
     int ledState3 = LOW;
     unsigned long previousMillis3 = 0;
     long OnTime3 = 15000;
     long OffTime3 = 15000;

     
    void setup() 
    {
  Serial.begin(38400); // Open serial connection to report values to host
   Serial.println("Starting up");
      // set the digital pin as output:
      pinMode(ledPin1, OUTPUT);      
      pinMode(ledPin2, OUTPUT);
      pinMode(ledPin3, OUTPUT);   

    if (mySensor.begin()) {
        Serial.println("Wait 10 seconds for sensor initialization...");
        delay(10000);
    } else {
        Serial.println("ERROR: Failed to connect to the sensor.");
        while(1);
    } 
    }
            
     
    void loop()
    {
      

  float temp_c;
  float temp_f;
  float humidity;

  // Read values from the sensor
  temp_c = sht1x.readTemperatureC();
  temp_f = sht1x.readTemperatureF();
  humidity = sht1x.readHumidity();

  // Print the values to the serial port
  Serial.print("Temperature: ");
  Serial.print(temp_c, DEC);
  Serial.print("C / ");
  Serial.print(temp_f, DEC);
  Serial.print("F. Humidity: ");
  Serial.print(humidity);
  Serial.println("%");
  Serial.println(millis()/1000);

  delay(3300);

//CO2 Begin

    if (mySensor.measure()) {
        Serial.print("CO2 Concentration is ");
        Serial.print(mySensor.ppm);
        Serial.println("ppm");
    } else {
        Serial.println("Sensor communication error.");
    }

    delay(10000); 

    
// CO2 sensor ON-OFF statement         
      if (mySensor.ppm > ppmLevel){
      digitalWrite(ledPin1,LOW);
     }
     else{
      digitalWrite(ledPin1,HIGH);
      }
    

           
// Humidity ON-OFF statment  
    if (humidity < humLevel)
          {
          (digitalWrite(ledPin2, LOW));
          }    else{
          (digitalWrite(ledPin2,HIGH));
            }  
// Fresh Air Exchange statement.
 unsigned long currentMillis = millis();
          if (mySensor.ppm > ppmLevel)
          {
            digitalWrite(ledPin1, LOW);
            }
          else 
          {
            digitalWrite(ledPin1, HIGH);
            }
            
//          else if((ledState1 == LOW) && (currentMillis - previousMillis1 >= OnTime1))
//          {
//           ledState1 = HIGH;  // Turn it off
//          previousMillis1 = currentMillis;  // Remember the time
//          digitalWrite(ledPin1, ledState1);  // Update the actual LED
//           }
//          else if ((ledState1 == HIGH) && (currentMillis - previousMillis1 >= OffTime1))
//          {
//          ledState1 = LOW;  // turn it on
//          previousMillis1 = currentMillis;   // Remember the time
//          digitalWrite(ledPin1, ledState1);    // Update the actual LED
//          }

            
// Internal Fan timing code (blink without delay)

          if((ledState3 == LOW) && (currentMillis - previousMillis3 >= OnTime3))
          {
           ledState3 = HIGH;  // Turn it off
          previousMillis3 = currentMillis;  // Remember the time
          digitalWrite(ledPin3, ledState3);  // Update the actual LED
           }
          else if ((ledState3 == HIGH) && (currentMillis - previousMillis3 >= OffTime3))
          {
          ledState3 = LOW;  // turn it on
          previousMillis3 = currentMillis;   // Remember the time
          digitalWrite(ledPin3, ledState3);    // Update the actual LED
          }

    }

Digital 0 and 1 are also used for serial connections.

A description of the sketch means nothing to anyone I am afraid.
Include you sketch on your next post but ensure you use code tags to do so ( </> )

EDIT..

Your edit above is much better it makes it easier for people to help you.

Do you power the relays from the Arduino's 5V pin?

If so, the regulator that converts 12V to 5V is probably overheating and shuts down.

Solution in that case is a separate power supply for the relays.