Magic BS2 Board
Jessica Uelmen
Posts: 490
····
Magic BS2 Board
Ever have a question that you just didnt 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) 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
Figure 2 Optional Mounting Bracket Assembly
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, its 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.
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 its not in code.
Figure 3 is a snippet of the tilt sensors 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!
Figure 3 Debug Terminal Output of Tilt Sensors 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 dont get the same response repeated time and time again. So well 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, well need to declare a variable that will be randomized. In this case, well make it a word-sized variable in order to maximize the amount of random numbers generated.
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.
Determining when the Board is Shaking
By looking at the pattern of the tilt sensors 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 dont display an answer while the user is shaking the board, well need to keep track of how many times the sensors state has repeated. Well give it a generous margin and say that if the sensors 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, well look at a nibble (or the last four bits) of the random number generated. To do this, well declare another variable:
This will give us a possibility of 16 different responses so well 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.
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 microcontrollers ability to predict the future!
Keep in mind when using the Magic BS2 Board that if youd like to ask it a second question, youll need to press the reset button on your board.
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:
Give it a try!
__________________________________________________
(c) 2011 by Parallax Inc. - all rights reserved
Magic BS2 Board
Ever have a question that you just didnt 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:
- Complete Chapter 1: The Parallax Serial LCD in Smart Sensors and Applications
- Review the GOSUB and RANDOM commands in the BASIC Stamp Syntax and Reference Manual
- Review the 4-Directional Tilt Sensors product documentation
(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
Figure 2 Optional Mounting Bracket Assembly
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, its 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 its not in code.
Figure 3 is a snippet of the tilt sensors 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!
Figure 3 Debug Terminal Output of Tilt Sensors 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 dont get the same response repeated time and time again. So well 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, well need to declare a variable that will be randomized. In this case, well 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)
··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 sensors 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 dont display an answer while the user is shaking the board, well need to keep track of how many times the sensors state has repeated. Well give it a generous margin and say that if the sensors 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, well look at a nibble (or the last four bits) of the random number generated. To do this, well declare another variable:
displayMessage VAR generateMessage.NIB0
This will give us a possibility of 16 different responses so well 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]
··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 microcontrollers ability to predict the future!
Keep in mind when using the Magic BS2 Board that if youd like to ask it a second question, youll 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?
Give it a try!
__________________________________________________
(c) 2011 by Parallax Inc. - all rights reserved
Comments
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!
Thank you again,
Dominic
Yes, I did purchase the "Mini Projects" book.
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...
... 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.
I always knew there had to be a secret behind all the cool stuff Parallax comes up with. ;-)
C.W.