from gpiozero import LEDBoard from time import sleep from signal import pause leds = LEDBoard(5, 6, 13, 19, 26) leds.on() sleep(1) leds.off() sleep(1) leds.value = (1, 0, 1, 0, 1) sleep(1) leds.blink() pause()
def active_seismometer(callback, callback_interval): buzzer = Buzzer(3) status_led = LED(26) scale_led = LEDBoard(a=18, b=23, c=12, d=19, e=6, f=22, g=17, xdp=16) seismometer = Seismometer() seismometer.start_calculation(callback, callback_interval) while True: try: seismic_scale = seismometer.seismic_scale scale_led.value = SCALE_LED_CHARSETS[ seismometer.get_user_friendly_formatted_seismic_scale()] if seismometer.ready: if not status_led.is_lit: status_led.on() if seismic_scale >= 3.5: if not buzzer.is_active: buzzer.on() else: buzzer.off() except KeyboardInterrupt: break seismometer.stop_calculation() scale_led.off() status_led.off() buzzer.off()
class Tree(object): Lower = 2 Upper = 28 Period = 20 def __init__(self): self.tree = LEDBoard(*range(Tree.Lower, Tree.Upper), pwm=True) def reset(self): for led in self.tree: led.source_delay = 0 led.source = [0] def off(self): self.tree.off() def on(self): self.tree.on() def pulse(self): self.tree.pulse(fade_in_time=4, fade_out_time=4) def flicker(self): for led in self.tree: led.source_delay = 0.2 led.source = random_values() def sequence(self): delay = Tree.Period * 1.0 / (Tree.Upper - Tree.Lower) values = [] for index in range(Tree.Lower, Tree.Upper): values.append(0) index = 0 for led in self.tree: #delay += 0.1 led.source_delay = delay values[index] = 1 led.source = list(values) values[index] = 0 index += 1 #print( led.source ) #led.on() #sleep( 0.1 ) #led.off() def wait(self): sleep(Tree.Period) def go(self): while True: self.sequence() self.wait() self.pulse() self.wait() self.flicker() self.wait() self.reset()
def sevensegment(ch,d,pinout): #ch is the character to display #d is the duration in seconds #pinout is the pinout for the display from gpiozero import LEDBoard from time import sleep import traceback num = { '1': (3,6), '2': (2,3,0,4,5), '3': (2,3,0,6,5), '4': (1,0,3,6), '5': (2,1,0,6,5), '6': (2,1,4,5,6,0), '7': (2,3,6), '8': (0,1,2,3,4,5,6), '9': (2,3,1,0,6), '0': (2,1,4,5,6,3), 'A': (4,1,2,3,0,6), 'C': (2,1,4,5), 'E': (2,1,0,4,5), 'F': (2,1,0,4), 'H': (1,4,0,3,6), 'J': (3,6,5,4), 'L': (1,4,5), 'P': (2,3,0,1,4), 'U': (1,4,5,6,3) } # Validate the inputs - ch needs to be in the num dictionary, and d needs to be an integer try: if ch not in num: raise ValueError ("Can't display that value on seven segment display)") if not type(d) is int: raise TypeError ("Expecting an integer for duration") if not type(pinout) is tuple: raise TypeError ("Need a tuple for pinouts") if len(pinout) != 8: raise IndexError ("Need list of 8 pinouts") for a in range(0,len(pinout)): if not (1<=pinout[a]<=26): raise ValueError ("GPIO pinouts must be from 1 to 26") except ValueError as error: traceback.print_exc() except TypeError as error: traceback.print_exc() except IndexError as error: traceback.print_exc() else: leds = LEDBoard(*pinout) for a in range(0,len(num[ch])): leds[num[ch][a]].on() sleep(d) leds.off()
class Graphic: WIDTH = 12 HEIGHT = 9 RENDER_DELAY = .001 FPS = 60 TPF = float(1) / FPS def __init__(self, panel): self._panel = panel if self._panel.width not in range( Graphic.WIDTH + 1) and self._panel.height not in range(Graphic.HEIGHT + 1): raise SystemError('Panel size is illegal!') self._running = True self._coms = LEDBoard(2, 3, 4, 17, 27, 22, 10, 9, 11) self._segs = LEDBoard(14, 15, 18, 23, 24, 25, 8, 7, 12, 16, 20, 21) self._canvas = Canvas(self._panel.width, self._panel.height) self._render_thread = Thread(target=self.render) self._update_thread = Thread(target=self.update) def render(self): while self._running: for i in range(self._panel.height): self._segs.value = self._canvas.matrix[i] com = self._coms.leds[i] com.on() sleep(Graphic.RENDER_DELAY) com.off() def update(self): last = time() while self._running: start = time() current = time() self._panel.render(self._canvas, current - last) last = current try: sleep(start + Graphic.TPF - time()) except IOError: pass def start(self): self._coms.off() self._segs.on() self._render_thread.start() self._update_thread.start() def stop(self): self._running = False self._render_thread.join() self._update_thread.join() self._segs.close() self._coms.close()
def top_down(forward): # function that lights the leds from top down. # Or from bottom up if forward is not True. branch1_leds = LEDBoard(27, 17, 4, 18, 15, 14) branch2_leds = LEDBoard(11, 5, 8, 12, 6, 7) branch3_leds = LEDBoard(19, 16, 26, 13, 20, 21) branch4_leds = LEDBoard(25, 9, 22, 24, 23, 10) top_down_time = time.time() + uniform(period_min, period_max) while top_down_time > time.time(): branch1_leds.off() branch2_leds.off() branch3_leds.off() branch4_leds.off() time.sleep(top_down_delay2) if forward: for point in range(6): branch1_leds[point].on() branch2_leds[point].on() branch3_leds[point].on() branch4_leds[point].on() time.sleep(top_down_delay) else: for point in range(5, -1, -1): branch1_leds[point].on() branch2_leds[point].on() branch3_leds[point].on() branch4_leds[point].on() time.sleep(top_down_delay)
class Lights(): def __init__(self, hostIP): ''' initialize the hostIP and leds GPIO pins''' self.hostIP = hostIP # host is the one that controls the lights self.factory = PiGPIOFactory(host=hostIP) # 18, 19, 20, 21, 22, 23, 24, 25 are the connected GPIO pins self.leds = LEDBoard(18, 19, 20, 21, 22, 23, 24, 25, pwm=True, pin_factory=self.factory) def adjustableLED(self, brightness): ''' adjust LED intensity according to brightness assigned''' leds = self.processBrightness(brightness) self.leds.value = (leds[0], leds[1], leds[2], leds[3], leds[4], leds[5], leds[6], leds[7]) def hardOnOffLED(self, onTime, offTime, brightness): ''' turn LEDS light on for onTime and off for offTime, with assigned brightness''' while True: leds = self.processBrightness(brightness) self.leds.value = (leds[0], leds[1], leds[2], leds[3], leds[4], leds[5], leds[6], leds[7]) sleep(onTime) self.leds.off() sleep(offTime) def processBrightness(self, brightness): ''' Return leds switch value as a list 1 for on, 0 for off ''' if brightness > 255 or brightness < 0: print("invalid light brightness level") sys.exit(0) leds = [0, 0, 0, 0, 0, 0, 0, 0] # initially turn everything off binLevel = bin(brightness) # string of form '0b101010' binLevel = binLevel[2:] binList = list(binLevel) binList.reverse() # because the pins are in reversed order for i in range(0, len(binList)): if binList[i] == '1': leds[i] = 1 print("active led") print(leds) return leds
def spiral(): # function to create a spiral pattern that repeats for a while print("Spiral") branch1_leds = LEDBoard(27,17,4,18,15,14) branch2_leds = LEDBoard(11,5,8,12,6,7) branch3_leds = LEDBoard(19,16,26,13,20,21) branch4_leds = LEDBoard(25,9,22,24,23,10) branch1_leds.off() branch2_leds.off() branch3_leds.off() branch4_leds.off() time.sleep(delay) spiral_time = time.time() + uniform(period_min, period_max) while spiral_time > time.time(): # spiral down for point in range(6): branch1_leds[point].on() time.sleep(delay2) branch2_leds[point].on() time.sleep(delay2) branch3_leds[point].on() time.sleep(delay2) branch4_leds[point].on() time.sleep(delay2) time.sleep(delay) # spiral up for point in range(5, -1, -1): branch1_leds[point].off() time.sleep(delay2) branch2_leds[point].off() time.sleep(delay2) branch3_leds[point].off() time.sleep(delay2) branch4_leds[point].off() time.sleep(delay2) time.sleep(delay)
sleep(5) board.off() else: sleep(0.5) board.leds[led].on() break else: gameState = -1 buzzer.beep(n=1, background=False) for i in range(led, -1, -1): board.leds[i].off() sleep(0.5) break sleep(0.1) count += 1 while True: board.off() while not button.is_pressed: for n in range(0, 9): if button.is_pressed: break board.leds[n].toggle() sleep(0.25) game()
#!/usr/bin/env python3 ######################################################################## # Filename : LightWater.py # Description : Use LEDBar Graph(10 LED) # Author : www.freenove.com # modification: 2019/12/27 ######################################################################## from gpiozero import LEDBoard from time import sleep from signal import pause print ('Program is starting ... ') ledPins = ["J8:11", "J8:12","J8:13","J8:15","J8:16","J8:18","J8:22","J8:3","J8:5","J8:24"] leds = LEDBoard(*ledPins, active_high=False) while True: for index in range(0,len(ledPins),1): #move led(on) from left to right leds.on(index) sleep(0.1) #leds.off(index) for index in range(len(ledPins)-1,-1,-1): #move led(on) from right to left leds.off(index) sleep(0.1) #leds.off(index)
18: 19, 17: 20, 4: 21, 24: 22, 23: 23, 13: 24, 5: 25, 12: 26, 11: 27 } leds = LEDBoard(*range(4, 28), pwm=True) def labelToPin(l): return treemap[l] def toBoard(l): return labelToPin(l) - 4 while True: for i in treelights: sleep(0.1) leds.on(toBoard(i)) for i in treelights: sleep(0.1) leds.off(toBoard(i))
def inoroutchoice(selected_value): print('choice') global mode global mode_old global p_out global p_in if selected_value == "Input - pull-up" or selected_value == "Input - pull-down": mode = 'input' if mode_old == 'output': p_out.close() elif mode_old == 'input': p_in.close() mode_old = 'input' #p_out.close() if selected_value == "Input - pull-up": p_in = ButtonBoard(2,3,4,14,15,17,18,27,22,23,24,10,9,11,25,8,7,5,6,12,13,19,16,26,20,21) buttons[1].enable() buttons[1].bg = 'green' buttons[0].enable() buttons[0].bg = 'green' else: p_in = ButtonBoard(4,14,15,17,18,27,22,23,24,10,9,11,25,8,7,5,6,12,13,19,16,26,20,21,pull_up=False) buttons[1].disable() buttons[1].bg = 'light grey' buttons[0].disable() buttons[0].bg = 'light grey' #p_in = ButtonBoard(b2=2,b3=3,b20=20) p_in.when_pressed = pressed #p_in.when_released = released i=0 floaters = [] for p in p_in.value: print(p) if p == 1: floaters.append(i) print(i) if buttons[i].bg == 'grey': buttons[i].bg = 'green' elif buttons[i].bg == 'light grey': buttons[i].bg = 'light grey' else: buttons[i].bg = 'grey' else: if buttons[i].bg == 'light grey': buttons[i].bg = 'light grey' else: buttons[i].bg = 'green' i+=1 if len(floaters) > 0: app.warn("Possible floaters!", "Found " + str(len(floaters)) + " pins that may be floating high") elif selected_value == 'Output': mode = 'output' if mode_old == 'input': p_in.close() elif mode_old == 'output': p_out.close() mode_old = 'output' #p_in.close() p_out = LEDBoard(2,3,4,14,15,17,18,27,22,23,24,10,9,25,11,8,7,5,6,12,13,19,16,26,20,21) p_out.off() for b in buttons: if b.bg == 'grey': b.bg = 'green'
class PiApplication(object): def __init__(self, config, plugin_manager): self._pm = plugin_manager self._config = config # Create directories where pictures are saved for savedir in config.gettuple('GENERAL', 'directory', 'path'): if osp.isdir(savedir) and config.getboolean('GENERAL', 'debug'): shutil.rmtree(savedir) if not osp.isdir(savedir): os.makedirs(savedir) # Prepare the pygame module for use os.environ['SDL_VIDEO_CENTERED'] = '1' pygame.init() # Create window of (width, height) init_size = self._config.gettyped('WINDOW', 'size') init_debug = self._config.getboolean('GENERAL', 'debug') init_color = self._config.gettyped('WINDOW', 'background') init_text_color = self._config.gettyped('WINDOW', 'text_color') if not isinstance(init_color, (tuple, list)): init_color = self._config.getpath('WINDOW', 'background') title = 'Pibooth v{}'.format(pibooth.__version__) if not isinstance(init_size, str): self._window = PtbWindow(title, init_size, color=init_color, text_color=init_text_color, debug=init_debug) else: self._window = PtbWindow(title, color=init_color, text_color=init_text_color, debug=init_debug) self._menu = None self._multipress_timer = PoolingTimer( config.getfloat('CONTROLS', 'multi_press_delay'), False) # Define states of the application self._machine = StateMachine(self._pm, self._config, self, self._window) self._machine.add_state('wait') self._machine.add_state('choose') self._machine.add_state('chosen') self._machine.add_state('preview') self._machine.add_state('capture') self._machine.add_state('processing') self._machine.add_state('filter') self._machine.add_state('print') self._machine.add_state('finish') # --------------------------------------------------------------------- # Variables shared with plugins # Change them may break plugins compatibility self.capture_nbr = None self.capture_date = None self.capture_choices = (4, 1) self.previous_picture = None self.previous_animated = None self.previous_picture_file = None self.count = Counters(self._config.join_path("counters.pickle"), taken=0, printed=0, forgotten=0, remaining_duplicates=self._config.getint( 'PRINTER', 'max_duplicates')) self.camera = camera.get_camera( config.getint('CAMERA', 'iso'), config.gettyped('CAMERA', 'resolution'), config.getint('CAMERA', 'rotation'), config.getboolean('CAMERA', 'flip'), config.getboolean('CAMERA', 'delete_internal_memory')) self.buttons = ButtonBoard( capture="BOARD" + config.get('CONTROLS', 'picture_btn_pin'), printer="BOARD" + config.get('CONTROLS', 'print_btn_pin'), hold_time=config.getfloat('CONTROLS', 'debounce_delay'), pull_up=True) self.buttons.capture.when_held = self._on_button_capture_held self.buttons.printer.when_held = self._on_button_printer_held self.leds = LEDBoard( capture="BOARD" + config.get('CONTROLS', 'picture_led_pin'), printer="BOARD" + config.get('CONTROLS', 'print_led_pin')) self.printer = Printer(config.get('PRINTER', 'printer_name'), config.getint('PRINTER', 'max_pages'), self.count) # --------------------------------------------------------------------- def _initialize(self): """Restore the application with initial parameters defined in the configuration file. Only parameters that can be changed at runtime are restored. """ # Handle the language configuration language.CURRENT = self._config.get('GENERAL', 'language') fonts.CURRENT = fonts.get_filename( self._config.gettuple('PICTURE', 'text_fonts', str)[0]) # Set the captures choices choices = self._config.gettuple('PICTURE', 'captures', int) for chx in choices: if chx not in [1, 2, 3, 4]: LOGGER.warning( "Invalid captures number '%s' in config, fallback to '%s'", chx, self.capture_choices) choices = self.capture_choices break self.capture_choices = choices # Handle autostart of the application self._config.handle_autostart() self._window.arrow_location = self._config.get('WINDOW', 'arrows') self._window.arrow_offset = self._config.getint( 'WINDOW', 'arrows_x_offset') self._window.text_color = self._config.gettyped('WINDOW', 'text_color') self._window.drop_cache() # Handle window size size = self._config.gettyped('WINDOW', 'size') if isinstance(size, str) and size.lower() == 'fullscreen': if not self._window.is_fullscreen: self._window.toggle_fullscreen() else: if self._window.is_fullscreen: self._window.toggle_fullscreen() self._window.debug = self._config.getboolean('GENERAL', 'debug') # Handle debug mode if not self._config.getboolean('GENERAL', 'debug'): set_logging_level() # Restore default level self._machine.add_failsafe_state('failsafe') else: set_logging_level(logging.DEBUG) self._machine.remove_state('failsafe') # Reset the print counter (in case of max_pages is reached) self.printer.max_pages = self._config.getint('PRINTER', 'max_pages') def _on_button_capture_held(self): """Called when the capture button is pressed. """ if all(self.buttons.value): self.buttons.capture.hold_repeat = True if self._multipress_timer.elapsed() == 0: self._multipress_timer.start() if self._multipress_timer.is_timeout(): # Capture was held while printer was pressed if self._menu and self._menu.is_shown(): # Convert HW button events to keyboard events for menu event = self._menu.create_back_event() LOGGER.debug("BUTTONDOWN: generate MENU-ESC event") else: event = pygame.event.Event(BUTTONDOWN, capture=1, printer=1, button=self.buttons) LOGGER.debug("BUTTONDOWN: generate DOUBLE buttons event") self.buttons.capture.hold_repeat = False self._multipress_timer.reset() pygame.event.post(event) else: # Capture was held but printer not pressed if self._menu and self._menu.is_shown(): # Convert HW button events to keyboard events for menu event = self._menu.create_next_event() LOGGER.debug("BUTTONDOWN: generate MENU-NEXT event") else: event = pygame.event.Event(BUTTONDOWN, capture=1, printer=0, button=self.buttons.capture) LOGGER.debug("BUTTONDOWN: generate CAPTURE button event") self.buttons.capture.hold_repeat = False self._multipress_timer.reset() pygame.event.post(event) def _on_button_printer_held(self): """Called when the printer button is pressed. """ if all(self.buttons.value): # Printer was held while capture was pressed # but don't do anything here, let capture_held handle it instead pass else: # Printer was held but capture not pressed if self._menu and self._menu.is_shown(): # Convert HW button events to keyboard events for menu event = self._menu.create_click_event() LOGGER.debug("BUTTONDOWN: generate MENU-APPLY event") else: event = pygame.event.Event(BUTTONDOWN, capture=0, printer=1, button=self.buttons.printer) LOGGER.debug("BUTTONDOWN: generate PRINTER event") pygame.event.post(event) @property def picture_filename(self): """Return the final picture file name. """ if not self.capture_date: raise EnvironmentError( "The 'capture_date' attribute is not set yet") return "{}_pibooth.jpg".format(self.capture_date) def find_quit_event(self, events): """Return the first found event if found in the list. """ for event in events: if event.type == pygame.QUIT: return event return None def find_settings_event(self, events): """Return the first found event if found in the list. """ for event in events: if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: return event if event.type == BUTTONDOWN and event.capture and event.printer: return event return None def find_fullscreen_event(self, events): """Return the first found event if found in the list. """ for event in events: if event.type == pygame.KEYDOWN and \ event.key == pygame.K_f and pygame.key.get_mods() & pygame.KMOD_CTRL: return event return None def find_resize_event(self, events): """Return the first found event if found in the list. """ for event in events: if event.type == pygame.VIDEORESIZE: return event return None def find_capture_event(self, events): """Return the first found event if found in the list. """ for event in events: if event.type == pygame.KEYDOWN and event.key == pygame.K_p: return event if event.type == pygame.MOUSEBUTTONUP and event.button in (1, 2, 3): # Don't consider the mouse wheel (button 4 & 5): rect = self._window.get_rect() if pygame.Rect(0, 0, rect.width // 2, rect.height).collidepoint(event.pos): return event if event.type == BUTTONDOWN and event.capture: return event return None def find_print_event(self, events): """Return the first found event if found in the list. """ for event in events: if event.type == pygame.KEYDOWN and event.key == pygame.K_e\ and pygame.key.get_mods() & pygame.KMOD_CTRL: return event if event.type == pygame.MOUSEBUTTONUP and event.button in (1, 2, 3): # Don't consider the mouse wheel (button 4 & 5): rect = self._window.get_rect() if pygame.Rect(rect.width // 2, 0, rect.width // 2, rect.height).collidepoint(event.pos): return event if event.type == BUTTONDOWN and event.printer: return event return None def find_print_status_event(self, events): """Return the first found event if found in the list. """ for event in events: if event.type == PRINTER_TASKS_UPDATED: return event return None def find_choice_event(self, events): """Return the first found event if found in the list. """ for event in events: if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT: return event if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT: return event if event.type == pygame.MOUSEBUTTONUP and event.button in (1, 2, 3): # Don't consider the mouse wheel (button 4 & 5): print("MOUSEBUTTONUP") rect = self._window.get_rect() x, y = event.pos if pygame.Rect(0, 0, rect.width // 2, rect.height).collidepoint(event.pos): event.key = pygame.K_LEFT else: event.key = pygame.K_RIGHT return event if event.type == BUTTONDOWN: if event.capture: event.key = pygame.K_LEFT else: event.key = pygame.K_RIGHT return event return None def main_loop(self): """Run the main game loop. """ try: fps = 40 clock = pygame.time.Clock() self._initialize() self._pm.hook.pibooth_startup(cfg=self._config, app=self) self._machine.set_state('wait') while True: events = list(pygame.event.get()) if self.find_quit_event(events): break if self.find_fullscreen_event(events): self._window.toggle_fullscreen() event = self.find_resize_event(events) if event: self._window.resize(event.size) if not self._menu and self.find_settings_event(events): self.camera.stop_preview() self.leds.off() self._menu = PiConfigMenu(self._window, self._config, self.count) self._menu.show() self.leds.blink(on_time=0.1, off_time=1) elif self._menu and self._menu.is_shown(): self._menu.process(events) elif self._menu and not self._menu.is_shown(): self.leds.off() self._initialize() self._machine.set_state('wait') self._menu = None else: self._machine.process(events) pygame.display.update() clock.tick( fps ) # Ensure the program will never run at more than <fps> frames per second except Exception as ex: LOGGER.error(str(ex), exc_info=True) LOGGER.error(get_crash_message()) finally: self._pm.hook.pibooth_cleanup(app=self) pygame.quit()
# 3- Fades in and out using PWN (gpizero api) ## import time from gpiozero import LEDBoard from gpiozero.tools import random_values from signal import pause tree = LEDBoard(*range(2,28),pwm=True) print "DIAG_XMAS - Tree diagnostics terminal..." print "DIAG_X_001 - Loop all LEDs ON" for led in tree: print "DIAG_X_003 - LED "+ str(led.pin) + " - ON" led.on() time.sleep(1) print "DIAG_X_002 - All LEDs OFF" tree.off() time.sleep(1) print "DIAG_X_003 - All LEDs ON" tree.on() time.sleep(3) print "DIAG_X_004 - All LEDs Fade on and off 3 times with PWN" tree.off() tree.pulse(1,1,3,False) print "DIAG_XMAS - Diagnostics Complete. Merry XMAS!"
#Library imports from gpiozero import LEDBoard from time import sleep #Declarations red = LEDBoard(2, 17, 10, 5) yellow = LEDBoard(3, 27, 9, 6) permissiveYellow = LEDBoard(4) rightYellow = LEDBoard(19) green = LEDBoard(22, 11, 13) protectedGreen = LEDBoard(14) rightGreen = LEDBoard(26) while True: #Setup red.off() yellow.off() permissiveYellow.off() rightYellow.off() green.on() protectedGreen.on() rightGreen.off() #Protected to permissive left sleep(5) #Number of seconds protected green light is on. protectedGreen.off() permissiveYellow.blink(1, 1, 10, False) #This blinks every second for 10 blinks. #Permissive left and green to yellow green.off()
leadMin = leadMin + 1 else: trailMin = trailMin + 1 # if not at 11pm, carry this out # increment hour by 1 if at 59 minutes else: if leadMin == 5 and trailMin == 9: leadMin, trailMin = 0, 0 # determine whether to increment leading hour or not if trailHour == 9: trailHour = 0 leadHour = leadHour + 1 else: trailHour = trailHour + 1 # increment minute by 1 if not at 59 # determine whether to increment leading Minute or not else: if trailMin == 9: trailMin = 0 leadMin = leadMin + 1 else: trailMin = trailMin + 1 else: AN.value = (0, 0) tubesOn.off() break call("sudo shutdown -h now", shell=True)
sleep(5) board.off() else: sleep(0.5) board.leds[led].on() break else: gameState = -1 buzzer.beep(n=1,background=False) for i in range(led, -1, -1): board.leds[i].off() sleep(0.5) break sleep(0.1) count += 1 while True: board.off() while not button.is_pressed: for n in range(0,9): if button.is_pressed: break board.leds[n].toggle() sleep(0.25) game()
tree = LEDBoard(6, 7, 8) tree1 = LEDBoard(13, 16, 20) tree2 = LEDBoard(19, 21, 26) tree3 = LEDBoard(9, 10, 22) tree4 = LEDBoard(23, 24, 25) tree5 = LEDBoard(15, 18, 27) tree6 = LEDBoard(4, 17, 14) tree7 = LEDBoard(5, 11, 12) tree8 = PWMLED(2) #This is the star while True: tree8.pulse() #Put this first so it starts immediately tree.on() sleep(0.5) tree.off() tree1.on() sleep(0.5) tree1.off() tree2.on() sleep(0.5) tree2.off() tree3.on() sleep(1) tree3.off() tree4.on() sleep(0.5) tree4.off() tree5.on() sleep(1) tree5.off()
from gpiozero import LEDBoard from time import sleep leds = LEDBoard(red=LEDBoard(top=2, bottom=3), green=LEDBoard(top=4, bottom=5)) leds.red.on() ## both reds on sleep(1) leds.green.on() # both greens on sleep(1) leds.off() # all off sleep(1) leds.red.top.on() # top red on sleep(1) leds.green.bottom.on() # bottom green on sleep(1)
# ## speed = 0.5 import time from gpiozero import LEDBoard from gpiozero.tools import random_values from signal import pause east = LEDBoard(16, 13, 20, 19, 26, 21, pwm=True) south = LEDBoard(25, 24, 23, 9, 22, 10, pwm=True) west = LEDBoard(17, 4, 14, 27, 18, 15, pwm=True) north = LEDBoard(8, 6, 7, 11, 5, 12, pwm=True) while True: south.off() north.on() time.sleep(speed) west.off() east.on() time.sleep(speed) north.off() south.on() time.sleep(speed) east.off() west.on() time.sleep(speed)
value_up = numpy.arange(0, 21) / 20 def smooth_start(led, value): for i in value: led.value = i def smooth_middle(led1, led2, value): for i in value: led1.value = 1 - i led2.value = i def smooth_end(led, value): for i in value: led.value = 1 - i leds_green.off() for i in range(len(leds_green) + 3): if i <= 2: smooth_start(leds_green[i], value_up) elif i >= len(leds_green): smooth_end(leds_green[i - 3], value_up) else: smooth_middle(leds_green[i - 3], leds_green[i], value_up) leds_green.off()
def play_a_different_song(): global _currently_playing_song, _songs next_song = random.choice(_songs) while next_song == _currently_playing_song: next_song = random.choice(_songs) _currently_playing_song = next_song pygame.mixer.music.load(next_song) pygame.mixer.music.play(0) # Timer turn light off lightoff(time) count = 0 print("Turn light off and start countdown\n") led.off() #start count down print("count down started\n") while count < delay: count = count + 1 # here if the input goes high again we reset the counter to 0 if pir.motion_detected: count = 0 print("Count down now ", (delay - count)) time.sleep(1) print("") print("Motion NOT detected for", (delay), "Sec.\n")
from gpiozero import LEDBoard from time import sleep from signal import pause leds = LEDBoard(18, 23, 24) # 모두 같이 동작 leds.on() sleep(1) leds.off() sleep(1) # 개별적인 값을 튜플로 지정 leds.value = (1, 0, 1) sleep(1) leds.blink() pause()
def sides(forward): # function that lights the leds on the sides in sequence # direction is defined by forward being True or False side1_leds = LEDBoard(8, 6, 7) side2_leds = LEDBoard(11, 5, 12) side3_leds = LEDBoard(19, 26, 21) side4_leds = LEDBoard(16, 13, 20) side5_leds = LEDBoard(25, 24, 23) side6_leds = LEDBoard(9, 22, 10) side7_leds = LEDBoard(17, 4, 14) side8_leds = LEDBoard(27, 18, 15) sides_time = time.time() + uniform(period_min, period_max) while sides_time > time.time(): if forward: # spin the side leds anticlockwise side1_leds.on() time.sleep(sides_delay) side1_leds.off() side2_leds.on() time.sleep(sides_delay) side2_leds.off() side3_leds.on() time.sleep(sides_delay) side3_leds.off() side4_leds.on() time.sleep(sides_delay) side4_leds.off() side5_leds.on() time.sleep(sides_delay) side5_leds.off() side6_leds.on() time.sleep(sides_delay) side6_leds.off() side7_leds.on() time.sleep(sides_delay) side7_leds.off() side8_leds.on() time.sleep(sides_delay) side8_leds.off() else: # spin the side leds clockwise side8_leds.on() time.sleep(sides_delay) side8_leds.off() side7_leds.on() time.sleep(sides_delay) side7_leds.off() side6_leds.on() time.sleep(sides_delay) side6_leds.off() side5_leds.on() time.sleep(sides_delay) side5_leds.off() side4_leds.on() time.sleep(sides_delay) side4_leds.off() side3_leds.on() time.sleep(sides_delay) side3_leds.off() side2_leds.on() time.sleep(sides_delay) side2_leds.off() side1_leds.on() time.sleep(sides_delay) side1_leds.off()
for rightknob_count in rightknobsound: print(rightknob_count) print(rightknobnum) rightknob_count = 0 def rightknobplay(): Popen([ 'bash', '/home/pi/buttonboard/lightshowpiMods/play', rightknobsound[rightknob_count] ]).wait() # LIGHTS CAMERA ACTION while True: allLEDS.off() if redButton.is_pressed: allLEDS.on() redplay() red_count += 1 print(red_count) if red_count >= rednum: red_count = 0 if blueButton.is_pressed: allLEDS.on() blueplay() blue_count += 1 print(blue_count) if blue_count >= bluenum: blue_count = 0 if yellowButton.is_pressed:
board17 = LEDBoard(20, pwm=True) board04 = LEDBoard(21, pwm=True) board24 = LEDBoard(22, pwm=True) board23 = LEDBoard(23, pwm=True) board13 = LEDBoard(24, pwm=True) board05 = LEDBoard(25, pwm=True) board12 = LEDBoard(26, pwm=True) board11 = LEDBoard(27, pwm=True) speed = 5.0 # Turn off time.sleep(speed) board24.on() time.sleep(speed) board24.off() board23.on() time.sleep(speed) board23.off() board22.on() time.sleep(speed) board22.off() board21.on() time.sleep(speed) board21.off() board20.on() time.sleep(speed) board20.off() board19.on() time.sleep(speed) board19.off()
"J8:11", "J8:12", "J8:13", "J8:15", "J8:16", "J8:18", "J8:22", "J8:3", "J8:5", "J8:24" ] leds = LEDBoard(*ledPins, active_high=False) #define array of 10 bit : 0000000000 binary = [] for i in range(10): binary.append(0) print("Init bit array:" + str(binary)) #convert number in binary string number = numberToConvert i = len(binary) - 1 while number > 0: bit = number % 2 binary[i] = bit number = number // 2 i = i - 1 print("Decimal is " + str(numberToConvert) + " Binary is :" + str(binary)) for i in range(len(binary) - 1, -1, -1): if binary[i] == 1: leds.on(i) else: leds.off(i)
import time from gpiozero import LEDBoard, Button saidas = LEDBoard(0, 4, 17, 22, 10, 11, 14, 15) entradas = Button(23) from firebase import firebase #importa a biblioteca de compatibilidade com firebase firebase = firebase.FirebaseApplication('https://autohome-cfe1f.firebaseio.com/', None) saidas.off() #bounce time is given in ms and represents the mininum time between two callbacks GPIO.add_event_detect(23, GPIO.RISING, bouncetime=5000) def my_callback_luz(self): luz = firebase.get('/status_luz', None) if GPIO.input(23) == True: print("funciona") if luz is not None: chave=list(luz.keys())[0] firebase.delete('/status_luz', chave) firebase.post('/status', "acender") firebase.post('/status_luz', "acender") else: firebase.post('status', "acender") firebase.post('status_luz', "acender") elif GPIO.input(23) == False: print("funciona2") if luz is not None: chave=list(luz.keys())[0] firebase.delete('/status_luz', chave)
## # Press enter to light up the tree ## from time import sleep from gpiozero import LEDBoard tree = LEDBoard(*range(2, 28), pwm=True) tree.off() # Turn all LED's off print("Press Enter to Light Up Tree") while True: raw_input() print("button pressed") tree.on() sleep(.2) tree.off()