Created
November 8, 2015 21:48
-
-
Save mandyRae/a78e9d731b60a1cb5b28 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Large Seven-Segment Display | |
Python Code for controlling display with a Raspberry Pi's GPIO | |
Written by Amanda on electrothoughts.wordpress.com, Aug 25, 2015 | |
For the original project visit https://electrothoughts.wordpress.com/2015/09/11/project-xl-seven-segment-display/ | |
Please feel free to modify this code to your heart's content! | |
This code contains function definitions. To make your display work, | |
it's recommended that you run the code in the Python shell IDE as root, and execute | |
functions from there, for example: | |
sudo python3 -i seven_segment_display.py | |
>>>flash(circles_flash, 0.5, 20) | |
>>>countdown() | |
Here's a simple graphical representation of the pin layout. There's | |
three LEDs per segment, and all number and animations are controlled | |
by the segment. | |
18-15-14 | |
17 23 | |
27 24 | |
22 25 | |
19-26-16 | |
10 12 | |
9 7 | |
11 8 | |
5-6-13 | |
''' | |
import RPi.GPIO as GPIO, time | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setwarnings(False) | |
seg1 = (18, 15, 14) | |
seg2 = (23, 24, 25) | |
seg3 = (12, 7, 8) | |
seg4 = (5, 6, 13) | |
seg5 = (10, 9, 11) | |
seg6 = (22, 27, 17) | |
seg7 = (19, 26, 16) | |
buzzer = 21 | |
segments = [seg1, seg2, seg3, seg4, seg5, seg6, seg7] | |
''' | |
Animaton 'Flash' Data | |
The display can display other animations other than numbers. For example, | |
the figure_eight_flash makes the the segments light up each individually and | |
consecutively in a figure-eight shape. In the flash function far below, these | |
lists of segments define the order of the animations. Experiment with segment | |
orders and make your own flashes. The circles_flash only has two frames: it has | |
four segments lit per frame. | |
''' | |
figure_eight_flash = [[seg1], [seg2], [seg7], [seg5], [seg4], [seg3], [seg7], [seg6]] | |
outside_flash = [[seg1], [seg2], [seg3], [seg4], [seg5], [seg6]] | |
center_flash = [[seg1], [seg7], [seg4], [seg7]] | |
circles_flash = [[seg1, seg2, seg6, seg7],[seg3, seg4, seg5, seg7]] | |
#setup lines | |
for seg in segments: | |
for pin in seg: | |
GPIO.setup(pin, GPIO.OUT) | |
GPIO.setup(buzzer, GPIO.OUT) | |
def buzz(buzzer, pitch, duration): | |
''' | |
buzzer: int, pin number of piezo buzzer | |
pitch: integer frequency in hertz | |
duration: float or int, duration of the buzz | |
This is a standard function for operating a piezo buzzer at a | |
certain frequency for a set duration. | |
''' | |
period = 1.0/pitch | |
delay = period/2 | |
cycles = int(duration * pitch) | |
for i in range(cycles): | |
GPIO.output(buzzer, True) | |
time.sleep(delay) | |
GPIO.output(buzzer, False) | |
time.sleep(delay) | |
def clearDisplay(): | |
''' | |
Turns off all LEDs and segments regardless of current state. | |
''' | |
for seg in segments: | |
for pin in seg: | |
GPIO.output(pin, False) | |
def lightSeg(seg_list): | |
''' | |
seg_list: list of segment(s) to be turned on | |
Clears display, then turns the indicated segments on. | |
''' | |
clearDisplay() | |
for seg in seg_list: | |
for pin in seg: | |
GPIO.output(pin, True) | |
def displayNumber(num): | |
''' | |
num: a string, '0' to '9' | |
Takes an integer in string format and display that number on the display | |
''' | |
if num == '0': | |
lightSeg([seg1, seg2, seg3, seg4, seg5, seg6]) | |
if num == '1': | |
lightSeg([seg2, seg3]) | |
if num == '2': | |
lightSeg([seg1, seg2, seg7, seg5, seg4]) | |
if num == '3': | |
lightSeg([seg1, seg2, seg3, seg4, seg7]) | |
if num == '4': | |
lightSeg([seg2, seg3, seg6, seg7]) | |
if num == '5': | |
lightSeg([seg1, seg6, seg7, seg3, seg4]) | |
if num == '6': | |
lightSeg([seg1, seg3, seg4, seg5, seg6, seg7]) | |
if num == '7': | |
lightSeg([seg1, seg2, seg3]) | |
if num == '8': | |
lightSeg([seg1, seg2, seg3, seg4, seg5, seg6, seg7]) | |
if num == '9': | |
lightSeg([seg1, seg2, seg3, seg4, seg6, seg7]) | |
def countdown(delay=1): | |
''' | |
delay: int or float, seconds between each number change, default is 1 sec, | |
must be >0.5 | |
Displays numbers 9-0, holding each for the delay amount. The buzzer buzzes | |
as part of the countdown. | |
''' | |
clearDisplay() | |
for i in range(10, 0 ,-1): #for num 9 to 0, display that number | |
displayNumber(str(i)) | |
buzz(buzzer, 256, 0.5) #buzz the buzzer at each number change | |
time.sleep(delay-0.5) | |
displayNumber('0') | |
buzz(buzzer, 660, 1) #final higher pitched '0' buzz | |
clearDisplay() #clear display when finished counting down | |
def flash(flash_data, speed, reps): | |
''' | |
flash_data: list or list of lists of the flash data, list of frames to display | |
speed: int or float, seconds between each frame | |
reps: int, number of time to repeat the animation | |
This function displays an animation defined by flash_data. It can display | |
both frames with one segment and with multiple segments. | |
''' | |
clearDisplay() | |
for i in range(reps): | |
for frame in flash_data: #for each frame, turn on all segments | |
for seg in frame: | |
for pin in seg: | |
GPIO.output(pin,True) | |
time.sleep(speed) #hold frame for the speed time | |
clearDisplay() #clear display for next frame |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment