Shop OBEX P1 Docs P2 Docs Learn Events
New to Programming, and need help — Parallax Forums

New to Programming, and need help

J^3J^3 Posts: 121
edited 2007-10-21 23:19 in BASIC Stamp
Hello everyone, I am currently working through the "What's a Microcontroller" book and have a question.· At the end of chp 4 the project requests you to add a kill switch to the program.· An example is given, but if I am not mistaken the kill switch will not interupt the DEBUGIN commands, and stop the machine.· This brings me to my question.· Is there an instruction that will interupt a program based on conditions being true or false.·
Thank you , any help would be appreciated.
·

Comments

  • D FaustD Faust Posts: 608
    edited 2007-10-21 15:09
    No there are no interupts.· The·stamp has to be looking for the event, in the current command.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --DFaust
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-10-21 15:36
    Hi JJ, when you write a program for your Stamp the code is executed sequentially one line at a time from top to bottom. Generally when you reach the bottom you will have a command that sends it back to the top so that the program is continually looping.

    During the journey from top to bottom you can place commands that will skip lines·or redirect program execution to elsewhere based on decisions you specify in your code.

    The ability to make decisions is the power of programming, look in the help file for IF....THEN , BRANCH , SELECT..CASE , GOSUB , DO..WHILE.

    Jeff T.
  • D FaustD Faust Posts: 608
    edited 2007-10-21 16:12
    Unsoundcode,
    JJ said...
    the kill switch will not interupt the DEBUGIN commands
    Yes, a command can be made to interupt a loop, but that wasn't the problem.· It was the ability to interupt a command.· JJ, correct me if I am wrong.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --DFaust
  • metron9metron9 Posts: 1,100
    edited 2007-10-21 17:45
    You can use SERIN instead of DEBUGIN amd specify a timeout (see documentation) , The timeout gives you an interrupt within the serin command. Up to 65 seconds on a bS2. This allows background processing while checking for keyboard input.

    I am not sure if while in the command if the keyboard input would finish for example if the timeout was 1000mS and the input started at 999 mS if the byte received would be a valid character. You can arrange your interface for the user to hit a button or press a special key like enter to begin input aswell, that way you can set a timeout of a few mS and background process while waiting for keyboard input. When you receive the special character you then use a 65 second timeout or something. Not sure if there would be a timeout extension as characters are typed either. If you set it for 5 seconds and started receiving at least one character per 5 seconds if it would stay in the command and continue receiving data. You will have to experiment with those things.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think Inside the box first and if that doesn't work..
    Re-arrange what's inside the box then...
    Think outside the BOX!
  • Larry SutherlandLarry Sutherland Posts: 77
    edited 2007-10-21 17:55
    Here is a perspective to consider. The kill switch cold be considered as a safety switch (exp. emergency stop, over-travel, pressure switch, hgh limit, proximity switch fault... etc.). Do you really want the DEBUG to disconnect? In the industrial arena you want to see resets.
  • J^3J^3 Posts: 121
    edited 2007-10-21 20:15
    I do not think I made the problem clear.· I understand that the BS2 will read my tokenized code from top to bottom and execute the instructions given in a looping fashion, if a DO LOOP etc. is used.··What I don't know is, once the BS2 sees the DEBUGIN comand and is waiting for datta to be sent via the DEBUG window, is there a way I can instruct the BS2 to ignore that instruction do to another instruction.· The E-stop, limit switch, etc.,·example is exactly what I am trying to do, but once the BS2 sees the DEBUGIN command it will not execute any further commands until I enter some data.· I hope this is less vauge.
  • smkbaynsmkbayn Posts: 25
    edited 2007-10-21 20:22
    is this maybe what you are trying to do

    Main:

    IF (IN1 = 1) Then "place name of Sub routine here":

    "Sub routine":

    code

    GOTO Main

    with this that portion of the code you are writing will only execute if that input is made.· help?
  • J^3J^3 Posts: 121
    edited 2007-10-21 20:25
    Thank you all for your suggestions.· I will try the SERIN command and the SUBROUTINE, either way I will let you all know if it works are not.· Thank you.
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2007-10-21 20:44
    JJ,

    You might be interested in the following link...

    Interrupt programming with BS2
    http://forums.parallax.com/showthread.php?p=467380

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • J^3J^3 Posts: 121
    edited 2007-10-21 21:48
    ' What's a Microcontroller - ServoControlWithDebug.bs2
    ' Send messages to the BASIC Stamp to control a servo using
    ' the Debug terminal. Also added start and stop buttons.

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    Estop VAR Bit
    counter VAR Word
    pulses CON 100
    duration VAR Word

    DO
    IF IN3 = 1 AND IN4 = 1 THEN 'IN3 is normally high and IN4 is normally low
    Estop = 1
    ELSEIF IN3 = 0 AND IN4 = 0 THEN 'IN3 (stop button) has been pushed
    Estop = 0
    ENDIF

    IF Estop = 1 THEN 'Start/Stop logic is true
    DO
    DEBUG CLS
    DEBUG "Enter PULSOUT duration:", CR
    DEBUGIN DEC duration
    IF duration < 500 THEN
    DEBUG "Value of duration must be above 499", CR
    PAUSE 1000
    ENDIF
    IF duration > 1000 THEN
    DEBUG "Value of duration must be less than 1001", CR
    PAUSE 1000
    ENDIF
    LOOP UNTIL duration > 499 AND duration < 1001

    DEBUG "Servo is running...", CR

    FOR counter = 1 TO pulses
    PULSOUT 14, duration
    PAUSE 20
    NEXT

    FOR counter = 1 TO pulses
    PULSOUT 14, 750
    PAUSE 20
    NEXT

    DEBUG "Done", CR

    ELSEIF Estop = 0 THEN 'Start/Stop logic is false
    DEBUG "Press start switch to start machinery"
    DEBUG HOME
    ENDIF

    LOOP

    'Problem is, once IF Estop = 1 is true, I haven't been able
    ' to get the BS2 to back out of the code block when Estop = 0.
  • metron9metron9 Posts: 1,100
    edited 2007-10-21 23:19
    Give this code a whirl and see what you think.

    Also when posting code use the # button to insert code tags or type:

    opensquarebracket code closedsquarebracket
    .your code here
    opensquarebracket /code closesquarebracket


    You may not be able to use the debug output to show the viewbar or reduce the pause 20 to allow for the output.

    If user inputs a duration and the stop is pushed it will go back to main as well as if during servo output the stop is pressed.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    Estop     VAR Bit
    counter   VAR Word
    pulses    CON 100
    duration  VAR Word
    viewbar   VAR Byte
    
    
    
    main:
     GOSUB TestEstop
     IF Estop = 1 THEN
      GOSUB GetDuration   'Start/Stop logic is true
      IF duration>499 AND duration<1001 THEN
       GOSUB RunServo
      ENDIF
     ELSE
      DEBUG "Press start switch to start machinery"
      PAUSE 500
      DEBUG CLS
     ENDIF
    GOTO main
    
    
    
    TestEstop:
     IF IN3 = 1 AND IN4 = 1 THEN 'IN3 is normally high and IN4 is normally low
      Estop = 1
     ELSEIF IN3 = 0 AND IN4 = 0 THEN 'IN3 (stop button) has been pushed
      Estop = 0
      DEBUG "<<<****Stop Button Engaged***>>>"
     ENDIF
    RETURN
    
    
    GetDuration:
     DO
      DEBUG CLS
      DEBUG "Enter PULSOUT duration (499-1001):"
      DEBUGIN DEC duration
      DEBUG CR
      IF duration < 500 THEN
       DEBUG "Value of duration must be above 499", CR
       PAUSE 1000
      ENDIF
    
      IF duration > 1000 THEN
       DEBUG "Value of duration must be less than 1001", CR
       PAUSE 1000
      ENDIF
    
     LOOP UNTIL duration > 499 AND duration < 1001
    RETURN
    
    RunServo:
    GOSUB TestEstop
    IF estop=1  THEN
     viewbar=0
     DEBUG "Servo is running", CR
    
      FOR counter = 1 TO pulses
       GOSUB TestEstop
       IF Estop=1 THEN
        PULSOUT 14, duration
        PAUSE 20
        viewbar=viewbar+1
        IF viewbar=10 THEN
         DEBUG "."
         viewbar=0
        ENDIF
       ELSE
        GOTO Exitrun
       ENDIF
      NEXT
    
      FOR counter = 1 TO pulses
       GOSUB testEstop
       IF Estop=1  THEN
        PULSOUT 14, 750
        PAUSE 20
         viewbar=viewbar+1
        IF viewbar=10 THEN
         DEBUG "."
         viewbar=0
        ENDIF
       ELSE
        GOTO Exitrun
       ENDIF
      NEXT
      DEBUG "Done", CR
      PAUSE 1000
    ENDIF
    
    Exitrun:
    RETURN
    
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think Inside the box first and if that doesn't work..
    Re-arrange what's inside the box then...
    Think outside the BOX!

    Post Edited (metron9) : 10/21/2007 11:29:21 PM GMT
Sign In or Register to comment.