code.py
 1  # SPDX-FileCopyrightText: 2019 Dano Wall for Adafruit Industries
 2  # SPDX-FileCopyrightText: 2019 Anne Barela for Adafruit Industries
 3  #
 4  # SPDX-License-Identifier: MIT
 5  
 6  # Circuit Playground Express PiƱata by Dano Wall for Adafruit Industries
 7  # CircuitPython code by Anne Barela for Adafruit Industries, MIT License
 8  import time
 9  import random
10  import board
11  import pwmio
12  from adafruit_motor import servo
13  from adafruit_circuitplayground.express import cpx
14  
15  # create a PWMOut object on CPX Pin A1
16  pwm = pwmio.PWMOut(board.A1, frequency=50)
17  # Create a servo object cpx_servo
18  cpx_servo = servo.Servo(pwm)
19  
20  hits = 0
21  max_hits = random.randint(3, 10)
22  cpx.detect_taps = 1  # Detect single taps
23  cpx_servo.angle = 0
24  cpx.pixels.fill((0, 0, 0))  # All NeoPixels off
25  
26  while hits < max_hits:
27      if cpx.tapped:
28          print("Hit!")
29          hits += 1
30          cpx.pixels.fill((255, 255, 255))  # All White
31          cpx.play_file("hit.wav")
32          time.sleep(1.0)  # Wait time in seconds
33          cpx.pixels.fill((0, 0, 0))  # All off
34      time.sleep(0.1)
35  
36  # Hits Reached, Payout!
37  print("Release!")
38  cpx.pixels.fill((0, 255, 0))  # All green
39  cpx.play_file("candy.wav")
40  cpx_servo.angle = 180
41  print("Press Reset or power cycle to reset device")
42  while True:
43      pass     # Infinite loop, press Reset button to reset