Lucky NumberLevel: beginner
We have seen them everywhere, 8 Ball fortune teller, Chinese paper fortune teller, fortunte cookie, wheel of fortunate, even the casino roulette, they all function the same way: randomly give you a number or a phrase. Here we will show you how to make your own fortunate teller to display a random number using a seven segment display.
A seven segment display is an electronic device to display decimal numerals or certain alphabet character. As its name indicates, it has seven small segments. By turning them individually on or off, they can be combined to display numerals or letters. These devices are widely used in digital clock, radio, etc. Here, we will use only one unit to display only one digital number.
How does it work?
Each segment of a 7-segment display is commonly referred by the letters A to G. There is also an eight-segment which represents the decimal point (dp). We will not be using this segment in our example. In order to display each segment correctly, we need to connect each segment to a pin on the Education Board. Like the LED, we also need to add one resistor and one jumper wire per segment as shown on figure below.
As all the fortune teller devices, it needs the user's input to get started. In our case, we use a tact switch as the trigger to get our lucky number. Here is the circuit to represent the tact switch and 7 segment display.
7-segment display encoding
The most popular bit encodings are gfedcba and abcdefg - 0 is off and 1 is on depending on the common anode or common cathode configuration. You can see the detail description here. In this project, we use a 7 segment display that comes with common cathode configuration.
'&B 1 1 1 0 1 1 0 1 Common cathode connection ' dp g f e d c b a segment ' 7 6 5 4 3 2 1 0 Ports assigment (P0 ~P7)
In order to display the number, we need to configure the corresponding segments. We use an array and store the setting of each segment using binary data type indicated by "&B". For example: to display 5, we need to turn on segment a, c, d, f and g. The array for number 5 will look like this:
NumArray(5) = &B11101101
After we have all the numbers set up, we generate a random number like the roulette. InnoBasic has a built-in Random command for this task. To generate a true random number, it takes an initial number known as Seed to be the base value from which it will generate the random number.
RANDOM(Seed) 'generate the base value
Now we wait for user's input. The In function returns 0 if the user has pressed the switch to generate the random number.
We store the random number KeyInNum. Because Random can generate a number greater than 9 and we only have a single digit seven segment display, we need to make sure it is a single digit with the MOD command.
We also need to set each port or segment to be the output port before we can display the number.
For i=0 To 7 Output i High i Next
In our code, we want to display the generated number num times before it stops. You can change it to any number you want. When it stops, the number that is displayed would be the lucky number.
You can use a For-Loop to turn on/off each segment, or you can use a single command for 7 pins all at once. The built-in WriteportX is one of three commands to write data to specific I/O port or pin. For more information, see the innoBasic manual or help. As we are using pin 0 to pin 7, the command would be Wirteport0.
To turn off the segments, pass &hFF as argument to Writeport0, where FF means 2 sets of 0000, which is the equivalent of &B00000000.
Testing your circuit and code
As always, in case of error, test your code with Debug command to see if the Random command generates any number. The result will be display on the Terminal Window of InnoBasic Workshop. Although this is a simple application, there are many wires and components involved on the bread board. Make sure all components are connected correctly before running the program.
Press the button once to trigger the display. The 7 segment display would start displaying random numbers. When it stops, it displays the lucky number. Press the button again to display another number. The program will go back again to the starting point indicated by the label Wait.
All parts are included in the Explorer Kit.
What is next?
Now we can move on to a 2-digits seven segment display. After all, almost all the devices have at least 2 digits, like the digital clock. Can you figure out how to make a digital clock with 2 digits seven segment display? Share your ideas and comments with us or post your questions on our Forum.
FAQ | Terms & Conditions | Contact Us