CS108 Lab 4: Binary and Control Block Practice
Create an app to demonstrate which shell will be launched given a selection of wires. Refer to the diagram which will be drawn on the whiteboard for a truth table.
Controls needed:
- Three checkboxes, marked b1, b2, and b3. These represent the three control wires, and a check in the box indicates power is applied to the wire. These don't have any associated events.
- "Launch" button. This will be used to launch the shell indicated by the checkboxes. Draw a red circle for the indicated shell.
- "Reset" button. This will reload all tubes with new shells. Draw new green circles when this is clicked.
Other elements from designer:
- A canvas to draw the shells
- A horizontal alignment to put the check boxes in a row (and buttons in a row
To draw a circle, use Canvas.DrawCircle. Call this 8 times to draw the initial green circles when Reset is clicked. When Launch is clicked, draw one red circle for the correct shell.
The Launch button click event
When Launch is clicked, you will need to do something like this (Counting shells starting at 0):
if not one.checked and not two.checked and not three.checked:
draw red circle over shell 0
if not one.checked and not two.checked and three.checked:
draw red circle over shell 1
if not one.checked and two.checked and not three.checked:
draw red circle over shell 2
// And so on, for the remaining 5 shells
Note: If you decide to actually build one of these, look up a "Demultiplexer" or "Demux". The appropriate logic can be purchased on a single premade chip, saving a significant amount of effort. You may also need a relay module depending on the current requirements of your shell igniters. I have used a SainSmart brand relay board and been pleased with the quality and longevity, but many brands are available. In my application, I used a raspberry pi with GPIO (General-Purpose Input Output) pins to provide the initial signal, to control a garage door opener in a chicken coop.
Improvements for the Future (or now if you like)
Boxes one, two and three can be treated as bits in binary number from 0 through 7. You could extract this number, by an algorithm such as the following:
n = 0
if one.checked:
n = n + 4
if two.checked:
n = n + 2
if three.chcked:
n = n + 1
Having n, it would be possible to draw a circle like this:
Canvas.DrawCircle(x = n*20, y = 30, ... )
Setting the x coordinate based on n will eliminate the large 8-if control block. Setting the shells in two rows of 4 can be accomplished like this:
Canvas.DrawCircle(x = n%4 * 20, y = 30 if n is greater than 3 else 50, ...)
The "30 if n is greater than 3 else 50" can be found as the if/else block that will click into a spot where a number is needed. % is modulus here, the remainder when dividing, same as for lab 4.
Turning in the Lab
Show me that you have finished by Thursday, February 21, at 3:30 PM