timer

Started by jose_hinestrosa March 21, 2004
Hi,

Im trying to turn on a fan every three minutes using the BX. Can somebody
give an idea how to accomplish this?

Thank you.



--- In , "jose_hinestrosa"
<jose_hinestrosa@y...> wrote:
> Im trying to turn on a fan every three minutes using the BX.
> Can somebody give an idea how to accomplish this?

Call the Timer() function to get the current "seconds since midnight"
count and store that value. From time to time, call Timer() again
and see if it is 180 seconds greater than the first reading. If so,
turn the fan on. Presumably you'll want to do something similar to
turn it off.

The one "gotcha" is that the value that Timer() return rolls over
every 24 hours. You can detect this by determining if the value
returned is less than when you called it recently.



Jose..try this.Ii havent tested it but it should be close. Suggest you
test code with 10.0 secs instead of 180 to start with.
Neil

dim FanOnTime as single
FanOnTime = 0.0 'init variable
call putpin(12,0) 'start with fan off

Public Sub main()

do
'Turn the fan on every 180 seconds.
if (timer > fanOnTime+ 180.0) then
call putpin (12,1) 'turn
fan on on pin 12.
call putpin(25,0) 'turn
on LED also
FanOnTime = timer 'store the
time
end if
'turn the fan off after 5 seconds
if (timer > fanOnTime+5.0) and (getpin(12) = 1) then
call putpin(12,0) 'turn fan off
call putpin(25,1) 'turn led off
end if
'at midnight,
if (fanOnTime>timer) then
fanOnTime = timer 'take care of
midnight rollover
end if loop

end sub

'Note that the fan must be connected via a suitable relay and
transistor. Note also that to turn on the green or red led ( 25 & 26),
you set the pin to 0, not 1, as you would do with a normal I/O pin.
jose_hinestrosa wrote:

> Hi,
>
> Im trying to turn on a fan every three minutes using the BX. Can somebody
> give an idea how to accomplish this?
>
> Thank you. >
> *
> <http://rd.yahoo.com/SIGcngeldp/M(1792.4727319.5879690.1261774/D=egroupweb/S06554205:HM/EXP80007282/A 58224/R=0/SIG6652qbq/*http://my.yahoo.com/promo/ppets.html >
>
> *>.






Hey Neil:

I really appreciate youre help. I will try it tonight
and I hope it works. I was going in another directin
but this way seems better.

Thank you very much,

Jose.

--- Neil Jepsen <> wrote:
> Jose..try this.Ii havent tested it but it should be
> close. Suggest you
> test code with 10.0 secs instead of 180 to start
> with.
> Neil
>
> dim FanOnTime as single
> FanOnTime = 0.0 'init variable
> call putpin(12,0) 'start with fan off
>
> Public Sub main()
>
> do
> 'Turn the fan on every 180 seconds.
> if (timer > fanOnTime+ 180.0) then
> call putpin (12,1)
> 'turn
> fan on on pin 12.
> call putpin(25,0)
> 'turn
> on LED also
> FanOnTime = timer
> 'store the
> time
> end if
> 'turn the fan off after 5 seconds
> if (timer > fanOnTime+5.0) and (getpin(12)
> = 1) then
> call putpin(12,0)
> 'turn fan off
> call putpin(25,1)
> 'turn led off
> end if
> 'at midnight,
> if (fanOnTime>timer) then
> fanOnTime = timer
> 'take care of
> midnight rollover
> end if > loop
>
> end sub
>
> 'Note that the fan must be connected via a suitable
> relay and
> transistor. Note also that to turn on the green or
> red led ( 25 & 26),
> you set the pin to 0, not 1, as you would do with a
> normal I/O pin.
> jose_hinestrosa wrote:
>
> > Hi,
> >
> > Im trying to turn on a fan every three minutes
> using the BX. Can somebody
> > give an idea how to accomplish this?
> >
> > Thank you.
> >
> >
> >
> > *
> >
>
<http://rd.yahoo.com/SIGcngeldp/M(1792.4727319.5879690.1261774/D=egroupweb/S06554205:HM/EXP80007282/A 58224/R=0/SIG6652qbq/*http://my.yahoo.com/promo/ppets.html>
>
> >
> >
> >
> >
>

> > *>.
> >
>


__________________________________




--- In , Neil Jepsen <njepsen@i...> wrote:
The code that Neil provided is pretty much what I was alluded to in
my response to your post. I do have a comments on the code as
written.

1) When the clock rolls over, the code simply resets the "start time".

> if (fanOnTime>timer) then
> fanOnTime = timer 'take care of midnight rollover
> end if

While this does work, it may cause you to completely miss a cycle.
Consider the case where the last fanOnTime was 179 seconds before
midnight. The Timer() value will never get to fanOnTime + 180
because that is one second past midnight. When the code above sees
that the timer has rolled over, it sets fanOnTime to the current time
so the earliest that the fan will be turned on next will be 180
seconds past midnight. The elapsed time between these two fan on
cycles will thus be 179+180 seconds.

A better way, perhaps, to handle the rollover problem is to decrease
the fanOnTime by 24 x 60 x 60 seconds when rollover is detected.
Yes, that will make it negative but the comparison will correctly
detect the next required on time.

So, put this code at the top of the loop before anything else is done
(and delete the rollover code at the bottom of the loop):

if (fanOnTime > Timer()) then
' account for clock rollover
fanOnTime = fanOnTime - SECONDS_PER_DAY
end if

where SECONDS_PER_DAY is defined as follows:

Const SECONDS_PER_DAY as Single = 86400.0

2) The code as written may exhibit a small amount of "creep" in the
time that the fan turns on because it sets fanOnTime to the current
time when it begins a cycle. If you want to ensure that each
successive fanOnTime is an integral multiple of 180 seconds later
than the last one, change this line:

fanOnTime = timer 'store the time

to this:

fanOnTime = fanOnTime + Fix((Timer() - fanOnTime) / 180.0) * 180.0

Note: You should probably use a defined constant in place of the
180.0 for simpler maintenance.

3) The code that attempts to determine if the fan is on by calling
GetPin() probably won't work for reasons discussed in another recent
post (see http://groups.yahoo.com/group/basicx/message/15581). To
make this work, you'll have to introduce a state variable to reflect
the fan state or use code similar to that which I offered as a
solution in the above thread which checks a bit in Register.PortC or
Register.PortA depending on the pin being used. See also
http://www.kinzers.com/don/BX24/Routines/PinFunc.bas.



--- In , "Don Kinzer" <dkinzer@e...> wrote:
> http://www.kinzers.com/don/BX24/Routines/PinFunc.bas.

That link should not have the trailing dot.
http://www.kinzers.com/don/BX24/Routines/PinFunc.bas



Don
You are correct of course. I knew about the creep or catchup at midnight
but in his particular application and in the interests of simplicity ,
I don't think it will matter.
Thanks for pointing out the getpin problem with outputs. I'd completely
forgotten about that and it might well have tripped him up. Again in
the interests of simplicity, it might be better to use a second boolean
variable for the fan state so he doesn't have to learn about passing
variables at this early stage. That ( simplicity) is also why I used
the pin number rather than a label for pin 12.
neil Don Kinzer wrote:

> --- In , Neil Jepsen <njepsen@i...> wrote:
> The code that Neil provided is pretty much what I was alluded to in
> my response to your post. I do have a comments on the code as
> written.
>
> 1) When the clock rolls over, the code simply resets the "start time".
>
> > if (fanOnTime>timer) then
> > fanOnTime = timer 'take care of midnight rollover
> > end if
>
> While this does work, it may cause you to completely miss a cycle.
> Consider the case where the last fanOnTime was 179 seconds before
> midnight. The Timer() value will never get to fanOnTime + 180
> because that is one second past midnight. When the code above sees
> that the timer has rolled over, it sets fanOnTime to the current time
> so the earliest that the fan will be turned on next will be 180
> seconds past midnight. The elapsed time between these two fan on
> cycles will thus be 179+180 seconds.
>
> A better way, perhaps, to handle the rollover problem is to decrease
> the fanOnTime by 24 x 60 x 60 seconds when rollover is detected.
> Yes, that will make it negative but the comparison will correctly
> detect the next required on time.
>
> So, put this code at the top of the loop before anything else is done
> (and delete the rollover code at the bottom of the loop):
>
> if (fanOnTime > Timer()) then
> ' account for clock rollover
> fanOnTime = fanOnTime - SECONDS_PER_DAY
> end if
>
> where SECONDS_PER_DAY is defined as follows:
>
> Const SECONDS_PER_DAY as Single = 86400.0
>
> 2) The code as written may exhibit a small amount of "creep" in the
> time that the fan turns on because it sets fanOnTime to the current
> time when it begins a cycle. If you want to ensure that each
> successive fanOnTime is an integral multiple of 180 seconds later
> than the last one, change this line:
>
> fanOnTime = timer 'store the time
>
> to this:
>
> fanOnTime = fanOnTime + Fix((Timer() - fanOnTime) / 180.0) * 180.0
>
> Note: You should probably use a defined constant in place of the
> 180.0 for simpler maintenance.
>
> 3) The code that attempts to determine if the fan is on by calling
> GetPin() probably won't work for reasons discussed in another recent
> post (see http://groups.yahoo.com/group/basicx/message/15581).
> <http://groups.yahoo.com/group/basicx/message/15581%29.> To
> make this work, you'll have to introduce a state variable to reflect
> the fan state or use code similar to that which I offered as a
> solution in the above thread which checks a bit in Register.PortC or
> Register.PortA depending on the pin being used. See also
> http://www.kinzers.com/don/BX24/Routines/PinFunc.bas. > *
> <http://ashnin.com/clk/muryutaitakenattogyo?YHE21611&yhad50447" target="_blank" rel="nofollow">http://rd.yahoo.com/SIGc7bmnrh/M&8585.4521611.5694062.1261774/D=egroupweb/S06554205:HM/EXP80085908/A50447/R=0/SIG45hvqf1/*http://ashnin.com/clk/muryutaitakenattogyo?YHE21611&yhad50447 >
>
> *>.




Can anyone recommend a good 10bit (or more) serial DAC

neil

Don Kinzer wrote:

> --- In , "Don Kinzer" <dkinzer@e...> wrote:
> > http://www.kinzers.com/don/BX24/Routines/PinFunc.bas.
>
> That link should not have the trailing dot.
> http://www.kinzers.com/don/BX24/Routines/PinFunc.bas >
> *
> <http://ashnin.com/clk/muryutaitakenattogyo?YHE21611&yhad50447" target="_blank" rel="nofollow">http://rd.yahoo.com/SIGcqhichb/M&8585.4521611.5694062.1261774/D=egroupweb/S06554205:HM/EXP80089016/A50447/R=0/SIG45hvqf1/*http://ashnin.com/clk/muryutaitakenattogyo?YHE21611&yhad50447 >
>
> *>.



Neil Jepsen wrote:

> Can anyone recommend a good 10bit (or more) serial DAC that is
> available ex stock. 25x required
>
> neil
>
> Don Kinzer wrote:
>
> > --- In , "Don Kinzer" <dkinzer@e...> wrote:
> > > http://www.kinzers.com/don/BX24/Routines/PinFunc.bas.
> >
> > That link should not have the trailing dot.
> > http://www.kinzers.com/don/BX24/Routines/PinFunc.bas
> >
> >
> >
> > *
> >
> <http://ashnin.com/clk/muryutaitakenattogyo?YHE21611&yhad50447" target="_blank" rel="nofollow">http://rd.yahoo.com/SIGcqhichb/M&8585.4521611.5694062.1261774/D=egroupweb/S06554205:HM/EXP80089016/A50447/R=0/SIG45hvqf1/*http://ashnin.com/clk/muryutaitakenattogyo?YHE21611&yhad50447
> <http://ashnin.com/clk/muryutaitakenattogyo?YHE21611&yhad50447>>" target="_blank" rel="nofollow">http://rd.yahoo.com/SIGcqhichb/M&8585.4521611.5694062.1261774/D=egroupweb/S06554205:HM/EXP80089016/A50447/R=0/SIG45hvqf1/*http://ashnin.com/clk/muryutaitakenattogyo?YHE21611&yhad50447>>
>
> >
> >
> >
> >
> > *>.
> >
> >
>
> *>.




--- In , Neil Jepsen <njepsen@i...> wrote:
> Can anyone recommend a good 10bit (or more) serial DAC that is
> available ex stock. 25x required.

I used an LTC1451 (12-bit) recently and was pleased with it. DigiKey
seems to have plenty of the 8-pin DIP version in stock.