Skip to content

Commit bfa8315

Browse files
authored
Add files via upload
1 parent d553e82 commit bfa8315

File tree

4 files changed

+164
-0
lines changed

4 files changed

+164
-0
lines changed
28.8 KB
Binary file not shown.
47.6 KB
Binary file not shown.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// SPDX-FileCopyrightText: 2018 Anne Barela for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: GPL-3.0-or-later
4+
5+
/*
6+
Name: Blinking Eyes - based on code by Brad Blumenthal, MAKE Magazine
7+
License: GPLv3
8+
Modified for 8 MHz ATTiny85 and low light photocell
9+
October 2013 for Adafruit Learning System
10+
*/
11+
12+
#define SENSITIVITY 550 // photocell sensitivity (changeable)
13+
#define CELL_PIN 1 // CdS Photocell voltage divider on
14+
// Trinket GPIO #2 (A1), Gemma D1/A1
15+
uint8_t eyes_open;
16+
volatile uint8_t blink_count;
17+
volatile uint8_t blink_flag;
18+
volatile uint8_t tick_flag;
19+
volatile uint8_t getting_brighter = 0;
20+
const uint8_t min_bright=16;
21+
const uint8_t max_bright=128;
22+
volatile uint8_t brightness;
23+
uint8_t lfsr; // Linear Feedback Shift Register
24+
const uint8_t min_blink = 64u; // don't blink more than once every 3 secs or so
25+
26+
void setup() {
27+
pinMode(0, OUTPUT); // Eyes set as output
28+
pinMode(2, INPUT); // Photocell as input
29+
analogWrite(0, max_bright); analogWrite(1, max_bright); // Light eyes
30+
eyes_open = 1;
31+
blink_flag = 0;
32+
lfsr = random(100); // initialize "blinking"
33+
blink_count = max(blink_count, min_blink);
34+
lfsr = (lfsr >> 1) ^ (-(lfsr & 1u) & 0xF0u); // pseudorandom blinking
35+
36+
// Timer1 set to CK/1024 ~10 (8) hZ at 8 MHz clock rate for blinking action
37+
TCCR1 |= _BV(CS13) | _BV(CS11) | _BV(CS10);
38+
TIMSK |= _BV(TOIE1); // Enable Timer/Counter1 Overflow Interrupt
39+
}
40+
41+
void loop() {
42+
uint16_t photocell;
43+
photocell = analogRead(CELL_PIN);
44+
if(photocell > SENSITIVITY) { // if too light, shut down eyes until it gets darker on photocell
45+
tick_flag=0;
46+
analogWrite(0,0); // Turn off eyes if too light out
47+
}
48+
if (tick_flag) { // if too bright or we've counted enough ticks (clocks for blink)
49+
tick_flag = 0;
50+
if (blink_flag) {
51+
blink_flag = 0;
52+
if (eyes_open) {
53+
eyes_open = 0;
54+
analogWrite(0,0); // Turn off eyes by stopping PWM
55+
blink_count = (lfsr & 0x01) + 1; // off for 1-2 ticks
56+
}
57+
else {
58+
eyes_open = 1;
59+
analogWrite(0,brightness); // Turn eyes on
60+
blink_count = max(blink_count, min_blink);
61+
lfsr = (lfsr >> 1) ^ (-(lfsr & 1u) & 0xF0u); // regenerate pseudorandom blink
62+
}
63+
}
64+
else { // One "tick," but we didn't blink... work on brightness control
65+
if (getting_brighter) {
66+
brightness += 2; // increase brightness
67+
analogWrite(0, brightness);
68+
if (brightness >= max_bright) getting_brighter = 0;
69+
} else {
70+
brightness -= 2; // decrease brightness
71+
analogWrite(0, brightness);
72+
if (brightness <= min_bright) getting_brighter = 1;
73+
}
74+
}
75+
}
76+
}
77+
78+
ISR (TIMER1_OVF_vect) { // Every 64 times a second, check blink
79+
noInterrupts();
80+
tick_flag = 1;
81+
blink_count--;
82+
if (!blink_count) {
83+
blink_flag = 1;
84+
}
85+
interrupts();
86+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# SPDX-FileCopyrightText: 2018 Mikey Sklar for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: GPL-3.0-or-later
4+
5+
"""
6+
Blinking Eyes - based on code by Brad Blumenthal, MAKE Magazine
7+
License: GPLv3
8+
"""
9+
10+
import time
11+
12+
import analogio
13+
import board
14+
import pwmio
15+
16+
try:
17+
import urandom as random # for v1.0 API support
18+
except ImportError:
19+
import random
20+
21+
# Initialize photocell
22+
photocell_pin = board.A1 # cds photocell connected to this ANALOG pin
23+
darkness_max = (2 ** 16 / 2) # more dark than light > 32k out of 64k
24+
photocell = analogio.AnalogIn(photocell_pin)
25+
26+
# Initialize PWM
27+
# PWM (fading) - Both LEDs are connected on D0
28+
# (PWM not avail on D1)
29+
pwm_leds = board.D0
30+
pwm = pwmio.PWMOut(pwm_leds, frequency=1000, duty_cycle=0)
31+
brightness = 0 # how bright the LED is
32+
fade_amount = 1285 # 2% steping of 2^16
33+
counter = 0 # counter to keep track of cycles
34+
35+
# blink delay
36+
blink_delay = True
37+
blink_freq_min = 3
38+
blink_freq_max = 6
39+
40+
# Loop forever...
41+
while True:
42+
43+
# turn on LEDs if it is dark out
44+
if photocell.value < darkness_max:
45+
46+
# blink frequency and timer
47+
if blink_delay:
48+
blink_delay = False
49+
blink_timer_start = time.monotonic()
50+
blink_freq = random.randint(blink_freq_min, blink_freq_max)
51+
52+
# time to blink? Blink once every 3 - 6 seconds (random assingment)
53+
if (time.monotonic() - blink_timer_start) >= blink_freq:
54+
blink_delay = True
55+
pwm.duty_cycle = 0
56+
time.sleep(.1)
57+
58+
# send to LED as PWM level
59+
pwm.duty_cycle = brightness
60+
61+
# change the brightness for next time through the loop:
62+
brightness = brightness + fade_amount
63+
64+
# reverse the direction of the fading at the ends of the fade:
65+
if brightness <= 0:
66+
fade_amount = -fade_amount
67+
counter += 1
68+
elif brightness >= 65535:
69+
fade_amount = -fade_amount
70+
counter += 1
71+
72+
# wait for 15 ms to see the dimming effect
73+
time.sleep(.015)
74+
75+
else:
76+
77+
# shutoff LEDs, it is too bright
78+
pwm.duty_cycle = 0

0 commit comments

Comments
 (0)