Shop OBEX P1 Docs P2 Docs Learn Events
Timing Issues — Parallax Forums

Timing Issues

SeanDFSeanDF Posts: 12
edited 2008-11-09 17:59 in BASIC Stamp
Hey guys,
After several months I am picking back up on a project. I'm gonna list the program I have here. If you notice in SYSTEM CONTROL section, I am asking it to turn 5 High. I would like to keep this pin on High for 20 seconds, while continuing the rest of the program. I'm a lil stumped. Are there any special ways I can make the chip count out 20 seconds while continuing the rest of the program? I'm almost thinking of adding a secondary 555 timer to the mix somehow, but was just wondering if anyone saw a way i could make that pin stay high, without an external timer..




Post Edited (SeanDF) : 11/11/2008 5:17:50 AM GMT

Comments

  • Carl HayesCarl Hayes Posts: 841
    edited 2008-11-09 12:03
    From your listing it appears that you are trying to implement a sequential machine without designing it first.· What you ought to have done is complete the conceptual design before starting to code.· At various times in your program you want to do different things depending upon what has gone before.

    I'm also building a sequential machine.· It's a controller for a fairly large Onan generator.· Here's what I want it to do:

    Watch the power mains.· If the power goes out, and stays out for a time, start the generator, give it a little time to stabilize, then switch the load (my house) to the generator.· Then continue to watch the mains, and if the utility power comes back on and stays on, switch the house back to the mains, run the generator unloaded for a time to cool it off gracefully, then shut it down.

    Note that the above paragraph is not a design for a controller.· Although it describes a sequential machine, it describes it rather vaguely.· The next step·was to write a document describing the various states the machine will go through.· A·state is a condition such as "watching the mains" or "trying to start the generator" or "trying to stop the generator" and things like that.· The actual design of the machine is in the attached Word document, which you might study as an example of how to·begin such a project as yours.· I wrote this first.· You too should write such a document first.· That's the only way to achieve a clear understanding of what your program is really trying to do.

    The first code that I wrote was a dispatcher.· It's a little loop that executes once a second, timing itself by a single PAUSE statement.· It maintains a variable called STATE, which specifies what the machine is trying to do at the moment (watch the mains, start the generator, etc.)· The machine tries to do only one thing at a time.· There are 23 states, described in the Word document.· The first one (State=0) is "starting up the program".· The second one (State=1) is "running normally, watching the mains".· State=2 is "power went out, seeing if it comes back on quickly", etc.· See the Word document.

    The program is currently in its fifth generation, appears to run correctly, and occupies five Slots in a BS2SX.· An early version, attached, ran in a single slot, and I have attached it.· Together, the two attachments (Word document and program) ought to constitute a reasonably good tutorial for designing the kind of thing I think you're trying to build.· Have a look if you like.

    Notice that the dispatcher loop (at label Mainloop[noparse]:)[/noparse] is exceedingly short and simple.· Note, also, that the little subroutines (one for each State) are simple too.· The only thing that may require study, but not much study, is the way each subroutine (which gets called once per second) decides for itself how many times (how many seconds) it will run.

    Notice that each State subroutine is doing only one thing -- but while it is doing that one thing it is simultaneously monitoring several things and deciding whether to take action by changing the State variable.· Doing one thing, while monitoring several things, is exactly what I think you really·asked how to do.

    For example, in State 3, the program is trying to start the generator.· At the same time, it is monitoring to see whether the mains came back up, monitoring to see whether the generator started (is producing power), and timing itself with a countdown to make sure it hits the starter relay for only 10 seconds, max.



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    · -- Carl, nn5i

    Post Edited (Carl Hayes) : 11/9/2008 1:01:23 PM GMT
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2008-11-09 12:46
    Carl -

    Just a small correction. Either your "Onan3.bsx" program is incorrect, or you are using a BS2p. Check the first line in the program for confirmation.

    Very well documented, and a nice start on a Personal Onan Users Manual, for your application.

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When all else fails, try inserting a new battery.
  • Carl HayesCarl Hayes Posts: 841
    edited 2008-11-09 13:22
    Hi, Bruce --

    I've tested on several BS2 variations, including BS2P.· Currently it's on a BS2SX.· The latest version includes support for a Real Time Clock and an LED display.· Those took a lot of code (almost as much additional as there is in the version I posted).· The reason for the RTC was to allow me to schedule automatic exercise periods (run the generator for a while each Saturday noon), and then I needed the LED and a LOT of additional code to run the LED, so I'd be able to include a way to set the clock ... one thing leads to another.· Currently it's in five Slots on a BS2SX:

    Slot 0 -- dispatcher and State 0.· This Slot is only about 10% full.

    Slot 1 -- States 1 through 10.· This Slot is about 30% full.

    Slot 2 -- States 11 through 19.· This Slot is about 30% full.

    Slot 3 -- States 20 through 22.· This Slot is about 10% full.

    Slot 4 -- RTC and LED support.· This Slot is absolutely full, only about 10 bytes left.· I may have to break this code into two Slots, perhaps by breaking out·the stuff that sets the clock by using only one front-panel switch (which, however, has momentary contacts in two directions and takes two Pins on the Stamp).

    Note how a good modular design makes it pretty easy to separate a project into several Slots when you need more room.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    · -- Carl, nn5i
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2008-11-09 13:43
    Carl -

    The only point that I was trying to make is the $STAMP compiler directive indicates you are using a BS-2p while the name of the program uses a file type of BSX. I honestly don't know if there is any compiler impact to that, but it's certainly confusing while the rest of the documentation is fine.

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When all else fails, try inserting a new battery.
  • Carl HayesCarl Hayes Posts: 841
    edited 2008-11-09 16:31
    Bruce, I hve just run an experiment. On my hard disk I renamed this file Onan3.cjk (cjk=Carl's junk), and the Stamp editor opened it just fine, and when I saved it back it didn't change the name. Apparently, the first time you save a file from the Stamp editor, it names it (applies a suffix) according to whatever Stamp directive is currently there, and doesn't worry about it after that. If you load it into a different Stamp, it will automatically change the directive but not worry about the suffix on the name.

    Only thing is, if the suffix is something like cjk, then (1) Windows won't know what program to use if you double-click the file in Explorer, and (2) the Stamp editor won't offer it as a choice when you click Open, unless you tell it to list "all files".

    Cheers!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    · -- Carl, nn5i
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2008-11-09 16:51
    Carl -

    I'll leave you with the description and use of the {$STAMP} compiler directive as shown in the PBasic IDE Help File:

    quote

    $STAMP

    The $STAMP directive is a special compiler command that must be included (usually near the top) in a program to indicate the model of BASIC Stamp targeted. The line below is an example of the $STAMP directive (in this case, it indicates that the program is intended for a BASIC Stamp 2):

    ' {$STAMP BS2}

    This line should be entered into your code, usually near the top, on a line by itself. Note that the directive appears on a comment line (the apostrophe (') indicates this) for compatibility with the DOS versions of the editor.

    The 'BS2' in the example above should be changed to indicate the appropriate model of the BASIC Stamp you are using. For example, to use another BASIC Stamp module, enter one (just one) of the following lines into program:

    ' {$STAMP BS1}
    ' {$STAMP BS2}
    ' {$STAMP BS2e}
    ' {$STAMP BS2sx}
    ' {$STAMP BS2p}
    ' {$STAMP BS2pe}
    ' {$STAMP BS2px}

    The directive itself must be enclosed in braces, {...}. There should not be any spaces between the dollar sign, $, and the word STAMP, however, the directive may contain additional spaces in certain other areas. Buttons on the editor tool bar simplify adding or changing the $STAMP directive.

    The $STAMP directive is read and acted upon by the BASIC Stamp Windows Editor any time a source code file is loaded, tokenized, downloaded (run), or viewed in the Memory Map.

    For BS2e, BS2sx, BS2p, BS2pe, and BS2px programs, each editor page can be a separate project, or part of a single project. A project is a set of up to eight files that should all be downloaded to the BASIC Stamp for a single application. Each of the files within the project is downloaded into a separate "program slot". Only the BASIC Stamp 2e, 2sx, 2p, 2pe, and 2px modules support projects (multiple program slots).

    For BASIC Stamp projects (consisting of multiple programs), the $STAMP directive has an option to specify additional filenames. The syntax below demonstrates this form of the $STAMP directive:

    ' {$STAMP BS2e, file2, file3, ..., file8}

    Use this form of the $STAMP directive if a project, consisting of multiple files, is desired. This directive must be entered into the first program (to be downloaded into program slot 0) and not in any of the other files in the project. The file2, file3, etc. items should be the actual name (and optionally the path) of the other files in the project. File2 refers to the program that should be downloaded into program slot 1, file3 is the program that should be downloaded into program slot 2, etc. If no path is given, the path of program 0 (the program in which the $STAMP directive is entered) is used.

    Up to seven filenames can be included, bringing the total to eight files in the project all together. Upon tokenizing, running or viewing program 0 in the Memory Map, the editor will read the $STAMP directive, determine if the indicated files exist, will load them if necessary and change their captions to indicate the project they belong to and their associated program number. After the directive is tokenized properly, and all associated files are labeled properly, tokenizing, running or viewing any program in the Memory Map will result in that program’s entire project being tokenized, downloaded or viewed.

    end quote

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When all else fails, try inserting a new battery.
  • Carl HayesCarl Hayes Posts: 841
    edited 2008-11-09 17:59
    Bruce, the compiler changes the directive as needed, when you load the program into a different model of Stamp. I change from one model to another often. Each time, the compiler says something like, "No BS2SX found. We found a BS2P, though. Do you wanna load your junk into that?" When I click on "yeah, whatever," it changes the directive and proceeds. I'm sorry you are unable to believe this, but it is so anyway.

    Have a nice day.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    · -- Carl, nn5i
Sign In or Register to comment.