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”]