PDA

View Full Version : Magic BS2 Board



Jessica Uelmen
01-21-2011, 08:12 PM
····
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 (http://forums.parallax.com/attachment.php?attachmentid=77471&d=1295636323) (.zip)


http://www.youtube.com/watch?v=hjDEkCFhZDs

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:


Complete Chapter 1: The Parallax Serial LCD in Smart Sensors and Applications (http://www.parallax.com/Portals/0/Downloads/docs/prod/sic/3rdPrintSmartSensors-v1.0.pdf)
Review the GOSUB and RANDOM commands in the BASIC Stamp Syntax and Reference Manual (http://www.parallax.com/dl/docs/prod/stamps/web-BSM-v2.2.pdf)
Review the 4-Directional Tilt Sensor’s product documentation (http://www.parallax.com/portals/0/downloads/docs/prod/sens/28036-4DirectionalTiltSensor-v1.0.pdf)

Parts Required

(1) HomeWork Board (http://www.parallax.com/tabid/134/ProductID/133/Default.aspx) with BASIC Stamp 2 (http://www.parallax.com/tabid/134/ProductID/1/Default.aspx)
····The BASIC Stamp 2 Board of Education (http://www.parallax.com/tabid/134/ProductID/294/Default.aspx) is also suitable for this activity.
(1) 4-Directional Tilt Sensor (http://www.parallax.com/tabid/768/ProductID/722/Default.aspx)
(1) Parallax Serial LCD (http://www.parallax.com/tabid/768/ProductID/52/Default.aspx)
(1) Servo/LCD Extension Cable (http://www.parallax.com/tabid/768/ProductID/385/Default.aspx)
(1) 3-pin header (http://www.parallax.com/tabid/151/ProductID/182/Default.aspx)

Optional Mounting Bracket Parts

(2) L-Shaped Mounting Brackets (http://www.parallax.com/tabid/768/ProductID/559/Default.aspx)
(6) 4-40 zinc plated nuts (http://www.parallax.com/tabid/768/ProductID/259/Default.aspx)
(4) 1/4" 4-40 pan-head screws (http://www.parallax.com/tabid/156/ProductID/273/Default.aspx)
(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
http://forums.parallax.com/attachment.php?attachmentid=77473&d=1295636340


Figure 2 – Optional Mounting Bracket Assembly
http://forums.parallax.com/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 (http://forums.parallax.com/showthread.php?122764)!

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!


http://forums.parallax.com/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 (http://forums.parallax.com/showthread.php?112438-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

AttorneyJon
01-23-2011, 02:15 AM
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!

servello
01-25-2011, 05:25 AM
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.

Whit
01-28-2011, 03:46 PM
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_H
01-28-2011, 05:05 PM
The project is a riot. But I'm more interested in the "Cloak of Visibility" T shirt! Where do we get those?

Jessica Uelmen
01-28-2011, 07:02 PM
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 (http://shirt.woot.com/friends.aspx?k=16992)!

And thanks everyone else for your kind words! It makes me so happy to know that people are enjoying this project. :D

ctwardell
01-31-2011, 01:47 PM
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.