Shop OBEX P1 Docs P2 Docs Learn Events
loop interrupt? — Parallax Forums

loop interrupt?

ArchiverArchiver Posts: 46,084
edited 2001-08-30 00:42 in General Discussion
Lou-

In addition to Chuck's excellent suggestions, you might also want to
get in the habit of doing some of the following:

When your Stamp program starts, all of the I/O pins are inputs, so
you don't need to initialize pins to the input state.

It's good practice to set the state of an I/O pin's output latch
prior to making that pin an output. In your code, you'd probably be
setting I/O pins 10, 11 and 12 low by making them outputs at the
beginning. Probably not what you mean to do. A couple of
initialization options:

HIGH 10 'Make pin 10 an output, high -DVD track 1
HIGH 11 'Make pin 10 an output, high -DVD track 2
HIGH 12 'Make pin 10 an output, high -DVD track 3

-or-

OUTS = %0001110000000000 ' initialize output latches
DIRS = %0001110000000000 ' initialize direction bits

PBASIC2 also allows you to use aliases for pin names and predefined
variables. This can greatly simplify your life:

coin_pin VAR IN1
button_pin VAR IN2
DVD_1 CON 10
DVD_2 CON 11
DVD_3 CON 12
i VAR WORD

Loop:
LOW DVD_1 'turn on dvd attract track
FOR i = 1 TO 2000
IF coin_pin = 0 THEN coinInserted ' assumes active low
PAUSE 10 'pause for 1/100 second
NEXT
HIGH DVD_1 'release button
GOTO Loop

coinInserted:
(do the right thing...)
GOTO Loop


In this example you can use "coin_pin" in place of IN1, and DVD_1 in
place of 10, and so on. It's a lot easier to remember, less prone to
typo errors, and you only have to change the pin number once should
you need to change your Stamp's wiring scheme.

Little things, but as your programs grow in size and complexity they
can make a big difference.

Regards,

Steve

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2001-08-29 12:23
    Lou-

    See the manual regarding OUTS and DIRS--it explains it better than I
    can. In short, they provide a quick way to set the output latches
    and input/output directions to desired values.

    > okay, if I want to create aliases for the pin inputs, should I be
    > using 'Coin_Mech VAR IN0' or 'Coin_Mech CON IN0'?

    IN0 is the name for the predefined bit variable whose value (0 or 1)
    is determined by the logic level (low or high) present at I/O pin 0.
    PBASIC won't allow you to treat a variable (predefined or otherwise)
    as a constant (CON). However, you can refer to any variable by a
    more meaningful name by defining an alias for the variable. Think
    of it as a nickname.

    You can only assign a CON alias to a number such as 10 or some other
    constant value.

    I like to simulate a button/switch by connecting a ~4.7K resistor
    from the I/O pin to +5 volts. Attach a jumper wire to the same I/O
    pin. After assuring yourself that the pin is indeed in input mode
    (check your program carefully--if in doubt send DIRS to the DEBUG
    screen), briefly touch the jumper to ground to simulate switch
    closure. In this configuration you are testing for an active low
    closure (IF switch = 0 THEN switchClosed). If the pin is in input
    mode, you won't hurt your Stamp using this technique.

    My guess is you're getting "free plays" because you have nothing
    attached to I/O 0, so it's reading high (i.e. "1") most of the time,
    which is very common for a floating (nothing attached) input. Pull
    the input to ground or +5 volts with the resistor described above to
    establish an unambiguous default state, then test for the opposite
    condition to detect switch closure.

    Regards,

    Steve
  • ArchiverArchiver Posts: 46,084
    edited 2001-08-29 17:11
    Hello-

    First off, I'd like to thank everyone who helped me
    through my stamp->pc setup. I've got it working now
    and I'm eager to get starting coding. I'm building a
    little prototype of a coin operated video game- it's
    pretty simple, but i'm new to pbasic, so i've got a
    quick question.

    so far i've got:

    '========== Initialize ============
    input 1 'Make pin 1 the coin mech input
    input 2 'Make pin 2 the button input
    output 10 'Make pin 10 an output -DVD track 1
    output 11 'Make pin 11 an output -DVD track 2
    output 12 'Make pin 12 an output -DVD track 3

    '======== Attract Startup =========
    Loop:
    LOW 10 'turn on dvd attract track
    PAUSE 1000 'pause for 1 second
    HIGH 10 'release button
    PAUSE 20000 'wait for 20 seconds, repeat
    GOTO Loop:

    How can I set this up to be interrupted by a pulse
    from the coin mech?

    Thanks for the help!

    -Lou

    __________________________________________________
    Do You Yahoo!?
    Make international calls for as low as $.04/minute with Yahoo! Messenger
    http://phonecard.yahoo.com/
  • ArchiverArchiver Posts: 46,084
    edited 2001-08-30 00:34
    Hey Steve-

    Thanks for the help, I've incorporated the pin aliases and got the loop
    working. It took a couple re-reads of your email to really grasp everything
    you explained, but I think I've got it now.

    All but:

    > OUTS = %0001110000000000 ' initialize output latches
    > DIRS = %0001110000000000 ' initialize direction bits

    of course, if I can achieve the same thing with:

    > HIGH 10 'Make pin 10 an output, high -DVD track 1
    > HIGH 11 'Make pin 10 an output, high -DVD track 2
    > HIGH 12 'Make pin 10 an output, high -DVD track 3

    okay, if I want to create aliases for the pin inputs, should I be using
    'Coin_Mech VAR IN0' or 'Coin_Mech CON IN0'?

    I have yet to attach anything to the stamp- I'm a little bit shaky on how to
    wire a button up, or deal with the 5volt pulse from the coin mechanism. I'm
    somewhat afraid (probably with good reason) that I'm going to blow the stamp
    up... My electrical knowledge is pretty weak, and I'm concerned that I will
    overload the board. I understand that I need to add resistors into the
    button, and that the overall sink (amount of current flowing in?) cannot
    exceed 50mA- on the output side I need to wire in a few relays- Oh no, it's
    electrical theory time... Time to do more reading with the radio shack
    catalog in hand-

    Is there some way to 'simulate' an input on a button? Is this what the
    macros are for? I tried setting one up as 'credit = 1', where credit had
    been defined at the beginning of the program- it didn't seem to do anything.

    Thanks again for the help!

    -Lou
  • ArchiverArchiver Posts: 46,084
    edited 2001-08-30 00:42
    I should have added this to the last email, but this is my code so far:
    (I'm having an issue with the coin_mech input, which seems to be giving
    'free games'...)

    DEBUG CLS, "System Startup", CR

    '======== Initialize I/O =========
    input 0
    input 1
    HIGH 10
    HIGH 11
    HIGH 12
    HIGH 13
    HIGH 14
    HIGH 15

    '============ Map I/O ============
    Coin_Mech VAR IN0 'Make pin 0 the coin mech input
    Button_Input1 VAR IN1 'Make pin 1 the button input
    DVD_Track_1 CON 10 'Make pin 10 an output triggering DVD track 1
    DVD_Track_1_Length CON 5000 'Length of DVD track 1
    DVD_Track_2 CON 11 'Make pin 11 an output triggering DVD track 2
    DVD_Track_2_Length CON 5000 'Length of DVD track 2
    DVD_Track_3 CON 12 'Make pin 12 an output triggering DVD track 3
    DVD_Track_3_Length CON 5000 'Length of DVD track 3
    Audio_Switch CON 13 'Make pin 13 an output -audio switcher
    Video_Switch CON 14 'Make pin 14 an output -video switcher
    Effect_Switch CON 15 'Make pin 15 an output -effect relay

    '===== Initialize Variables =======
    pulseCount VAR WORD 'establist pulsecount variable
    pulseCount = 0
    credit VAR NIB 'establish credit variable as 0-15
    credit = 0
    gameInPlay VAR BYTE 'establish if game is currenly in play
    gameInPlay = 0
    gameCount VAR WORD 'establish gamecount variable
    gameCount = 0
    reps VAR WORD 'establish counter loop


    '===== Output Debugging Info ======
    GOSUB System_Status_Sub

    '======== Game_Logic Loop =========
    Game_Logic:
    GOSUB System_Status_Sub
    IF credit=0 THEN Attract_Loop 'if credits are not present
    IF credit>0 THEN Game_Play 'if credits are present
    GOTO Game_Logic


    '========= Attract_Loop ==========
    Attract_Loop:
    GOSUB System_Status_Sub
    LOW DVD_Track_1 'turn on DVD_Track_1
    FOR reps = 1 TO DVD_Track_1_Length/2
    IF Coin_Mech = 1 THEN Game_Play 'assumes active high
    PAUSE 2 'pause for 2 ms
    NEXT
    HIGH DVD_Track_1 'release DVD_Track_1
    GOTO Game_Logic


    '========== Game_Play ============
    Game_Play:
    GOSUB System_Status_Sub
    credit=credit-1 'subtract credit for game
    gameInPlay=1 'set gameInPlay variable
    gameCount = gameCount + 1 'increment gameCount
    ' ...
    gameInPlay=0 'set gameInPlay variable
    GOTO Game_Logic


    '====== System_Status_Sub ========
    System_Status_Sub:
    DEBUG CLS, "SYSTEM READY:", CR
    DEBUG ? gameInPlay
    DEBUG ? credit
    DEBUG ? gameCount
    RETURN
Sign In or Register to comment.