Shop OBEX P1 Docs P2 Docs Learn Events
Random timing output — Parallax Forums

Random timing output

JSGrewalJSGrewal Posts: 9
edited 2008-07-04 22:53 in BASIC Stamp
First off, thanks for all the great topics in this forum. Because of this place, its been fairly easy for one of my first programming electronic projects in a long long time!

So I am using a BS2 to make a controller for a large humidor. I have been able to get the LCD display and temp/ humidity control to work great. What I want to do is randomly turn on a fan every hour or two for a few minutes. I know I can use an RTC and track time that way, but I dont want to if I can avoid it. The time is not all that critical as long as it turns on every once in a while and runs for a while.


Any suggestions?

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-07-04 22:53
    You have some kind of repeating process in your program, sort of like "read the temp/humidity", "display stuff on LCD", etc. This takes a roughly fixed amount of time that you can measure. Basic has a random operator. All you have to do is initialize a "state variable" to zero and a time counter to zero. In your main loop:
    If the counter is zero, generate a random number
    --- If the state variable is zero, adjust the range of the random number and save it in the counter
    --- Turn on the fan
    --- Set the state variable to 1
    --- If the state variable is 1, adjust the range of the random number and save it in the counter
    --- Turn off the fan
    --- Set the state variable to 0
    If the counter is not zero, decrease it by one

    The adjustment of the random number depends on whether you have state 0 or 1 and the random
    number becomes the number of times your program goes through its main loop for the time needed
    for on or off. The minimum time is the base value, the difference between maximum and minimum
    becomes a scale factor for the random number. In other words, you take the random number, multiply
    by the scale factor and add the base value. The result is the number of times you go through your main
    loop before you change state. The actual numbers depend on the amount of time it takes to go through
    the main loop and how long you want the fan on or off.
Sign In or Register to comment.