Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import RPi.GPIO as IO
- import time
- IO.setwarnings(False)
- IO.setmode(IO.BCM)
- IO.setup(18,IO.OUT) #data pin on the shift register
- IO.setup(23,IO.OUT) #push pin on the shift register
- IO.setup(24,IO.OUT) #display pin on the shift register
- #function to convert decimal number to an array of integers representing the binary equivalent of the decimal
- def convert_to_binary(decimal):
- #convert decimal number to binary number
- passthrough = format(decimal, 'b')
- #convert the binary number into an array of strings
- binary = list(passthrough)
- #pad the array to be 8 digits if necessary. now array is a mix of integers and strings
- while len(binary)< 8:
- binary.insert(0,0)
- #convert all array items to integers
- for i in range(8):
- binary[i] = int(binary[i])
- #output the array of integers
- return binary
- #function to output binary array to the LEDs
- def led_output(bin_arr):
- flip = bin_arr[::-1] #because of how a shift register works, we need to reverse the array then
- #send it to the register
- for x in flip: #iterate through the array and turn on/off an LED depending on the value of the item
- if x == 1:
- IO.output(18,1)
- IO.output(23,1)
- IO.output(23,0)
- IO.output(18,0)
- IO.output(24,1)
- IO.output(24,0)
- #function to clear the LEDs
- def led_clear():
- for z in range (8):
- IO.output(18,0)
- IO.output(23,1)
- IO.output(23,0)
- IO.output(24,1)
- IO.output(24,0)
- #count from 0 to 255 and output the binary number to the LEDs
- for num in range(256):
- led_output(convert_to_binary(num))
- time.sleep(0.2)
- led_clear()
- #count from 254 to 0 and output the binary number to the LEDs
- for num in range(254,-1,-1):
- led_output(convert_to_binary(num))
- time.sleep(0.2)
- led_clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement