Merge pull request #153 from adafruit/Trinket_Gemma_Blinky_Eyes
ported to CircuitPython
This commit is contained in:
commit
5f680e5b0b
5 changed files with 159 additions and 0 deletions
BIN
Trinket_Gemma_Blinky_Eyes/Gemma_Blinky_Eyes.fzz
Normal file
BIN
Trinket_Gemma_Blinky_Eyes/Gemma_Blinky_Eyes.fzz
Normal file
Binary file not shown.
4
Trinket_Gemma_Blinky_Eyes/README.md
Normal file
4
Trinket_Gemma_Blinky_Eyes/README.md
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
# Trinket_Gemma_Blinky_Eyes
|
||||
|
||||
Code to accompany this Adafruit tutorial:
|
||||
https://learn.adafruit.com/trinket-gemma-blinky-eyes
|
||||
BIN
Trinket_Gemma_Blinky_Eyes/Trinket_Blinky_Eyes.fzz
Normal file
BIN
Trinket_Gemma_Blinky_Eyes/Trinket_Blinky_Eyes.fzz
Normal file
Binary file not shown.
82
Trinket_Gemma_Blinky_Eyes/Trinket_Gemma_Blinky_Eyes.ino
Normal file
82
Trinket_Gemma_Blinky_Eyes/Trinket_Gemma_Blinky_Eyes.ino
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
/*
|
||||
Name: Blinking Eyes - based on code by Brad Blumenthal, MAKE Magazine
|
||||
License: GPLv3
|
||||
Modified for 8 MHz ATTiny85 and low light photocell
|
||||
October 2013 for Adafruit Learning System
|
||||
*/
|
||||
|
||||
#define SENSITIVITY 550 // photocell sensitivity (changeable)
|
||||
#define CELL_PIN 1 // CdS Photocell voltage divider on
|
||||
// Trinket GPIO #2 (A1), Gemma D1/A1
|
||||
uint8_t eyes_open;
|
||||
volatile uint8_t blink_count;
|
||||
volatile uint8_t blink_flag;
|
||||
volatile uint8_t tick_flag;
|
||||
volatile uint8_t getting_brighter = 0;
|
||||
const uint8_t min_bright=16;
|
||||
const uint8_t max_bright=128;
|
||||
volatile uint8_t brightness;
|
||||
uint8_t lfsr; // Linear Feedback Shift Register
|
||||
const uint8_t min_blink = 64u; // don't blink more than once every 3 secs or so
|
||||
|
||||
void setup() {
|
||||
pinMode(0, OUTPUT); // Eyes set as output
|
||||
pinMode(2, INPUT); // Photocell as input
|
||||
analogWrite(0, max_bright); analogWrite(1, max_bright); // Light eyes
|
||||
eyes_open = 1;
|
||||
blink_flag = 0;
|
||||
lfsr = random(100); // initialize "blinking"
|
||||
blink_count = max(blink_count, min_blink);
|
||||
lfsr = (lfsr >> 1) ^ (-(lfsr & 1u) & 0xF0u); // pseudorandom blinking
|
||||
|
||||
// Timer1 set to CK/1024 ~10 (8) hZ at 8 MHz clock rate for blinking action
|
||||
TCCR1 |= _BV(CS13) | _BV(CS11) | _BV(CS10);
|
||||
TIMSK |= _BV(TOIE1); // Enable Timer/Counter1 Overflow Interrupt
|
||||
}
|
||||
|
||||
void loop() {
|
||||
uint16_t photocell;
|
||||
photocell = analogRead(CELL_PIN);
|
||||
if(photocell > SENSITIVITY) { // if too light, shut down eyes until it gets darker on photocell
|
||||
tick_flag=0;
|
||||
analogWrite(0,0); // Turn off eyes if too light out
|
||||
}
|
||||
if (tick_flag) { // if too bright or we've counted enough ticks (clocks for blink)
|
||||
tick_flag = 0;
|
||||
if (blink_flag) {
|
||||
blink_flag = 0;
|
||||
if (eyes_open) {
|
||||
eyes_open = 0;
|
||||
analogWrite(0,0); // Turn off eyes by stopping PWM
|
||||
blink_count = (lfsr & 0x01) + 1; // off for 1-2 ticks
|
||||
}
|
||||
else {
|
||||
eyes_open = 1;
|
||||
analogWrite(0,brightness); // Turn eyes on
|
||||
blink_count = max(blink_count, min_blink);
|
||||
lfsr = (lfsr >> 1) ^ (-(lfsr & 1u) & 0xF0u); // regenerate pseudorandom blink
|
||||
}
|
||||
}
|
||||
else { // One "tick," but we didn't blink... work on brightness control
|
||||
if (getting_brighter) {
|
||||
brightness += 2; // increase brightness
|
||||
analogWrite(0, brightness);
|
||||
if (brightness >= max_bright) getting_brighter = 0;
|
||||
} else {
|
||||
brightness -= 2; // decrease brightness
|
||||
analogWrite(0, brightness);
|
||||
if (brightness <= min_bright) getting_brighter = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ISR (TIMER1_OVF_vect) { // Every 64 times a second, check blink
|
||||
noInterrupts();
|
||||
tick_flag = 1;
|
||||
blink_count--;
|
||||
if (!blink_count) {
|
||||
blink_flag = 1;
|
||||
}
|
||||
interrupts();
|
||||
}
|
||||
73
Trinket_Gemma_Blinky_Eyes/Trinket_Gemma_Blinky_Eyes.py
Normal file
73
Trinket_Gemma_Blinky_Eyes/Trinket_Gemma_Blinky_Eyes.py
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
# 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
|
||||
|
||||
import board
|
||||
import analogio
|
||||
import time
|
||||
import pulseio
|
||||
|
||||
try:
|
||||
import urandom as random # for v1.0 API support
|
||||
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 = analogio.AnalogIn(photocell_pin)
|
||||
|
||||
# Initialize PWM
|
||||
# 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
|
||||
|
||||
# blink delay
|
||||
blink_delay = True
|
||||
blink_freq_min = 3
|
||||
blink_freq_max = 6
|
||||
|
||||
# Loop forever...
|
||||
while True:
|
||||
|
||||
# turn on LEDs if it is dark out
|
||||
if ( photocell.value < darkness_max ):
|
||||
|
||||
# blink frequency and timer
|
||||
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 ):
|
||||
blink_delay = True
|
||||
pwm.duty_cycle = 0
|
||||
time.sleep(.1)
|
||||
|
||||
# send to LED as PWM level
|
||||
pwm.duty_cycle = brightness
|
||||
|
||||
# change the brightness for next time through the loop:
|
||||
brightness = brightness + fade_amount
|
||||
|
||||
# reverse the direction of the fading at the ends of the fade:
|
||||
if ( brightness <= 0 ):
|
||||
fade_amount = -fade_amount
|
||||
counter += 1
|
||||
elif ( brightness >= 65535 ):
|
||||
fade_amount = -fade_amount
|
||||
counter += 1
|
||||
|
||||
# wait for 15 ms to see the dimming effect
|
||||
time.sleep(.015)
|
||||
|
||||
else:
|
||||
|
||||
# shutoff LEDs, it is too bright
|
||||
pwm.duty_cycle = 0
|
||||
Loading…
Reference in a new issue