Shop OBEX P1 Docs P2 Docs Learn Events
Magic BS2 Board — Parallax Forums

Magic BS2 Board

Jessica UelmenJessica Uelmen Posts: 490
edited 2011-01-31 05:47 in Learn with BlocklyProp
····
Magic BS2 Board

Ever have a question that you just didn’t know the answer to? Let the Magic BS2 Board help! Simply ask any question, give it a shake, and watch the answer appear magically before your eyes!

Disclaimer: Parallax Inc. takes no responsibility for unwanted, untrue or potentially slanderous answers. This project is intended strictly for entertainment purposes.

Download Source Code (.zip)

Getting Started

If you are new to the BASIC Stamp microcontroller or to programming, it would be a good idea to review the following before beginning:
  1. Complete Chapter 1: The Parallax Serial LCD in Smart Sensors and Applications
  2. Review the GOSUB and RANDOM commands in the BASIC Stamp Syntax and Reference Manual
  3. Review the 4-Directional Tilt Sensor’s product documentation
Parts Required

(1) HomeWork Board with BASIC Stamp 2
····The BASIC Stamp 2 Board of Education is also suitable for this activity.
(1) 4-Directional Tilt Sensor
(1) Parallax Serial LCD
(1) Servo/LCD Extension Cable
(1) 3-pin header

Optional Mounting Bracket Parts

(2) L-Shaped Mounting Brackets
(6) 4-40 zinc plated nuts
(4) 1/4" 4-40 pan-head screws
(2) 1/2" 4-40 pan-head screws
(2) 1/4" round nylon spacers #4

Schematics and Building the Circuits

Figure 1 below shows the schematic for the Magic BS2 board, and Figure 2 displays the optional mounting bracket assembly for the LCD display.

Figure 1 – Magic BS2 Board Schematic
attachment.php?attachmentid=77473&d=1295636340


Figure 2 – Optional Mounting Bracket Assembly
attachment.php?attachmentid=77474&d=1295636340

Not sure how to wire circuits from schematics? No problem! Parallax conveniently has another tutorial on how to wire circuits from schematics available on the Stamps in Class Mini Projects page. Check it out here!

Testing the Circuit

Once everything is wired, it’s a good idea to test that everything is working properly before developing code. The program below displays a message on the Serial LCD and displays the states of the 4-Directional Tilt Sensor using the Debug Terminal.
' MagicBS2Board_Test.bs2
' Tests the functionality of the board

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

LCDpin    PIN    13                    ' Declare LCD pin

DEBUG CLS                              ' Clear the Debug Terminal

SEROUT LCDpin, 84, [22, 12]            ' Initialize the LCD
PAUSE 5

SEROUT LCDpin, 84, ["Testing..."]      ' Display test message

DO
  DEBUG BIN1 IN0, "  ", BIN1 IN1, CR   ' Display tilt states
  PAUSE 300                            ' at human speed
LOOP

Another thing to check when running this code is to monitor how the 4-Directional Tilt Sensor responds when being shaken. We will need to determine what pattern emerges (if any) so that we can track when the board is moving and when it’s not in code.

Figure 3 is a snippet of the tilt sensor’s states when being shaken. Notice that the state on at least one pin changes almost every time a reading is taken. But you should also notice that sometimes, the state remains the same. This will be key information as we put together our final program code!
attachment.php?attachmentid=77477&d=1295638631

Figure 3 – Debug Terminal Output of Tilt Sensor’s States

Piecing Together the Final Program

As we prepare the final code for the Magic BS2 Board, there are a couple of important factors we will need to keep in mind.

Creating “Random” Answers

We certainly want to be sure that the people asking questions don’t get the same response repeated time and time again. So we’ll need to devise a way to randomize the results.

If you recall from A Bi-Color LED Memory Game, we used the RANDOM command to generate a random pattern of red & green LED flashes. We can use the same theory from that application for our Magic BS2 board.

First, we’ll need to declare a variable that will be randomized. In this case, we’ll make it a word-sized variable in order to maximize the amount of random numbers generated.
generateMessage VAR Word

Placing the following code at the beginning of our program will keep randomizing the generateMessage variable until the 4-Directional Tilt sensor detects movement. This code will execute very quickly, so it will be very difficult for the user to get the same number twice.
DO
··old0 = IN0: old1 = IN1
··RANDOM generateMessage
LOOP UNTIL (old0 <> IN0 OR old1 <> IN1)

Determining when the Board is Shaking

By looking at the pattern of the tilt sensor’s states in Figure 3, we can apply a general rule that as the states of pin 0 or 1 are fluctuating, the board is being shaken. Once the states remain the same for several cycles, we can assume that the board is still and the user is ready for their answer.

For this, we will need to compare the previous state of both pins with their current state, and when the states begin to repeat, we know the user has stopped shaking the board.

But what if the states repeat a couple times even while the board is being shaken, as seen in Figure 3? To make sure we don’t display an answer while the user is shaking the board, we’ll need to keep track of how many times the sensor’s state has repeated. We’ll give it a generous margin and say that if the sensor’s state repeats 10 times, the user has probably finished shaking the board.

Establishing Responses

The final thing that needs to be determined is what responses the user will get when done shaking the board. In order to determine what response will be displayed, we’ll look at a nibble (or the last four bits) of the random number generated. To do this, we’ll declare another variable:
displayMessage VAR generateMessage.NIB0

This will give us a possibility of 16 different responses – so we’ll need to think of some! Regular Magic 8-Balls have 20 possible responses with 10 being positive, 5 negative and 5 unclear. To try and keep the proportions somewhat equal, our Magic BS2 Board will have 8 positive responses, 4 negative and 4 undecided. These responses can be chosen by using SELECT…CASE statements, as depicted in the pseudo-code below.
SELECT displayMessage
··CASE 0
····SEROUT LCDpin, 84, [128, "Message 1"]
··CASE 1
····SEROUT LCDpin, 84, [128, "Message 2"]
··...
··CASE ELSE
····SEROUT LCDpin, 84, [128, “Message 16”]

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

Run the Final Program!

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

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

' -----[ 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 

It's 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!

__________________________________________________

(c) 2011 by Parallax Inc. - all rights reserved
650 x 167 - 27K
582 x 191 - 23K
450 x 405 - 53K

Comments

  • AttorneyJonAttorneyJon Posts: 10
    edited 2011-01-22 18:15
    Pretty cool, Jess!

    What I especially love is that I just received the Smart Sensors kit yesterday. Great timing for another cool project.

    By the way, love the answers, "In-variable-y". lol. Very clever.

    I think that I might add some legal latin phrases for my students. Something like my usual response to their questions, "IPSE DIXIT!"

    Anyway, great job Jess. Thanks for another top-notch project designed to make me look like I know what I'm doing!

    And now for my first question, what am I going to do in zero degree weather? ...[shaking]...Oh yeah, play with my cool BASIC stamp magic board. And why? Because Parallax rulz! Vas-y!
  • servelloservello Posts: 113
    edited 2011-01-24 21:25
    It's been a while. Welcome back, Jessica. Once again, an excellent project. BSII, Code I can work with to learn, parts I already have, short completion time.......and an actually, fun real world object!

    Thank you again,
    Dominic

    Yes, I did purchase the "Mini Projects" book.
  • WhitWhit Posts: 4,191
    edited 2011-01-28 07:46
    Jess,

    This is really great work and a great video to boot! Thanks for keeping things lively!

    Let me know when you get the flux capacitor working...
  • Martin_HMartin_H Posts: 4,051
    edited 2011-01-28 09:05
    The project is a riot. But I'm more interested in the "Cloak of Visibility" T shirt! Where do we get those?
  • Jessica UelmenJessica Uelmen Posts: 490
    edited 2011-01-28 11:02
    Oh noes! I'm going to give away my coveted "shirt secret"?

    ... OK maybe it's not that big of a deal.

    Anyway, all the shirts I wear for the videos are from shirt.woot.com. It's a pretty rad-tastic site with one shirt for sale a day for $10 until they sell out (+ free shipping!). But, if you still want to get it after that date, you can check out The Reckoning, where they sell their top 27 sellers for $15 (and still free shipping!).

    In fact, you can still purchase the Cloak of Visibility, check it out!

    And thanks everyone else for your kind words! It makes me so happy to know that people are enjoying this project. :D
  • ctwardellctwardell Posts: 1,716
    edited 2011-01-31 05:47
    Forget the shirt, I see the Tardis on your monitor, is Parallax getting into time travel, I want one.

    I always knew there had to be a secret behind all the cool stuff Parallax comes up with. ;-)

    C.W.
Sign In or Register to comment.