Can this program for a timed switch be improved? O8M2, DS1307 clock, latching relay.

parto

Member
I wonder whether anyone with Picaxe programming experience would be willing to take a look at what I've put together for a power supply switch program, to see whether I'm being as efficient with the Picaxe power as I can be? I've put together a Picaxe 08M2, a DS1307 clock, and a latching relay. This combination lets me delay turning on 12-V power to a remote camera system until a specified date, which will be about a month after the whole system is deployed. The relay I think needs to be high on one side and low on the other side to turn the 12-V switch on, and vice-versa for off.

The Picaxe, clock, and relay will be powered with three AAA batteries, which I think will last for the period (although I'm not confident I've been able to measure the small current accurately).

I had some questions as I went along, which I've put into the comments. I'll appreciate any help that will make the program more efficient or otherwise more reliable. Thanks!
_______________________________________________________________
Code:
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 cam_on = b8
cam_on = 0

i2cslave %11010000, i2cslow, i2cbyte               [i];set DS1307 slave address

; Initialise the clock (actual settings will be put in pre-field). Decimal numbers follow dollar sign; they are converted to BCD automatically when written to the clock:[/i]

let day = $04	[i]; Written to clock in BCD format, numbers >9 not in correct decimal format in debug[/i]
let year = $16	[i]; Written to clock in BCD format, numbers >9 not in correct decimal format in debug[/i]
let month = $04	[i]; Written to clock in BCD format, numbers >9 not in correct decimal format in debug[/i]
let date = $26	[i]; Written to clock in BCD format, numbers >9 not in correct decimal format in debug[/i] 
let hour = $16	[i]; Written to clock in BCD format, numbers >9 not in correct decimal format in debug[/i] 
let mins = $02	[i]; Written to clock in BCD format, numbers >9 not in correct decimal format in debug[/i] 
let seconds = $00	[i]; Written to clock in BCD format, numbers >9 not in correct decimal format in debug[/i]
let control = %00010000 [i]; Enable output at 1Hz[/i] 

writei2c 0,(seconds,mins,hour,day,date,month,year,control)   [i];initialize clock time with values above[/i]
pause 500                                                                     [i];how long is the pause needed to write the clock data?[/i]

Pause 10000                                                                  [i]; do I need to provide this 10 seconds to move the jumper over so that I can use Pin 0?[/i]

low c.4                                                                         [i]; Sets to low the pin that outputs to the "Set", or "S" post on the latching relay.[/i]
high c.0
pause 300                                                                     [i]; Sets to high the pin that outputs to the "Reset", or "R" relay post. When this "R" is high and the "S" is low, the camera system power is switched off. I think 300 milliseconds is the recommended switch time.[/i]
low c.0                                                                         [i]; need to stop power to Pin 0 to minimize power use; the switch seems to stay in position OK.[/i]

readi2c 0,(seconds,mins,hour,day,date,month,year)             [i];read the clock time pre-loop; this sequencing is the i2c format[/i]

pause 500                                                                     [i]; is this pause needed, for time to read the clock?[/i]

do while year<$17 and month<$10                                   [i]; run this loop until October, when the camera system's power will be shut off. The year limit is to keep it from starting the loop again in January (when the month is again <10).[/i]

readi2c 0,(seconds,mins,hour,day,date,month,year)             [i];read the clock time[/i]

[i];pause 100?[/i]

if month>$05 and date>$04 and cam_on = 0 then               [i]; This turns on the camera system power on on 5 June 2016.[/i]

high c.4
pause 300
low c.4
low c.0
cam_on = 1

endif                                                                            [i]; This exits the IF condition. Now that cam_on = 1, the conditional segment is skipped on subsequent loops.[/i]

disablebod                                                                    [i]; This disables the brownout protection, which lowers power consumption during the sleep time.[/i]

sleep 37565                                                                  [i]; Sleep then wake up, the sleep time should be 37,565 seconds times 2.3, or 86,400--the number of seconds in a day. Thus, this loop to check for the year, month, and (in the IF statement) the date and "cam_on" variable should occur once a day.[/i]

enablebod

loop                                                                             [i]; Go to top of loop and check to see if decimal month is still <$10 and decimal_year is still <$17.[/i]

low c.4                                                                         [i]; Month is >$09 or Year > $16 so the loop has been exited; switch off the power to the camera system.[/i]
high c.0
pause 300
low c.0

end
 
Last edited:

lbenson

Senior Member
The code looks good to me.

If you have block indentation (spacing in after do, if, etc.) it will show up in your post if you put "[ code]" before your code and" [ /code]" after (leaving out the spaces). You can edit your post to do this, which will make the code easier for forum readers to scan.

If you have a digital volt meter which can read amps, you might test your circuit. If you are in the tens of microamps when you are sleeping, you should be good (and quite possibly would be even without sleep with readings in the milliamps if you're working off of a 12V battery).
 

parto

Member
OK, I fixed up the code to make it easier to read. I am working on reading the current flow. Thanks.
 
Top