Skip to content
Parallax Learn

Parallax Learn

  • Welcome
  • Tutorials
        • Tutorial Series head tag

          Tutorial Series
        • Tutorial Series

          The special, classroom-ready series pages are organized collections of tutorials for our most popular hardware and/or languages. The tutorials for each topic are conveniently accessible from a single page, shown in the order it is recommended that they be completed.
        • Robotics Series Head tag

          Robotics Series
        • Robotics Series

          • Artificial Intelligence
          • Cybersecurity: Radio Data tutorialCybersecurity
          • cyber:bot + Python
          • cyber:bot + MakeCode
          • Boe-Bot Tutorial SeriesBoe-Bot
          • Arduino Shield-Bot
          • ActivityBot with C TutorialsActivityBot + C
          • ActivityBot with BlocklyProp Tutorial SeriesActivityBot + BlocklyProp
          • Scribbler 3 Tutorial SeriesScribbler 3
        • Electronics & Programming Series Head tag

          Electronics & Programming Series
          • BS2 Board of Education Tutorial SeriesBS2 Board of Education
          • Propeller C-Language BasicsPropeller C Basics
          • FLiP Try-It Kit C Tutorial SeriesFLiP Try-It Kit + C
          • FLiP Try-It Kit BlocklyProp TutorialsFLiP Try-It Kit + BlocklyProp
          • Badge WX Tutorial SeriesBadge WX
          • Propeller BlocklyProp Basics and ProjectsPropeller BlocklyProp Basics
          • View All Tutorial Series »
        • Browse Tutorials
        • Browse Tutorials

          Individual tutorials sorted by robot or kit, and language.
        • By Robot or Kit
          • ActivityBot
          • SumoBot WX
          • Boe-Bot
          • Shield-Bot
          • cyber:bot
          • Badge WX
          • ELEV-8
          • ARLO
        • By Language
        • By Language

          • Propeller C
          • Arduino
          • BlocklyProp
          • PBASIC
          • Python
          • MakeCode
          • View All Tutorials »
  • Educators
  • Reference
  • Downloads
  • Home
  • All Courses

The Magic BS2 Board Project

Curriculum

  • 1 Section
  • 3 Lessons
  • Lifetime
Expand all sectionsCollapse all sections
  • The Magic BS2 Board Project
    3
    • 1.0
      Wire Up and Test the Board
    • 1.1
      Coding Some Magic
    • 1.2
      Ask Some Questions and Get Some Answers

Ask Some Questions and Get Some Answers

Run the Final Program!

Now for the following Magic BS2 Board code, I used Parallax and electronics themed responses (for the fun of it). Feel free to change the responses to anything you desire!

Running the MagicBS2Board.bs2 code below magically turns your BASIC Stamp 2 into an amazing fortune-telling machine. Impress all your friends and family with your microcontroller’s ability to predict the future!

Keep in mind when using the Magic BS2 Board that if you’d like to ask it a second question, you’ll need to press the reset button on your board.

' -----[ Title ]--------------------------------------------------------------
' MagicBS2Board.bs2
' Ask a question & get a response from the wise Magic BS2 Board!

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

' -----[ I/O Definitions ]----------------------------------------------------
LCDpin            PIN    13                     ' Serial LCD pin

' -----[ Variables ]----------------------------------------------------------
old0              VAR    Bit                    ' Variable space for previous
old1              VAR    Bit                    ' tilt states

counter           VAR    Nib                    ' Counter variable

generateMessage   VAR    Word                   ' Variable space for randomizing
displayMessage    VAR    generateMessage.NIB0   ' and displaying answers

' -----[ Initialization ]-----------------------------------------------------
SEROUT LCDpin, 84, [22, 12]                     ' Initialize the LCD
PAUSE 500                                       ' Wait for battery connection

SEROUT LCDpin, 84, [128, "ASK A QUESTION &",    ' Prompt user response
                    148, "SHAKE FOR ANSWER"]

DO
  old0 = IN0: old1 = IN1                        ' Loop until board is
  RANDOM generateMessage                        ' shaken & randomize answer
LOOP UNTIL (old0 <> IN0 OR old1 <> IN1)

SEROUT LCDpin, 84, [12]                         ' Clear the LCD

' -----[ Main Routine ]-------------------------------------------------------
DO
  old0 = IN0: old1 = IN1                        ' Save tilt states to variables
  PAUSE 100                                     ' Pause for human speeds

  IF old0 <> IN0 OR old1 <> IN1 THEN            ' Compare to current state &
    RANDOM generateMessage                      ' if different, board is shaking
    counter = 0                                 ' Reset counter to 0
  ELSE                                          ' If they repeat, track with
    counter = counter + 1                       ' counter variable
    IF counter > 10 THEN                        ' If it's been the same for 10
      GOSUB Display_Answer                      ' cycles, display the answer
    ENDIF
  ENDIF
LOOP                                            ' Loop until shaking stops

' -----[ Subroutine - Display_Answer ]----------------------------------------
' Displays answers based on value in displayMessage & ends the program

Display_Answer:
  SELECT displayMessage
    CASE 0
      SEROUT LCDpin, 84, [128, " PBASICALLY YES"]
    CASE 1
      SEROUT LCDpin, 84, [128, "  THE BOE-BOTS",
                          148, "    SAY YES"]
    CASE 2
      SEROUT LCDpin, 84, [128, " RESPONSE FRIED",
                          148, "   ASK LATER"]
    CASE 3
      SEROUT LCDpin, 84, [128, "  DEBUGGING...",
                          148, "   TRY AGAIN"]
    CASE 4
      SEROUT LCDpin, 84, [128, " THE SCRIBBLERS",
                          148, "     SAY NO    "]
    CASE 5
      SEROUT LCDpin, 84, [128, " PROPELLERY NOT"]
    CASE 6
      SEROUT LCDpin, 84, [128, " REPLY STATIC-Y",
                          148, "    ASK AGAIN"]
    CASE 7
      SEROUT LCDpin, 84, [128, "  CURRENTLY NO"]
    CASE 8
      SEROUT LCDpin, 84, [128, " AMMETERS POINT",
                          148, "     TO YES"]
    CASE 9
      SEROUT LCDpin, 84, [128, "AS THE STINGRAYS",
                          148, "  SEE IT - YES"]
    CASE 10
      SEROUT LCDpin, 84, [128, " VOLTAGE SPIKE",
                          148, " CAN'T SAY NOW"]
    CASE 11
      SEROUT LCDpin, 84, [128, "  AS I SPIN IT",
                          148, "     ...YES"]
    CASE 12
      SEROUT LCDpin, 84, [128, " YOU WILL MEET",
                          148, " NO RESISTANCE"]
    CASE 13
      SEROUT LCDpin, 84, [128, "DON'T CNT ON IT"]
    CASE 14
      SEROUT LCDpin, 84, [128, "THE POLARITY IS",
                          148, "   POSITIVE"]
    CASE ELSE
      SEROUT LCDpin, 84, [128, " IN-VARIABLE-Y",
                          148, "     YES"]
    ENDSELECT
END                                             ' End the program

 

Your Turn

As mentioned above, if you want to ask the Magic BS2 Board a second question, you have to press the reset button on the board. This is sort of inconvenient when wanting to ask many questions in a row. Is there a way that you can modify the program so that the reset button doesn’t have to be pushed? (Hint: Yes, there is!)

Here are some important things to ponder if attempting to improve the code:

  • How will you determine if the board is shaking because the user is asking another question or if the 4-Directional Tilt Sensor is just responding to small movements as the answer is being read?
  • Could you specify how long to wait before the board resumes taking tilt measurements?
  • Would it be possible to put a piece of code in the subroutine to track movement before returning to the main program?

If you get stuck, go back and run MagicBS2Board_Test.bs2 and monitor how the board responds in different situations. The key to solving these kinds of problems is to pick up on any patterns that emerge which can easily be replicated in code.

Give it a try!


Printer-friendly version
Coding Some Magic
Prev

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2024

© 2026 Parallax Learn • Built with GeneratePress