Count down to Christmas

Randy5140

Member
Hello All!

I got a request from my daughter to build a count down timer to Christmas for two of my grandkids.

I have a few of the RTC I2C DS1307 AT24C32 Real Time Clock Modules from Ebay, (China) and thought I could use one to do it.

what I would like to do is get the date from the module and display the time left until Dec. 25th in days and hours.

But,, I am not sure how to work the math without it getting complicated.(at least or me)

I do know how to get the date and time from the module, just need help getting 2 + 2 not to equal 6 :eek:


I have looked on the forum but my searches don't seem to return anything that applies to my project.

Any help would be greatly appreciated!

Below is the code I am working with so far

Thanks


Check_Time:

HI2Csetup I2Cmaster, %11010000, I2Cslow, I2Cbyte 'SETS UP THE TIME TEMP MODULE
readi2c 0, (b23,b24,b25,b26,b27,b28,b29) 'READS THE TIME TEMP MODULE
PM_AM ="P" : IF b25 < $12 then :pM_AM = "A" : ENDIF 'MATH TO SET CORRECT TIME INDICATOR PM or AM
IF b25 = $20 OR b25 = $21 THEN : LET b25 = b25 - $6 : ENDIF
IF b25 > $12 THEN : LET b25 = b25 - $12 : ENDIF '24 to 12 hour format
IF b25 = $0 THEN : b25 = $12 : ENDIF
'pause 200
bcdtoascii b23,b40,b41 'Secs Convert to ASCII
bcdtoascii b24,b30,b31 'Mins
bcdtoascii b25,b32,b33 'Hours b42 shows clock time to chage to military substitue b25 for b42
bcdtoascii b26,b42,b43 'DayOfWeek;
bcdtoascii b27,b34,b35 'Date
bcdtoascii b28,b36,b37 'Month
bcdtoascii b29,b38,b39 'Year



SEROUT LCD,LCD_Baud,(254,128,b32,b33,":",b30,b31,b23,"M ",b36,b37,"/",b34,b35,"/",b38,b39) 'SENDS TIME TO LCD
if b36 = 49 and b37 = 50 and b34 = 50 and b35 = 53 then '"b" veriable numeric equiverants,,48=0,49=1,50=2,51=3,52=4,53=5,54=6,55=7,56=8,57=9
serout Lcd,lcd_baud,(254,192,"Merry Christmas")
PAUSE 200
ENDIF


goto Check_Time
return
 
Last edited:

Buzby

Senior Member
One way is to think about it backwards !.

Make 3 PICAXE variables : DaysLeft, HoursLeft, MinsLeft

Preset the values to the current time left. ( Use a web based countdown clock, once !. )

Every minute decrement MinsLeft.
If MinsLeft = 0 then dec HoursLeft
etc. etc.


No need for any calculations in PICAXE, just decrements.

Cheers,

Buzby
 

hippy

Ex-Staff (retired)
Not sure if it's how marks's example does it, but I'd do it by working out how many days from 'now' until Christmas then multiply by 24 for the number of hours to go, then add a fudge factor for exactly what hour Christmas is at. Days and hours to go is easyish enough to calculate but should factor in DST changes.

I don't disagree with Buzby's solution but it would be nice to have a normal clock which can be switched any time to a 'time until' mode, festival, holiday, birthday, the weekend, etc.
 

Bill.b

Senior Member
Hi Randy

This is my Days to christmas display.

the progam contains an algorithm to calculate the number of days remaining to christmas day.
It also compensates for leapyears.

The displays used are a large 7 seg LED.

Code:
'Days to christmas display
'picaxe 28x2
'  WB 6th June 2010

symbol seconds 	= b0
symbol mins 	= b1
symbol hour 	= b2
symbol day 		= b3
symbol date 	= b4
symbol month 	= b5
symbol year 	= b6
symbol control 	= b7
symbol temp 	= b8
symbol Unit 	= b10
symbol tens 	= b11
symbol hundred 	= b12
symbol hoursleft	= b15
symbol minleft	= b16
symbol secleft	= b17

symbol dayCount 	= w12
symbol daycalculation = w13
symbol leapyearCalc = w14
symbol intday  	= b18


dirsB=%11111111

setfreq m16
' uncomment this line to update the clock time
'goto start_clock 

i2cslave %11010000, i2cslow, i2cbyte    ' set DS1307 slave address
readi2c 0,(seconds,mins,hour,day,date,month,year)
gosub CalDays  '
DayCount = daycalculation
	


main:
	if pina.0=1 then          ' set days to christmas when input 1 is set
		 gosub CalDays
		 DayCount = daycalculation
		 'gosub StorDay
	 endif
	
	i2cslave %11010000, i2cslow, i2cbyte    ' set DS1307 slave address
	readi2c 0,(seconds,mins,hour,day,date,month,year)

	gosub display
	if hour <> $00 then  main  ' set time to decrement days display
	if mins <> $10 then main
	if Seconds < $60 then
		gosub CalDays
		DayCount = daycalculation
		pause 1100
	endif
	 
	goto main
' .---------------------------------------------------------------------------------------.
' | Display days to christmas on LED panel,  the BCD data for the display is  multiplexed.| 
' | b9 bit 0 - 3 = BCD data to the 7 segment decoder. B9 bit 5 - unit driver,             | 
' | bit 6 - tens driver and bit 7 - hundreds driver                                       |
' '---------------------------------------------------------------------------------------'
	
display:

for w15 = 1 to 1000
bintoascii daycount,unit,tens,hundred,b13,b14    
 let b9 = b12 & 15					 'setbit b9,7
 if b9 <> 0 then
	setbit b9,7
	b32=0
else
	b9=0
	let b32 = 1
endif
 
 let pinsb=b9		'  Set output pins for hundreds display
 pause 10

let b9=b13 & 15    
if b9 = 0 and b32 = 1 then
	b9=0
else
	
	setbit b9,6
endif

let pinsb=b9   	' set output pins for tens display
 pause 10
let b9=b14 & 15
setbit b9,5

let pinsb=b9	' set output pins for unit display
next w15
hoursleft = 24-hour
minleft = 60-mins
secleft = 60-seconds
return
	

' .-------------------------------------------------------------------------------------.
' |  Calculate days to chistmas when power is switched On or at 5 min past midnight     |
' '-------------------------------------------------------------------------------------'

CalDays:
	b20=bcdtobin b6		'determine if it is a leap year
	let b19 =b20 // 4		' if year is divisable by 4 then it is a leapyear (manual change is 
					' required for leapcentry).
	if b19 <> 0 then
		let leapyearCalc=364	'normal year
		b19 = 0
	else
		b19 = 1	'leapyear
		let leapyearCalc=364
	endif
	intday =bcdtobin b4		' Convert data to binary for calculation

	' Calculate the number of days remaining to christmas  intday  = current date (Day)
	select case month 
		case $01 								'January
			daycalculation = leapyearCalc - 6 - intday + b19
		case $02 								'Febuary
			daycalculation = leapyearCalc - 36 - intday + b19
		case $03 								'March
			daycalculation = leapyearCalc - 64 - intday 
		case $04 								'April
			daycalculation = leapyearCalc - 95 - intday 
		case $05 								'May
			daycalculation = leapyearCalc - 125 - intday 
		case $06 								'June
			daycalculation = leapyearCalc - 156 - intday 
		case $07 								'July
			daycalculation = leapyearCalc - 186 - intday 
		case $08 								'August
			daycalculation = leapyearCalc - 217 - intday 
		case $09 								'September
			daycalculation = leapyearCalc - 248 - intday 
		case $10 								'October
			daycalculation = leapyearCalc - 278 - intday 
		case $11 								'November
			daycalculation = leapyearCalc - 309 - intday 
		case $12 								'December up to 24th
			daycalculation = leapyearCalc - 339 - intday 
	endselect
	if month = $12 and date>$24 then				'December (after chistmas day)
		daycalculation = leapyearCalc - intday +25
	endif
	
return

	
'write time and date e.g. to 11:00:00 on Thurs 25/10/13
start_clock:
	i2cslave %11010000, i2cslow, i2cbyte
	let seconds = $00	' 00 -59 sec Note all BCD format
	let mins    = $00	' 00-59 min Note all BCD format
	let hour    = $11	' 0-12 or 00-23 Note all BCD format
	let day     = $05	' 03 Note all BCD format
	let date    = $25	' 01-31 Note all BCD format
	let month   = $10	' 01-12 Note all BCD format
	let year    = $13      ' Note all BCD format
            let control = %00010000 ' Enable output at 1Hz
	writei2c 0,(seconds,mins,hour,day,date,month,year,control)
end
daystoxmas.jpg

you can leave out the resonator for the latetest 20x2s


Bill
 
Last edited:

Randy5140

Member
Thanks and a mod to code

Many Thanks all who have helped out here!

I have taken it upon myself to modify Marks' and Bill.B's codes and came up with what I think is one that will work running from any date during the year.

I hope they are not upset with me on doing this.

I am posting the code below, anyone please look at it and if you see any problems or ways to shorten it I will appreciate it! :)

Again Thanks to all!!!





Code:
'This code is a compilation of codes originated by "MARKS" and "Bill.B"
'This code will calculate the remaining days untill Christmas from any Date.
'In other words it could run all year long starting at any date.
'I believe its working correctly, but feel free to tweak it.



#picaxe 20x2	
	
	SYMBOL character1    	= B2
	SYMBOL character2    	= B3  
	SYMBOL character3    	= B4  
	SYMBOL character4    	= B5  
	SYMBOL character5    	= B6  
	SYMBOL character6    	= B7
	SYMBOL hours         	= B8
             SYMBOL mins          	= B9
	SYMBOL secs          	= B10
	SYMBOL day           	= B11 
	SYMBOL date          	= B12  
	SYMBOL month         	= B13
	SYMBOL year          	= B14
	SYMBOL PM_AM         	= B15 
	SYMBOL intday  	    	= b18
	SYMBOL dayCount 	    	= w12
	SYMBOL daycalculation 	= w13
	SYMBOL leapyearCalc   	= w14
		 
INIT:            
  	pause 2000  'allows time for everything to power up.
     		 



Main:
  	'uncomment line below if you only want to show CountDown Until Christmas
  	'Goto DaysToChristmas

	 
'_______________________________Clock and Date  Display___________________________________
	
	
	HI2Csetup I2Cmaster, %11010000, I2Cslow, I2Cbyte  ' setsup 3702 clock module        
	 	HI2Cin $0,   (secs,mins,hours,day,date,month,year) ' read time
	
	
    		PM_AM ="P" : IF hours < $12 then :PM_AM = "A" : ENDIF       'indicate PM  or AM
    
    	If hours = $20 OR hours = $21 THEN 
     		Let hours = hours - $6 : ENDIF
    	If hours > $12 THEN : LET hours = hours - $12 : ENDIF       '24 to 12 hour format
    	If hours = $0 THEN  : hours = $12 : ENDIF
    
   		BcdTOASCII hours,character1,character2 : IF character1 = "0" THEN : character1 = " " : ENDIF ' zero blanking character1
   		BcdTOASCII mins ,character3,character4
   		BcdTOASCII secs ,character5,character6 
 		sertxd (CR,LF,character1,character2,":",character3,character4,":",character5,character6," ",PM_AM,"M    ") '(11.58.00 PM)
 
	'DateMonthYearDisplay
   		BcdTOASCII date ,character1,character2
   		BcdTOASCII month,character3,character4
   		BcdTOASCII year ,character5,character6
 		sertxd (character3,character4,"/",character1,character2,"/20",character5,character6,CR,LF)' (05/11/13)
   

   
	
		
		
		
		
		
		
		
DaysToChristmas:			 

     '_______________________________'CALCULATE DAYS TO CHRISTMAS AND DETURMINE IF LEAP YEAR______________________________________
     
     
     HI2Csetup I2Cmaster, %11010000, I2Cslow, I2Cbyte        
		HI2Cin $0,   (secs,mins,hours,day,date,month,year) ' read time
     
		b20=bcdtobin b14 'b6		'determine if it is a leap year
		let b19 =b20 // 4		' if year is divisable by 4 then it is a leapyear (manual change is required for leapcentry).
		
	If b19 <> 0 then
		let leapyearCalc=364	'normal year
		b19 = 0
	else
		b19 = 1	'leapyear
		let leapyearCalc=364
	endif
		intday =bcdtobin b12 + 1		' Convert data to binary for calculation

	' Calculate the number of days remaining to christmas  intday  = current date (Day)
	select case month 
		case $01 								'January
			daycalculation = leapyearCalc - 6 - intday + b19
		case $02 								'Febuary
			daycalculation = leapyearCalc - 36 - intday + b19
		case $03 								'March
			daycalculation = leapyearCalc - 64 - intday 
		case $04 								'April
			daycalculation = leapyearCalc - 95 - intday 
		case $05 								'May
			daycalculation = leapyearCalc - 125 - intday 
		case $06 								'June
			daycalculation = leapyearCalc - 156 - intday 
		case $07 								'July
			daycalculation = leapyearCalc - 186 - intday 
		case $08 								'August
			daycalculation = leapyearCalc - 217 - intday 
		case $09 								'September
			daycalculation = leapyearCalc - 248 - intday 
		case $10 								'October
			daycalculation = leapyearCalc - 278 - intday 
		case $11 								'November
			daycalculation = leapyearCalc - 309 - intday 
		case $12 								'December up to 24th
			daycalculation = leapyearCalc - 339 - intday 
	endselect
	
	If month = $12 and date > $25 then				'December (after chistmas day)
		daycalculation = leapyearCalc - intday +27
	endif
     		hours  = hours  / 16 * 250 + hours  ' convert BCD to Binary
     		hours = 23-hours
     		mins  = mins  / 16 * 250 + mins  ' convert BCD to Binary
     		mins = 59-mins
     		secs  = secs  / 16 * 250 + secs  ' convert BCD to Binary
     		secs = 60-secs 
     		
     		
     		
     		
     		
     '______________________________________output section:____________________________________________
            'Insert code in this section to direct output to what ever system you are using, 
                           'for instance LCD or Segmented LED etc.
     
     
     		
     		
     IF month = $12 and date = $25 THEN
     		sertxd ("0 Days 0 Hours 0 Minutes 0 seconds",cr,lf)
      	goto message
     else  
  		sertxd (#daycalculation," Days   ",#hours," Hours   ",#Mins," Mins   ",#secs," Secs",CR,LF)      
     		goto main
     endif
     
 Message: 
  sertxd ("Merry Christmas EveryOne! lol") 
     goto main
     
     '____________________________________end output section___________________________________________
 

Paix

Senior Member
@Busby, TTL and Solder' what no Fairchild RTL and having to work out the number of drives available. I designed my first shift register before I discovered TTL and the fact that I had just designed a basic component the hard way. :)

I once read the Unix Man page about TIME and realised that at the time it was more a case of national politics than science. At least today we have 'a little more' international agreement about when DST changes back and forward. I also had some interesting experiences over a three year period with an industrial strength database due to DST changes. Between us and the database manufacturer all getting our ducks fully into a line to get a consistent view of past, present and future, I realised that time becomes a very complex subject once you start to examine it.

Almost like Schrodinger's cat in fact.

Happy forthcoming Christmas . . .
 

Buzby

Senior Member
... time becomes a very complex subject once you start to examine it.
Time2.PNG

Above is the cover of the magazine I chose to read while on a train journey yesterday.

Seven hours to move 395 miles !.

An object lesson in the stupefying effect of traveling through time and space using the British railway system !.
 

papaof2

Senior Member
An average speed of about 56MPH? Better than you can do on some of the Mxx roads (based on driving 1100 miles in two weeks in the summer of 2002). When the Mxx roads work, they're nice; but they suffer from occasional "no see ums" - stopped for an extended period but no evidence of an accident or other traffic-stopper as you finally move on down the road.
 
Top