Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Raspberry Pi LED Blueprints

You're reading from  Raspberry Pi LED Blueprints

Product type Book
Published in Sep 2015
Publisher
ISBN-13 9781782175759
Pages 180 pages
Edition 1st Edition
Languages
Author (1):
Agus Kurniawan Agus Kurniawan
Profile icon Agus Kurniawan

Table of Contents (14) Chapters

Raspberry Pi LED Blueprints
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Getting Started with LED Programming through Raspberry Pi GPIO Make Your Own Countdown Timer Make Your Own Digital Clock Display LED Dot Matrix Building Your Own Traffic Light Controller Building Your Own Light Controller-based Bluetooth Making Your Own Controlled Lamps Through Internet Network 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...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}