Skip to content

Commit 0816fb5

Browse files
authored
Merge pull request #2308 from adafruit/batty_bat
Adding code for CPX bat
2 parents 0e74c01 + 81cf805 commit 0816fb5

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

CPX_Halloween_Bat/code.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import time
6+
import board
7+
import pwmio
8+
import neopixel
9+
from adafruit_motor import servo
10+
from adafruit_led_animation.animation.comet import Comet
11+
from adafruit_led_animation.sequence import AnimationSequence
12+
from adafruit_led_animation.color import PURPLE
13+
14+
# create 2 PWM instances for the servos
15+
left_pwm = pwmio.PWMOut(board.A3, duty_cycle=2 ** 15, frequency=50)
16+
right_pwm = pwmio.PWMOut(board.A6, duty_cycle=2 ** 15, frequency=50)
17+
18+
# left wing servo
19+
left_servo = servo.Servo(left_pwm)
20+
# right wing servo
21+
right_servo = servo.Servo(right_pwm)
22+
23+
# use onboard neopixels on CPX
24+
pixel_pin = board.NEOPIXEL
25+
# number of onboard neopixels
26+
num_pixels = 10
27+
28+
# neopixels object
29+
pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.05, auto_write=False)
30+
31+
# comet animation
32+
comet = Comet(pixels, speed=0.01, color=PURPLE, tail_length=10, bounce=True)
33+
34+
# create animation sequence
35+
animations = AnimationSequence(comet)
36+
37+
# beginning angles for each wing
38+
left_angle = 100
39+
right_angle = 30
40+
41+
while True:
42+
# run comet animation while servos move
43+
animations.animate()
44+
45+
# left angle decreases by 10
46+
left_angle = left_angle - 10
47+
# once it's less than 30 degrees, reset to 100
48+
if left_angle < 30:
49+
left_angle = 100
50+
# right angle increases by 10
51+
right_angle = right_angle + 10
52+
# once it's greater than 100, reset to 30
53+
if right_angle > 100:
54+
right_angle = 30
55+
# move left wing
56+
left_servo.angle = left_angle
57+
# move right wing
58+
right_servo.angle = right_angle
59+
# delay
60+
time.sleep(0.05)

0 commit comments

Comments
 (0)