Book Image

Raspberry Pi LED Blueprints

By : Agus Kurniawan
Book Image

Raspberry Pi LED Blueprints

By: Agus Kurniawan

Overview of this book

Table of Contents (14 chapters)
Raspberry Pi LED Blueprints
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Displaying a random number on the LED dot matrix display


In this section, we will explore more practices using 8 x 8 LED dot matrix displays. We will display a random number on the LED dot matrix module. A number will be generated by a Python program via random object and then shown on the LED dot matrix display.

Let's start to write the program. Create a file named ch04_02.py. Write the following completed code:

# ch04_02.py file

import max7219.led as led
import time
import random

device = led.matrix(cascaded=1)

print("Running...")
print("Press CTRL+C to exit ")
try:
    while 1:
        number = random.randint(0,9)
        print "display ", number
        device.letter(0, ord(str(number)))
        time.sleep(1)

except KeyboardInterrupt:
    device.command(led.constants.MAX7219_REG_SHUTDOWN,0x00)
    time.sleep(0.01)

print("done")

We can generate a random number by using random.randint() from random object. We pass (0,9) so it generates value from 0 to 9. After this, we pass this random...