Go Down

Topic: Arduino Clock: Setting Time (Read 10706 times) previous topic - next topic

PaulS

You might want to print out some values.
Code: [Select]
nextAlarm =  previousMidnight(DateTime.now())+ alarmOffTime;

The nextAlarm value looks to me like that's the next time to turn the alarm off. If that's the case, the name choice is poor.

The nextAlarm variable is going to hold a fairly large number, though, since it is the number of seconds from an event that happened years ago.

The variables alarmOnTime and alarmOffTime hold very small values, since they are to be used as relative values. alarmOffTime is relative to alarmOnTime. alarmOnTime is not relative to the same event that nextAlarm is, though. It isn't relative to anything, for that matter.

So, this code:
Code: [Select]
if ( DateTime.now() >= alarmOnTime  && DateTime.now()  < alarmOnTime + alarmOffTime )
will always be false.

DateTime.now() returns the number of seconds since that same event many years ago, so it is a large number. That number is always going to be greater than the value in alarmOnTime. It will NOT be less than alarmOnTime + alarmOffTime, unless alarmOffTime is very large (say about 40 years).


DarkElf

I see now...

I don't need next alarm

all I want is LED to activate when it hits 10:41:00 and be activated about a minute

how to do it, I don't know

from when DateTime.now() is start counting
my intention is ti put
DateTime.sync(DateTime.makeTime(45, 40, 10, 29, 5, 2010));
that time in DateTime.now() that will start count secont if that is posible and than if condition will work, I think so

would appreciate that if condition that works, VERY
or some exaples or advices to move on...

PaulS

The nextAlarm variable is set to when the alarm should go off (10 hours and 42 minutes after midnight). Rename it to nextAlarmOff. Create another variable, nextAlarmOn that is set like nextAlarm, but to when to then the alarm on.

Then, in the if test, see if now is greater than nextAlarmOn and less than nextAlarmOff. If it is, light up the LED.

DarkElf

thnx a lot!!!

the code is now working!!

here it is:

time_t  alarmOnTime =  (10 * SECS_PER_HOUR) + (41 * SECS_PER_MIN);
 time_t  alarmOffTime =  (10 * SECS_PER_HOUR) + (42 * SECS_PER_MIN);
 //time_t  alarmOffTime =  alarmOnTime + (30 * SECS_PER_SEC);
 time_t nextAlarmOff;
 time_t nextAlarmOn;
 
 
 nextAlarmOn  =  previousMidnight(DateTime.now())+ alarmOnTime;
 nextAlarmOff =  previousMidnight(DateTime.now())+ alarmOffTime;


 if ( DateTime.now() >= nextAlarmOn  && DateTime.now()  < nextAlarmOff){
  digitalWrite(ledPin, HIGH);
 }
 else
 {
  digitalWrite(ledPin, LOW);
 }  
}
}

now I'm curious to set exact seconds to turn of, not minutes, something like
time_t  alarmOffTime =  alarmOnTime + (30 * SECS_PER_SEC);
but SECS_PER_SEC don't exist

and also I'll need to slowly start led...


PaulS, you saved me  :)

PaulS

That's because SECS_PER_SEC is 1.

DarkElf

ok, second to turn off led are solved

but there still a problem with turning on led, opposite of fading (can't remember word now)

I used code from fading, but problem is when for loop is active, clock stop counting...
any ideas how to accomplish it in another way?



if ( DateTime.now() >= nextAlarmOn  && DateTime.now()  < nextAlarmOff){
  //digitalWrite(ledPin, HIGH);
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=10) {
   // sets the value (range from 0 to 255):
   analogWrite(ledPin, fadeValue);        
   
   delay(500);                            
 }
 }
 else
 {
  digitalWrite(ledPin, LOW);
 }  
}

PaulS

Look at the BlinkWithoutDelay example.

On each pass through loop, see if it is time to have the LED on, as you are doing now. If it is, see if it is time to step up another level.

DarkElf

I mixed something up...

#include <LiquidCrystal.h>
#include <DateTime.h>


LiquidCrystal lcd(7,8,9,10,11,12);

int ledPin =  6;    // LED connected to digital pin 6


void setup(){
 DateTime.sync(DateTime.makeTime(55, 40, 10, 29, 5, 2010));  //set up time
 lcd.begin(16, 2);
 // initialize the digital pin as an output:
   pinMode(ledPin, OUTPUT);
}

void  loop(){    
 if(DateTime.available()) {
   unsigned long prevtime = DateTime.now();
   while( prevtime == DateTime.now() )  // wait for the second to rollover
     ;
   DateTime.available(); //refresh the Date and time properties
   digitalClockDisplay( );   // update digital clock
   
 
 time_t  alarmOnTime =  (10 * SECS_PER_HOUR) + (41 * SECS_PER_MIN);
 time_t  alarmOffTime =  alarmOnTime + (1 * SECS_PER_MIN)/4;  //led turn on 15 seconds
 time_t nextAlarmOff;
 time_t nextAlarmOn;
 
 
 nextAlarmOn  =  previousMidnight(DateTime.now())+ alarmOnTime;
 nextAlarmOff =  previousMidnight(DateTime.now())+ alarmOffTime;


 if ( DateTime.now() >= nextAlarmOn  && DateTime.now()  < nextAlarmOff){
  //digitalWrite(ledPin, HIGH);
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=15) {
   // sets the value (range from 0 to 255):
   analogWrite(ledPin, fadeValue);        
   delay(800);                            
 }
 digitalWrite(ledPin, HIGH);
 }
 else
 {
  digitalWrite(ledPin, LOW);
 }  
}
}

void printDigits(byte digits){
 // utility function for digital clock display: prints preceding colon and leading 0
 lcd.print(":");
 if(digits < 10)
   lcd.print('0');
 lcd.print(digits,DEC);
}

void digitalClockDisplay(){
 lcd.home();
 // digital clock display of current time
 lcd.print(DateTime.Hour,DEC);  
 printDigits(DateTime.Minute);  
 printDigits(DateTime.Second);  
}


alarm activates at 10:41:00 and lasts for 15 seconds....

problem is to me now to left light turn on, while after execute the program it turns off

ideao was to put digitalWrite(ledPin, HIGH); after for loop but it doesn't work

why?   :o

PaulS

Quote
alarm activates at 10:41:00 and lasts for 15 seconds....

That's good.

Quote
problem is to me now to left light turn on, while after execute the program it turns off


I suspect that English is not your native language, but, this doesn't make sense. If the light stays on, then the alarm is not lasting just 15 seconds.

Can you try again to explain what the problem is?

mem

DarkElf, you are in good hands with PaulS so I don't want to interrupt as it sounds like you are close to solving your problem.

However, I do wonder why you are using the older DateTime library as there is an updated version that is a little easer to use and has some enhancements and bug fixes, see: http://www.arduino.cc/playground/Code/Time

That download includes a companion library called TimeAlarms that would make your task easier. Using this library you can create one alarm to turn on your led at 10:41 and another alarm that turns the led off at 10:42. There is an example sketch for the TimeAlarm library that you could use as a starting point.

DarkElf

you are write, English is my second language so sorry about errors

what I meant is,

if I set alarm clock to least 1 minute, after reaching it's full power it resets and starts powering on again

my objective is to accomplish turning bulb on slowly let's say for 30 seconds and than bulb stays on while alarm is on, no matter how long alarm lasts, while in my case it resets and stars over again

now, alarm time and powering on time are syn synchronized so that problem isn't visible...

Go Up