using __TIME__ and __DATE__

how can i use these to set the time of arduino without using RTC?

#include <Time.h>
#include <TimeAlarms.h>
#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 2
int Relay1 = 3;
int Relay2 = 4;
char state = 0;
String msg = "";
int Relaytest11 = 5;
int Relaytest12 = 6;
int Relaytest21 = 7;
int Relaytest22 = 8;
int val = 0;
int val2 = 0;
boolean state1 = HIGH;
boolean state2 = LOW;
int stat = 0;

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);


void setup()
{
  pinMode(Relay1, OUTPUT); 
  pinMode(Relay2, OUTPUT);
  pinMode(Relaytest11, OUTPUT);
  pinMode(Relaytest21, OUTPUT);
  pinMode(Relaytest12, INPUT);
  pinMode(Relaytest22, INPUT);
  digitalWrite(Relay1, HIGH);
  digitalWrite(Relay2, HIGH);
  
  Serial.begin(9600);
  setTime(8,29,0,1,1,11); // set time to Saturday 8:29:00am Jan 1 2011
  // create the alarms 
  Alarm.alarmRepeat(8,29,10, AC1on);  // 8:30am every day
  Alarm.alarmRepeat(8,29,20,AC2off);  // 5:45pm every day 
  Alarm.alarmRepeat(8,29,30,AC2on);  // 8:30:30 every day
  Alarm.alarmRepeat(8,29,40,AC1off);  // 8:30:30 every day
 }

void  loop(){  
  //digitalClockDisplay();
  Alarm.delay(1000); // wait one second between clock display
  String msg = "";
if (Serial.available()>0){
  while (Serial.available()>0){
    char each= Serial.read();
    msg += each;
  

 if (msg == "ac1 on")
 {
    digitalWrite(Relay1,HIGH);
    Serial.print("ac1 is now on");
    digitalWrite(Relaytest11, LOW);
    Serial.println(); 
    Serial.println(); 
  }
    
 else if (msg == "ac1 off")
 {
    digitalWrite(Relay1,LOW); 
    Serial.print("ac1 is now off");
    digitalWrite(Relaytest11, HIGH); 
    Serial.println(); 
    Serial.println(); 
 }
 else if (msg == "ac2 on")
 {
    digitalWrite(Relay2,LOW);
    Serial.print("ac2 is now on");
    digitalWrite(Relaytest21, HIGH);
    Serial.println();
    Serial.println();  
  }
    
 else if (msg == "ac2 off")
 {
    digitalWrite(Relay2,HIGH);
    Serial.print("ac2 is now off");
    digitalWrite(Relaytest21, LOW);
    Serial.println(); 
    Serial.println(); 
  }
  
  else if(msg == "ac1 alone")
  {
    state1 = HIGH;
    state2 = HIGH;
    digitalWrite(Relay1,state1);
    digitalWrite(Relay2,state2);
    Serial.print("AC1 will be always ON");
    Serial.println();
    digitalWrite(Relaytest11, LOW);
    digitalWrite(Relaytest21, LOW);
    stat = 1;
    
  }
  
    else if(msg == "ac2 alone")
  {
    state1 = LOW;
    state2 = LOW;
    digitalWrite(Relay2,state2);
    digitalWrite(Relay1,state1);
    Serial.print("AC2 will be always ON");
    Serial.println();
    digitalWrite(Relaytest21, HIGH);
    digitalWrite(Relaytest11, HIGH);

    stat = 2;
  }
  
    else if(msg == "default")
  {
    state1 = HIGH;
    state2 = LOW;
    digitalWrite(Relay1,HIGH);
    digitalWrite(Relay2,HIGH);
    Serial.print("Default alarm was set");
    Serial.println();
    digitalWrite(Relaytest21, LOW);
    digitalWrite(Relaytest11, LOW);
    
    stat = 0;
  }
    
 else if (msg == "info"){
     Serial.println("Planter's Hall Site");
     sensors.requestTemperatures(); // Send the command to get temperatures
     Serial.print("current Temperature is: ");
     Serial.print(sensors.getTempCByIndex(0)); // Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
     Serial.print(" C");
     Serial.println();
     Serial.print("The current time is: ");
     Serial.print(hour());
     printDigits(minute());
     printDigits(second());
     Serial.print(" ");
     Serial.print(day());
     Serial.print(",");
     Serial.print(month());
     Serial.print(",");
     Serial.print(year());
     Serial.println();
     Serial.print("Air conditioning status: ");
     Serial.println(); 
     val= digitalRead(Relaytest12);
     if (val == LOW)
     {Serial.print("AC1 is on");}
     else if(val == HIGH)
     {Serial.print("AC1 is off");}
     Serial.println();
     val2= digitalRead(Relaytest22);
     if (val2 == HIGH)
     {Serial.print("AC2 is on");}
     else if(val2 == LOW)
     {Serial.print("AC2 is off");}
     Serial.println();
     Serial.println();
     
   }
   

}}
}

// functions to be called when an alarm triggers:
void AC1on(){ 
  Serial.println("AC1 on");    //print "AC1 on" on serial monitor
  digitalWrite(Relay1, state1);  //trigger AC1 relay 
  if (stat = 0)
  {digitalWrite(Relaytest11, LOW);}
  if (stat = 1)
  {digitalWrite(Relaytest11, LOW);}
  if (stat = 2)
  {digitalWrite(Relaytest11, HIGH);}
}

void AC2off(){  
  Serial.println("AC2 off");    //print "AC1 on" on serial monitor
  digitalWrite(Relay2, state1);  //trigger AC1 relay  
  if (stat = 0)
  {digitalWrite(Relaytest21, LOW);}
  if (stat = 1)
  {digitalWrite(Relaytest21, LOW);}
  if (stat = 2)
  {digitalWrite(Relaytest21, HIGH);} 
}

void AC2on(){
  Serial.println("AC2 on");    //print "AC1 on" on serial monitor
  digitalWrite(Relay2, state2);  //trigger AC2 relay 
  if (stat = 0)
  {digitalWrite(Relaytest21, HIGH);}
  if (stat = 1)
  {digitalWrite(Relaytest21, LOW);}
  if (stat = 2)
  {digitalWrite(Relaytest21, HIGH);}
}


void AC1off(){
  Serial.println("AC1 off");     //print "AC2 on" on serial monitor
  digitalWrite(Relay1, state2);   //trigger AC2 relay on
  if (stat = 0)
  {digitalWrite(Relaytest11, HIGH);}
  if (stat = 1)
  {digitalWrite(Relaytest11, LOW);}
  if (stat = 2)
  {digitalWrite(Relaytest11, HIGH);}
}



void ExplicitAlarm(){
  Serial.println("Alarm: - this triggers only at the given date and time");       
}

void Repeats(){
  Serial.println("15 second timer");         
}

void OnceOnly(){
  Serial.println("This timer only triggers once");  
}

void digitalClockDisplay()
{
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.println(); 
}

void printDigits(int digits)
{
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

is there a way to change its format to “8,29,0,1,1,11” (hours,minute,seconds,month,day,year)
or is there other function that i can use so it will automatically sync the time from the computer…
(the arduino is always connected to the computer)

is there a way to change its format to "8,29,0,1,1,11"

No. The TIME and DATE macros have a hardcoded format.

Those macros generate compile-time constants. They have nothing to do with the current date and time on the PC, except for when the compiler is invoked.

The Time library includes an example Processing application that can provide the date and time in a format that the Arduino can understand and use to set the time (in a way that the Time library understands).

oh i see.. so, is there other way to automatically set the time using other sources?.. or it really has to be set manually?..

dazmehard:
oh i see.. so, is there other way to automatically set the time using other sources?.. or it really has to be set manually?..

It only needs to be set once. Why is that a big deal?

It can be set using the DATE and TIME values, if they are stored in strings and parsed, but that seems like more trouble than it is worth.

I want it to set automatically when the computer is shutdown or there is a power cut, since im not using a RTC.

and i cannot use DATE and TIME in the code without using RTC function command, or is there other function that read these?. Im only using

setTime(8,29,0,1,1,11);

to set the time..

The sketch you create with DATE and TIME will have strings in it that contain the date and time the sketch was compiled. I don't see how that is better than some other arbitrary time.

Getting the real date and time from the PC would be a far better choice.

i havent used DATE and TIME in my sketch yet, im not sure how can i use it… to set the time, it requires hours, minutes, seconds, day, etc.

Well, try it. Try using Serial.println() to print out the values of those macros. Then look at the result and imagine how you might extract the minutes and seconds etc.

TIME and Date will always set the clock to the date and time of compilation. It is useless for setting a clock a week after you compiled it.

TIME and Date will always set the clock to the date and time of compilation. It is useless for setting a clock a week after you compiled it.

yeah thats what im thinking too.. i have to sync the time at least once a day, to make sure that its the right time, and using TIME and Date doesn't fit right. Ill try to look for different approach. python might have a solution.

Ill try to look for different approach. python might have a solution.

The PC needs to be running some application that knows the time and date, and can generate a return value that the Arduino understands.

What format is that value? If you are using the Time library, it includes a Processing application that can be run on the PC that knows how to return the date and time in a format that the Time library can understand.

There is NO need to reinvent the wheel.