Format Blinky Eyes
This commit is contained in:
parent
d5829fdee4
commit
42fc4881f3
1 changed files with 19 additions and 18 deletions
|
|
@ -1,11 +1,12 @@
|
|||
# Name: Blinking Eyes - based on code by Brad Blumenthal, MAKE Magazine
|
||||
# License: GPLv3
|
||||
# 2013 - Adafruit Learning System - Modified for 8 MHz ATTiny85 and low light photocell
|
||||
# 2018 - Adafruit Learning System - Modified for Gemma M0 and Circuit Python
|
||||
"""
|
||||
Blinking Eyes - based on code by Brad Blumenthal, MAKE Magazine
|
||||
License: GPLv3
|
||||
"""
|
||||
|
||||
import board
|
||||
import analogio
|
||||
import time
|
||||
|
||||
import analogio
|
||||
import board
|
||||
import pulseio
|
||||
|
||||
try:
|
||||
|
|
@ -14,18 +15,18 @@ except ImportError:
|
|||
import random
|
||||
|
||||
# Initialize photocell
|
||||
photocell_pin = board.A1 # cds photocell connected to this ANALOG pin
|
||||
darkness_max = (2**16 / 2) # more dark than light > 32k out of 64k
|
||||
photocell_pin = board.A1 # cds photocell connected to this ANALOG pin
|
||||
darkness_max = (2 ** 16 / 2) # more dark than light > 32k out of 64k
|
||||
photocell = analogio.AnalogIn(photocell_pin)
|
||||
|
||||
# Initialize PWM
|
||||
# PWM (fading) - Both LEDs are connected on D0
|
||||
# PWM (fading) - Both LEDs are connected on D0
|
||||
# (PWM not avail on D1)
|
||||
pwm_leds = board.D0
|
||||
pwm = pulseio.PWMOut(pwm_leds, frequency=1000, duty_cycle=0)
|
||||
brightness = 0 # how bright the LED is
|
||||
fade_amount = 1285 # 2% steping of 2^16
|
||||
counter = 0 # counter to keep track of cycles
|
||||
brightness = 0 # how bright the LED is
|
||||
fade_amount = 1285 # 2% steping of 2^16
|
||||
counter = 0 # counter to keep track of cycles
|
||||
|
||||
# blink delay
|
||||
blink_delay = True
|
||||
|
|
@ -33,19 +34,19 @@ blink_freq_min = 3
|
|||
blink_freq_max = 6
|
||||
|
||||
# Loop forever...
|
||||
while True:
|
||||
while True:
|
||||
|
||||
# turn on LEDs if it is dark out
|
||||
if ( photocell.value < darkness_max ):
|
||||
if photocell.value < darkness_max:
|
||||
|
||||
# blink frequency and timer
|
||||
if ( blink_delay ):
|
||||
if blink_delay:
|
||||
blink_delay = False
|
||||
blink_timer_start = time.monotonic()
|
||||
blink_freq = random.randint(blink_freq_min, blink_freq_max)
|
||||
|
||||
# time to blink? Blink once every 3 - 6 seconds (random assingment)
|
||||
if ( ( time.monotonic() - blink_timer_start ) >= blink_freq ):
|
||||
if (time.monotonic() - blink_timer_start) >= blink_freq:
|
||||
blink_delay = True
|
||||
pwm.duty_cycle = 0
|
||||
time.sleep(.1)
|
||||
|
|
@ -57,10 +58,10 @@ while True:
|
|||
brightness = brightness + fade_amount
|
||||
|
||||
# reverse the direction of the fading at the ends of the fade:
|
||||
if ( brightness <= 0 ):
|
||||
if brightness <= 0:
|
||||
fade_amount = -fade_amount
|
||||
counter += 1
|
||||
elif ( brightness >= 65535 ):
|
||||
elif brightness >= 65535:
|
||||
fade_amount = -fade_amount
|
||||
counter += 1
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue