Tutorial - BASIC Stamp Debugging

Qwaszx72Qwaszx72 Posts: 30
edited February 2012 in BASIC Stamp Vote Up0Vote Down
Here is a BASIC Stamp Debugging tutorial(As suggested by vaclav_sal):

Debugging is a simple way to locate and fix errors within your program. It is a very easy thing to do but if not done correctly or not done at all, it is very hard to find an error within your code

Through the use of the debug commands, you are able to detect accidental infinite loops; assure that the math or computations the BASIC Stamp is doing are being done correctly; find and then fix bugs or errors in your code; and altogether become a better programmer!


  • 9 Comments sorted by Date Added Votes
  • Qwaszx72Qwaszx72 Posts: 30
    edited April 2011 Vote Up0Vote Down
    The Debugging Commands:

    Debug - This command is used to send the value of variables or messages from the BASIC Stamp module to your computer for easy debugging.

    Debugin - This is commonly used to send commands via your computer to your BASIC Stamp in order to test different parts of your code.
  • Qwaszx72Qwaszx72 Posts: 30
    edited April 2011 Vote Up0Vote Down

    The debug command works on ALL BASIC Stamp modules.

    The DEBUG command can send variables or values to your computer to be displayed in the Debug Window.

    Here is a simple program that will send the value of the variable Debug_Data:
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    Debug_Data VAR Byte
    Debug_Data = 123
    DEBUG Debug_Data


    If you run this program, you will notice that the ASCII character #123("{") will show up in your debug screen. This is usually of no help when you want to know a numerical value. For example, if you would like to count how many times someone clicked a button, you would want to display a DECIMAL value of the variable.

    To do this you would simply add a formatter before the variable:
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    Debug_Data VAR Byte
    Debug_Data = 123
    DEBUG DEC Debug_Data

    In the above example you may notice in the line DEBUG DEC Debug_Data the "DEC" before Debug_Data. This tells your computer to display the decimal value of the variable.

    All of the different formatters are listed below:

    ? - Displays "[Variable_Name] = [Variable_Value]"; by default the variable is displayed as a decimal value, but you can add a formatter before the "?" to display the variable in a different format(Example: BIN ? Debug_Data will display "Debug_Data = 1111011").***

    ASC ? - Displays "[Variable_Name] = [Variable_Value]"; the variable will be displayed in the ASCII format.***

    DEC - Displays decimal value, optionally fixed to 1-5 digits.*

    HEX - Displays hexadecimal value, optionally fixed to 1-4 digits.**

    BIN - Displays binary value of the variable, optionally fixed to 1-16 digits.**

    STR - Displays ASCII string from the bytearray(An array of multiple bytes) until byte = 0(This will be when there are no more values stored in the array). Optionally add "\ [Amount of characters to display]" after the variable to only display a certain amount of bytes from the array.

    REP - Displays the ASCII a certain amount of times. Must be in this format: "REP [Variable or ASCII character] \ [Amount of times to repeat].

    * A "S" may be put before the formatter for a signed value.
    ** "S" for signed value and/or "I" for indicated value(The "I" must come before the "S" when displaying a Signed, Indicated value)
    *** An automatic line feed will be added

    Here is a program that demonstrates the uses of formatters:
    ' {$STAMP BS2px}
    ' {$PBASIC 2.5}
    String_Data VAR Byte(10)
    Debug_Data VAR Word
    Debug_Data = 123
    String_Data(0) = "H"
    String_Data(1) = "e"
    String_Data(2) = "l"
    String_Data(3) = "l"
    String_Data(4) = "o"
    String_Data(5) = "!"
    DEBUG Debug_Data, CR          ' {
    DEBUG ? Debug_Data            ' Debug_Data = 123
    DEBUG BIN ? Debug_Data        ' Debug_Data = 1111011
    DEBUG ASC ? Debug_Data        ' Debug_Data = {
    DEBUG DEC Debug_Data, CR      ' 123
    DEBUG SDEC Debug_Data, CR     ' 123
    DEBUG HEX Debug_Data, CR      ' 7B
    DEBUG SHEX Debug_Data, CR     ' 7B
    DEBUG ISHEX Debug_Data, CR    ' $7B
    DEBUG BIN Debug_Data, CR      ' 1111011
    DEBUG SBIN Debug_Data, CR     ' 1111011
    DEBUG ISBIN Debug_Data, CR    ' %1111011
    DEBUG STR String_Data, CR     ' Hello!
    DEBUG STR String_Data \ 5, CR ' Hello
    DEBUG REP Debug_Data \ 3, CR  ' {{{


    Sometimes, you will want to send Commands to your debug window in order to do certain things.

    These commands are:

    Clear Screen(CLS) - Clear the screen and place cursor at home position.

    Home(HOME) - Place cursor at home in the upper-left corner or the screen.

    Move To [x,y](CRSRXY) - Moves the cursor to a specified location.

    Cursor Left(CRSRLF) - Move cursor left one character.

    Cursor Right(CRSRRT) - Move cursor right one character.

    Cursor Up(CRSRUP) - Move cursor up one character.

    Cursor Down(CRSRDN) - Move cursor down one character.

    Bell(BELL) - Beep the PC speaker.

    Backspace(BKSP) - Back up cursor to left one space.

    Tab(TAB) - Tab to the next column.

    Line Feed(LF) - Move cursor down one line.

    Clear to End of Line(CLREOL) - Clear line contents right of the cursor.

    Clear Down(CLRDN) - Clear screen contents below the cursor.

    Carriage Return(CR) - Move cursor to the beginning of the next line. This also moves everything to the right of the cursor to the next line.

    Move To Column X(CRSRX) - Move cursor to specified column.

    Move To Line Y(CRSRY) - Move cursor to specified line.

    Here is a program that demonstrates the use of all the commands:
    ' {$STAMP BS2px}
    ' {$PBASIC 2.5}
    Counter VAR Byte
    GOTO Main_Program
      PAUSE 3000
      DEBUG "This text will be cleared in 3 seconds. . ."
      PAUSE 3000
      DEBUG "The cursor will be sent to home and then this text will be overwritten."
      PAUSE 3000
      DEBUG HOME, "As you can see, the cursor was sent to the home position."
      DEBUG CRSRXY, 8, 3, "The cursor was moved to the 8th row and the 3rd column!"
      DEBUG "The cursor was moved right twice and then down twice."
      DEBUG "The speaker will beep in 3 seconds. . ."
      PAUSE 3000
      PAUSE 3000
      FOR Counter = 0 TO 25
        DEBUG BKSP
        PAUSE 100
      DEBUG TAB, "This text is tabbed", CR
      DEBUG "This text is not tabbed"
      DEBUG LF, LF, "This text is two lines down."
      DEBUG "All the text starting here -> will be cleared in 3 seconds. . ."
      PAUSE 3000
      DEBUG "All this text", CR, "Starting here", CR, "Will be cleared", CR, "In 3 seconds. . ."
      PAUSE 3000
      DEBUG CRSRY, 1
      DEBUG "This text is on top of that text"
      DEBUG CR
      DEBUG "This text is under that text"
      DEBUG CRSRX, 10
      DEBUG "This text started at the 10th column."
      DEBUG CRSRY, 5
      DEBUG "This text started at the 5th line."
      GOSUB Clear_Screen
      GOSUB Reset_Debug
      GOSUB Cursor_Home
      GOSUB Reset_Debug
      GOSUB Move_To_X_Y
      GOSUB Reset_Debug
      GOSUB Cursor_Left_Right_Up_Down:
      GOSUB Reset_Debug
      GOSUB Beep_Speaker
      GOSUB Reset_Debug
      GOSUB Backspace
      GOSUB Reset_Debug
      GOSUB Tab_The_Cursor
      GOSUB Reset_Debug
      GOSUB Line_Feed
      GOSUB Reset_Debug
      GOSUB Clear_To_End_Of_Line
      GOSUB Reset_Debug
      GOSUB Clear_Down
      GOSUB Reset_Debug
      GOSUB Carriage_Return
      GOSUB Reset_Debug
      GOSUB Move_To_Column_X
      GOSUB Reset_Debug
      GOSUB Move_To_Line_Y
      GOSUB Reset_Debug
  • Qwaszx72Qwaszx72 Posts: 30
    edited April 2011 Vote Up0Vote Down

    The debugin command only works on the BASIC Stamp 2 and up.

    The DEBUGIN command will read keystrokes from your computer and send them to the BASIC Stamp module.

    Here is a simple program that will get a byte sized value from the debug window and then subtract it from 65535 and send the result to the debug window:
    ' {$STAMP BS2px}
    ' {$PBASIC 2.5}
    Debugin_Data VAR Word
    Debug_Data VAR Word
    DEBUG "Enter a decimal value between 0 and 65535. Then press 'RETURN' ->"
    DEBUGIN DEC Debugin_Data
    Debug_Data = 65535 - Debugin_Data
    DEBUG CR, DEC Debug_Data

    If you look, the above program uses the same formatters for DEBUGIN as used in the DEBUG command. There are however two additional formatters: NUM and SNUM.

    NUM - A generic numeric input. The user must indicate hex($), binary(%), or decimal(N/A) and then provide the value(Only $, %, 0 - 9, A - F accepted)

    SNUM - Does the exact same thing as NUM yet allows for a signed value(-32768 to + 32767).

    After any character not used in the formatter is given the value will be sent to the BASIC Stamp. If the BASIC Stamp uses a command like DEC5 then after 5 digits have been submitted, the value will be sent to the BASIC Stamp.

    I will not go into much more detail on the formatters because they are the same as the DEBUG command.
  • Qwaszx72Qwaszx72 Posts: 30
    edited April 2011 Vote Up0Vote Down
    I hope you enjoyed this short tutorial on BASIC Stamp Debugging.

    Please post any comments, questions, or fixes that you wish. I will get back to you as soon as I can.

    Also, if you would like me to create more tutorials, please do not be hesitant to reply asking me to do so. Just be sure to tell me what you would like the tutorial to be on.

    Here are the links to my other tutorials:
    Retrieving EEPROM Data From BASIC Stamp Modules
  • vaclav_salvaclav_sal Posts: 451
    edited April 2011 Vote Up0Vote Down
    Nice work on DEBUG.
    You are filling big void in BASIC Stamp programming.

    I would suggest you always check the official terminology in Parallax documentation.

    For example DEBUG is a command, DEC , ASC etc are formatters, CLS is a control character in DEBUG command.

    I think you should avoid generic terms likes "data" or "send".
    It is good to use simple terminology, but if you say " to set variable" that is exactly what you doing when using command "DEBUGIN variable".

    I also think emphasizing command default options would be nice for a beginner to know.
    Sample "DEBUG ? variable" is same as "DEBUG DEC ? variable"

    I do not know your background but I would suggest you solicit a "technical and English " editor help before your next post.
    Sorry, but I cannot help you with English and really do not have time for technical editing.
    Too many honeydoos!!!

    Keep it up. You are doing valuable service to the forum.

  • Qwaszx72Qwaszx72 Posts: 30
    edited April 2011 Vote Up0Vote Down
    Thanks for all the comments, I threw this together quite quickly so I apologize for incorrect grammar...
  • Just JeffJust Jeff Posts: 36
    edited February 2012 Vote Up0Vote Down
    Just found this and MANY THANKS it is exactly what I needed tonight!
    [ Love Robots / Learning to Code ]

    The most exciting phrase to hear in science, the one that heralds new discoveries, is not 'Eureka!' (I found it!) but 'That's funny ...' Isaac Asimov

    Jeff F. Skinner / Network Engineer, Entrepreneur & Expat who loves to "Tinker"!
  • CatspawCatspaw Posts: 49
    edited February 2012 Vote Up0Vote Down
    I might suggest including "how to" use the debug statements.

    For example: using a debug statement after a variable is fine for finding out what it's value is. What if that value is wrong? Placing another debug cmd right before the suspect code can tell you what the value was before you make a change. Sometimes the output is incorrect because the input was incorrect to begin with.
Sign In or Register to comment.