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

Coding Some Magic

Piecing Together the Final Program

As we prepare the final code for the Magic BS2 Board, there are several important factors we will need to keep in mind: how we will create “random” answers, how to determine when the board is being shaken, and what kind of responses we want the board to display.

 

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 (shown on previous page), 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”]

Printer-friendly version
Wire Up and Test the Board
Prev
Ask Some Questions and Get Some Answers
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

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

© 2026 Parallax Learn • Built with GeneratePress