Shop OBEX P1 Docs P2 Docs Learn Events
wait loop — Parallax Forums

wait loop

damiondamion Posts: 39
edited 2007-05-15 03:48 in BASIC Stamp
Hello out there,

So I'm trying to get a bs2p to wait in a loop until given the signal from another bs2p. Do I need to do this using shiftin/shiftout or...well I'm kind of at a loss. I've tried using serin/serout it didn't seem to work. Any advice?

Thanks

Comments

  • stamptrolstamptrol Posts: 1,731
    edited 2007-05-13 12:23
    Why not just have the first stamp watch a single input while in the loop? The second stamp turns on an output when its ready to play.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tom Sisk

    http://www.siskconsult.com
    ·
  • metron9metron9 Posts: 1,100
    edited 2007-05-13 15:00
    To further elaborate on what stamptrol has suggested. A typical connection would be to have both stamps set a pin to input and connect both of the stamps pins together. Then connect a 4.7K resistor to the same line to ground. That is a pulldown circuit. Both stamps can then monitor that line for a high state. When stamp1 wants to talk to stamp2 it sends a high signal by turning its pin from input to output high. Stamp2 can then do something like jump to a serial input routine. The communication might look like this:

    Stamp1 sends a high pulse or holds the line high for say 10mS giving stamp2 time to read the input if it is only checking the input every 5ms or so. This would allow stamp2 to be processing other data and only check for input at specific times. (Another microprocessor might use interrupts to automatically read the input ans switch to an input routine, still other microprocessors with dedicated hardware serial communications could just process information in the background storing information in a buffer and checking periodically if there was any data to process.)

    stamp2 reads the high signal, jumps to the input routine and waits for the signal to go low
    stamp1 releases the line by setting its pin back to input and waits for a response from stamp2
    stamp2 sends a high pulse and stamp1 initiates serial input while stamp2 initiates serial output

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think Inside the box first and if that doesn't work..
    Re-arrange what's inside the box then...
    Think outside the BOX!
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-05-13 15:11
    Well, you wouldn’t really need a pull-down resistor if the other Stamp Module was always asserting the line HIGH/LOW. Based on that you could do something like:

    DO : LOOP UNTIL IN0 = 1

    This waits until P0 goes HIGH. Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • skylightskylight Posts: 1,915
    edited 2007-05-13 19:01
    The·PBasic reference guide ·states that·the special formatter WAIT·can be used to halt a loop until the input satisfies a criteria:

    The SERIN command can also be configured to wait for specified data before it retrieves any additional input. For example, suppose a device that is attached to the BASIC Stamp is known to send many different sequences of data, but the only data you desire happens to appear right after the unique characters, "XYZ". The BS1 has optional Qualifier arguments for this purpose. On the BS2, BS2e, BS2sx, BS2p, and BS2pe a special formatter called WAIT can be used for this.

    sData VAR ByteMain: SERIN 1, 16780, [noparse][[/noparse]WAIT("XYZ"), DEC sData] END


    The above code waits for the characters "X", "Y" and "Z" to be received, in that order, and then it looks for a decimal number to follow. If the device in this example were to send the characters "XYZ100" followed by a carriage return or some other non-decimal numeric character, the sData variable would end up with the number 100 after the SERIN line finishes. If the device sent some data other than "XYZ" followed by a number, the BASIC Stamp would continue to wait at the SERIN command.
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-05-13 19:02
    Another option with the BS2p is POLLing.

    The method you tried, using SEROUT/SERIN with or without a flow control pin should also work. What did you try with that and what else are the two programs doing in terms of program flow?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • damiondamion Posts: 39
    edited 2007-05-13 19:39
    Thanks everybody.

    Tracy,
    I tried using the serout/serin something like this:

    this is the waiting stamp as far as program flow it just waits in this loop
    DO

    'SEROUT txd,bd, [noparse][[/noparse]"Enter password ---> "]
    SERIN 2,9600, [noparse][[/noparse]waitSTR userEntry \3] ' Get user input password.

    FOR index = 0 TO 3 ' Check array against DATA
    READ Password + index, Bytedata1 ' Get next password char
    IF Bytedata1 <> userEntry(index) THEN EXIT ' Compare to user input,

    NEXT ' exit if not equal.

    IF index <> 3 THEN ' If exit, then index not equal to 3
    ' and pass is not correct.
    SEROUT 16,9600, [noparse][[/noparse]CLS,"Password not correct.", CR] ' Clear user input from screen
    ENDIF
    LOOP UNTIL index = 3
    SEROUT 16,9600, [noparse][[/noparse]"Password is correct.", CR]


    this is the stamp that gives the command this stamp asks for a user input. Once the input is entered it goes off and runs through other programs.
    What I've done so far is send "WCC" as a serout statement. I think propably I need to send [noparse][[/noparse]STR userEntry \3].


    Check_Password:
    DO
    SEROUT txd,bd, [noparse][[/noparse]"Enter password ---> "] ' User instructions.
    SERIN 16,bd, [noparse][[/noparse]STR userEntry \3] ' Get user input password.

    FOR index = 0 TO 3 ' Check array against DATA
    READ Password + index, Bytedata1 ' Get next password char
    IF Bytedata1 <> userEntry(index) THEN EXIT ' Compare to user input,
    NEXT ' exit if not equal.

    IF index <> 3 THEN ' If exit, then index not equal to 3
    ' and pass is not correct.
    SEROUT txd,bd, [noparse][[/noparse]CLS,"Password not correct.", CR] ' Clear user input from screen
    ENDIF
    LOOP UNTIL index = 3 ' Only get out of loop when index = 3.

    ' Clear user input from screen
    SEROUT txd,bd, [noparse][[/noparse]"Password is correct.", CR] ' Program can move on when
    SEROUT 12,bd, [noparse][[/noparse]"WCC"]

    PAUSE 500
    ' GOSUB MissionStart ' password is correct.
    RETURN
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-05-14 01:38
    Hi Damion,

    I think you are using the WAITSTR incorrectly and making unecessary work for yourself. The WAITSTR itself checks the incoming data against an array that already exists, so there is no need for in your program for a loop to "check array against DATA". To start with, try a simple fixed password on the receiving side like this:

    DO
      SERIN 2,9600, [noparse][[/noparse]wait "WCC"] ' Get user input password.
      DEBUG CR,"PASSWORD RECEIVED"
    LOOP
    



    That might be sufficient, to have a hard coded password. The program does not get past the SERIN until it receives the WCC. But if your program does nees a variable password, then here is the syntax:

    userentry byte(3)
    userentry(0) = "W"
    userentry(1) = "C"
    userentry(2) = "C"
    DO
      SERIN 2,9600, [noparse][[/noparse]WAITSTR userEntry\3] ' Get user input password.
      DEBUG CR,"PASSWORD RECEIVED"
    LOOP
    



    Again, it won't get past the serin until it receives the "WCC". But the password is no longer hard coded and can be changed by changing the contents of the userentry array.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • damiondamion Posts: 39
    edited 2007-05-15 03:48
    thanks all I got it under control
Sign In or Register to comment.