/ LED_Noodle_Lantern / code.py
code.py
1 # SPDX-FileCopyrightText: 2022 Noe Ruiz for Adafruit Industries 2 # SPDX-License-Identifier: MIT 3 # Adafruit nOOds lantern with "analog" (PWM) brightness control using GPIO. 4 # Uses 6 nOOds, anode (+) to GPIO pin, cathode (-) to ground. 5 # A current-limiting resistor (e.g. 10 Ohm) can go at either end. 6 7 import math 8 import time 9 import board 10 import pwmio 11 from digitalio import DigitalInOut, Direction, Pull 12 13 PINS = (board.SCK, board.MOSI, board.A1, board.A3, board.MISO, board.A2) # List of pins 14 GAMMA = 2.6 # For perceptually-linear brightness 15 16 # Convert pin number list to PWMOut object list 17 pin_list = [pwmio.PWMOut(pin, frequency=1000, duty_cycle=0) for pin in PINS] 18 19 # Button switch set up 20 switch = DigitalInOut(board.A0) 21 switch.direction = Direction.INPUT 22 switch.pull = Pull.UP 23 24 # LED set up 25 led = DigitalInOut(board.TX) 26 led.direction = Direction.OUTPUT 27 28 while True: # Repeat forever... 29 # If the button is pressed turn on LED n00ds 30 if switch.value: 31 for i, pin in enumerate(pin_list): # For each pin... 32 # Calc sine wave, phase offset for each pin, with gamma correction. 33 # If using red, green, blue nOOds, you'll get a cycle of hues. 34 phase = (time.monotonic() - 2 * i / len(PINS)) * math.pi 35 brightness = int((math.sin(phase) + 1.0) * 0.5 ** GAMMA * 65535 + 0.5) 36 pin.duty_cycle = brightness 37 led.value = True # Turn button LED on 38 else: # Otherwise turn LED n00ds off 39 for i, pin in enumerate(pin_list): 40 pin.duty_cycle = 0 41 led.value = False # Turn button LED off 42 43 time.sleep(.01)