Contents
Step 1 - Number generation without GPIO
The first step here is to get the random number generation working, however, we need to remember that when controlling the 7Seg, we only have 3 commands, count up, down, and reset. This means that we essentially have to start with a variable which will be 0 (as when reset the 7Seg goes down to 0), then increment this a random number of times. This incrementation must be between 1 and 6 times, so we end up with a number between 1 and 6.
## This is part of the tutorial series for the 7Seg LED Dispaly
## For more information please contact the author, Tom Garry
## http://7seg.co.uk http://tomgarry.co.uk
import time ## Import thye time library used for delays
from random import randint ## Import the randint function, used for generating the number to be displayed
def Scramble(): ## Define a function named scramble - this will simple count from 1-6 slowing down each time
var = 0
for i in range(1,randint(2,7)): ## Increment the number from 0 a random number of times (1-6)
time.sleep(0.001)
var = var + 1 ## Duplicates the output on the display ussing a varibale which will be returned to the console
time.sleep(0.05)
print var ## Print the number we have just generated
while(True): ## Create an infinite loop to keep repeating dice rolls
raw_input("Press 'Enter' to roll the dice") ## Request raw_input - the program will only contiune when the user presses enter
Scramble() ## Run the scramble function to 'roll' the dice
Step 2 - Getting it working on the GPIO
Now, we have to import the GPIO library. We set the mode to 'GPIO.BOARD', this helps the code work across different board revisions. We essentially mirror the process we designed in step 1, after initialising the 'reset pin' and 'up pin', we reset the display (at the same time that we declared the variable), we then pulse the up pin every time we increase the variable.
## This is part of the tutorial series for the 7Seg LED Dispaly
## For more information please contact the author, Tom Garry
## http://7seg.co.uk http://tomgarry.co.uk
import RPi.GPIO as GPIO ## Import GPIO library
import time ## Import thye time library used for delays
from random import randint ## Import the randint function, used for generating the number to be displayed
GPIO.setmode(GPIO.BOARD) ## Use board pin numbering - This should help insure compatability accross different RPi board revisions
rstPin = 15 ## The GPIO pin which connects to the reset for the 7Seg
upPin = 7 ## The GPIO pin which connects to the increment pin on the 7Seg
GPIO.setup(rstPin, GPIO.OUT) ## Setup the GPIO as an output
GPIO.setup(upPin, GPIO.OUT) ## Setup the GPIO as an output
def Scramble(): ## Define a function named scramble - this will simple count from 1-6 slowing down each time
GPIO.output(rstPin,True) ## Reset the 7Seg to 0
time.sleep(0.005)
GPIO.output(rstPin,False)
var = 0
for i in range(1,randint(2,7)): ## Increment the number from 0 a random number of times (1-6)
GPIO.output(upPin,True) ## Quickly, increment the number so it appears to start at 1
time.sleep(0.001)
var = var + 1 ## Duplicates the output on the display using a variable which will be returned to the console
GPIO.output(upPin,False)
time.sleep(0.05)
print var ## Print the number we have just generated
while(True): ## Create an infinite loop to keep repeating dice rolls
raw_input("Press 'Enter' to roll the dice") ## Request raw_input - the program will only contiune when the user presses enter
Scramble() ## Run the scramble function to 'roll' the dice
Step 3 - Making the roll effect
Finally, we want to scroll though a bunch of numbers before the random number is generated. In order to do this, we simply add a 'for' statement, which repeatedly counts from 1-6 on the display.
## This is part of the tutorial series for the 7Seg LED Dispaly
## For more information please contact the author, Tom Garry
## http://7seg.co.uk http://tomgarry.co.uk
import RPi.GPIO as GPIO ## Import GPIO library
import time ## Import thye time library used for delays
from random import randint ## Import the randint function, used for generating the number to be displayed
GPIO.setmode(GPIO.BOARD) ## Use board pin numbering - This should help insure compatability accross different RPi board revisions
rstPin = 15 ## The GPIO pin which connects to the reset for the 7Seg
upPin = 7 ## The GPIO pin which connects to the increment pin on the 7Seg
GPIO.setup(rstPin, GPIO.OUT) ## Setup the GPIO as an output
GPIO.setup(upPin, GPIO.OUT) ## Setup the GPIO as an output
def Scramble(): ## Define a function named scramble - this will simple count from 1-6 slowing down each time
for i in range(0,randint(4,6)): ## Start a for loop, which will run between 4 and 6 times, this will simply cycle through the numbers 1-6
GPIO.output(rstPin,True)
time.sleep(0.005)
GPIO.output(rstPin,False)
GPIO.output(upPin,True)
time.sleep(0.005)
GPIO.output(upPin,False)
for i in range(1,5):
GPIO.output(upPin,True)
time.sleep(0.005)
GPIO.output(upPin,False)
time.sleep(0.05) ## End of cycling loop - this part was just for show
GPIO.output(rstPin,True) ## Reset the 7Seg to 0
time.sleep(0.005)
GPIO.output(rstPin,False)
var = 0
for i in range(1,randint(2,7)): ## Increment the number from 0 a random number of times (1-6)
GPIO.output(upPin,True) ## Quickly, increment the number so it appears to start at 1
time.sleep(0.001)
var = var + 1 ## Duplicates the output on the display using a variable which will be returned to the console
GPIO.output(upPin,False)
time.sleep(0.05)
print var ## Print the number we have just generated
while(True): ## Create an infinite loop to keep repeating dice rolls
raw_input("Press 'Enter' to roll the dice") ## Request raw_input - the program will only contiune when the user presses enter
Scramble() ## Run the scramble function to 'roll' the dice
Conclusion
Thanks very much for reading! If this all seems to go a bit over you head, then please don't give up! There are loads of other sources on the internet, as well as some of the previous tutorials, which may be of help.