Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
Digital Dice
In the last project, we learned how to use 74HC595 to light up the LED Segment Display. Of course, we can expand on this basis to make it more interesting.
Here, we will add a button to the previous project to make a digital dice. When the button is pressed, the 7 segment display will randomly show 1-6.
Schematic
Wiring
Code
When the program starts running, every time you press the button, the digital tube will randomly display a number from 1 to 6.
How it works?
SEGCODE = [0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f]
First, define a hexadecimal array to represent the number from 0 to 9 displayed by the 7 segment display.
if (digitalRead(button)==HIGH){
dice = random(1,7);
}
When the value of the button is HIGH, i.e. the key has been pressed, the random() function is called to generate a random number from 1 to 6 to be stored in the variable dice.
The following table shows the codes corresponding to these numbers.
Numbers |
Binary Code |
Hex Code |
|---|---|---|
0 |
00111111 |
0x3f |
1 |
00000110 |
0x06 |
2 |
01011011 |
0x5b |
3 |
01001111 |
0x4f |
4 |
01100110 |
0x66 |
5 |
01101101 |
0x6d |
6 |
01111101 |
0x7d |
7 |
00000111 |
0x07 |
8 |
01111111 |
0x7f |
9 |
01101111 |
0x6f |
dice = random(1,7) the random function generates pseudo-random numbers from 1 to 6(Exclude 7).
digitalWrite(STcp,LOW); //ground ST_CP and hold low for as long as you are transmitting
shiftOut(DS,SHcp,MSBFIRST,datArray[dice]);
digitalWrite(STcp,HIGH); //pull the ST_CPST_CP to save the data
delay(100); //wait for a second
Display the corresponding number on the 7 segment display via shiftOut() function.