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