Binærteller basert på Python og gpiozero
Program for testing av oppkobling:
import gpiozero as gpzero from time import sleep resetbutton = gpzero.Button(3) leds = gpzero.LEDBoard(26,16,20,21) # test trykknappen while True: if resetbutton.is_pressed: print("Trykknappen virker.") break else: print("Test ut trykknappen.") # test led lysene leds.on() sleep(3) leds.value = (0,0,0,1) sleep(1) leds.value = (0,0,1,0) sleep(1) leds.value = (0,1,0,0) sleep(1) leds.value = (1,0,0,0) sleep(1) leds.off()
Program for binærteller:
# Eksempel på Raspberry Pi binærteller med gpiozero # Ref: gpiozero.readthedocs.io import gpiozero as gpzero from time import sleep # Gjør klar trykknapp and bank med led-lys resetbutton = gpzero.Button(3) leds = gpzero.LEDBoard(26,16,20,21) # Funksjoner def LightsOn (): while resetbutton.is_pressed: leds.on() def ResetCounter (): global counter leds.off() counter = 0 def binary2lights(showThis): leds.value = ( showThis & 0b1000, showThis & 0b0100, showThis & 0b0010, showThis & 0b0001) # Ta i mot kommandoer fra trykknapp resetbutton.when_pressed = LightsOn resetbutton.when_released = ResetCounter # Sende 0...15 til LED-Lampene while True: ResetCounter() while counter < 16: binary2lights(counter) counter += 1 sleep(1)