My Creature Code

The Robot Head

# This is the code for the robot head 
import time
import board
import pwmio
from adafruit_motor import servo
from adafruit_circuitplayground import cp
from adafruit_circuitplayground.express import cpx
 
# Create the PWMOut object on Pin A1 
pwm1 = pwmio.PWMOut(board.A1, duty_cycle = 2 ** 15, frequency = 50)
 
# Create the servo object using my_servo
my_servo1 = servo.Servo(pwm1) # For the robot head
 
pixels = cpx.pixels # the following neopixel codes I modify from the cpx neopixel example in Week 7 tutorial
 
# Set the brightness of the pixels to a floating point number between 0 and 1.0
pixels.brightness = 0.5 # I want the brightness to be 0.5
 
# For playing with the brightness
bright = 0
bright_max = 1
bright_delta = 0.2
 
# For playing with the colours
red = 0
green = 0
blue = 0
red_delta = 10
green_delta = 20
blue_delta = 30
 
# For playing with individual pixels
which_pixel = 0
 
# For double-tap detection from Adafruit tutorial
# https://learn.adafruit.com/circuitpython-made-easy-on-circuit-playground-express/tap
cp.detect_taps = 2


While True:
    if cp.shake(shake_threshold = 10) or cp.tapped:
        print("Shake or tap detected!")
        # The default threshold is 30.
        # Decreasing the threshold makes it easier to have a shake detected.
        # The minimum value allowed is 10,  so I set up for 10 because I don't have to shake the robot too hard.
        cp.red_led = True
        # Set up the sound effects using different frequency of tones
        # Modified from the cpx play 2 tones example in Week 7 tutorial
        cpx.play_tone(250, 1)
        cpx.play_tone(500, 1)
        cpx.play_tone(750, 1)
        cpx.play_tone(1000, 1)
        
        # Set up the robot head movement from Adafruit tutorial
        # https://learn.adafruit.com/using-servos-with-circuitpython/high-level-servo-control
        for angle in range(0, 60, 5): # 0 - 60 degrees, 5 degrees at a time.
            my_servo1.angle = angle
            time.sleep(0.5)
        for angle in range(120, 0, -5): # 120 - 0 degrees, 5 degrees at a time.
            my_servo1.angle = angle
            time.sleep(0.5)
        for angle in range(0, 60, 5): # 0 - 60 degrees, 5 degrees at a time.
            my_servo1.angle = angle
            time.sleep(0.5)
        cp.play_file("rimshot.wav") # sound effect at the end https://learn.adafruit.com/circuitpython-made-easy-on-circuit-playground-express/play-file
 
    else:
        cp.red_led = False
        # play with colours - cycle using different deltas
        red = (red + red_delta) % 256
        green = (green - green_delta) % 256
        blue = (blue + blue_delta) % 256
        # Determine which pixel to colour
        which_pixel = (which_pixel - 1) % 10  # cycling through the 10 pixels
        # Set the one pixel to the cycled through the colours
        pixels[which_pixel] = (red, green, blue)
        # To show the pixels
        pixels.show()
        # The time delay between the cycles of colouring the neopixels
        time.sleep(1)

 

The Robot Arm and Belly

# This is the code for the robot arms and bottom
import time
import board
import pwmio
from adafruit_motor import servo
from adafruit_circuitplayground import cp
from adafruit_circuitplayground.express import cpx
 
# Create the PWMOut object on Pin A2 
pwm2 = pwmio.PWMOut(board.A2, duty_cycle = 2 ** 15, frequency = 50)
 
# Create the servo object using my_servo
my_servo2 = servo.Servo(pwm2) # For the robot arms and bottom
 
pixels = cpx.pixels # the following neopixel codes I modify from the cpx neopixel example in Week 7 tutorial
 
# Set the brightness of the pixels to a floating point number between 0 and 1.0
pixels.brightness = 0.5 # I want the brightness to be 0.5
 
# For playing with the brightness
bright = 0
bright_max = 1
bright_delta = 0.2
 
# For playing with the colours
red = 0
green = 0
blue = 0
red_delta = 10
green_delta = 20
blue_delta = 30
 
# For playing with individual pixels
which_pixel = 0
 
# For double-tap detection from Adafruit tutorial
# https://learn.adafruit.com/circuitpython-made-easy-on-circuit-playground-express/tap
cp.detect_taps = 2
 
While True:
    if cp.shake(shake_threshold = 10) or cp.tapped:
        print("Shake or tap detected!")
        # The default threshold is 30.
        # Decreasing the threshold makes it easier to have a shake detected.
        # The minimum value allowed is 10,  so I set up for 10 because I don't have to shake the robot too hard.
        cp.red_led = True
        # Set up the sound effects using different frequency of tones
        # Modified from the cpx play 2 tones example in Week 7 tutorial
        cpx.play_tone(250, 1)
        cpx.play_tone(500, 1)
        cpx.play_tone(750, 1)
        cpx.play_tone(1000, 1)
        
        # Set up the robot arms and bottom movement from Adafruit tutorial
        # https://learn.adafruit.com/using-servos-with-circuitpython/high-level-servo-control
        for angle in range(0, 180, 180): # 0 - 180 degrees, 180 degrees at a time.
            my_servo2.angle = angle
            time.sleep(5)
        for angle in range(180, 0, -180): # 180 - 0 degrees, 180 degrees at a time.
            my_servo2.angle = angle
            time.sleep(1)
        cp.play_file("rimshot.wav“) # sound effect at the end https://learn.adafruit.com/circuitpython-made-easy-on-circuit-playground-express/play-file
 
    else:
        cp.red_led = False
        # play with colours - cycle using different deltas
        red = (red + red_delta) % 256
        green = (green - green_delta) % 256
        blue = (blue + blue_delta) % 256
        # Determine which pixel to colour
        which_pixel = (which_pixel - 1) % 10  # cycling through the 10 pixels
        # Set the one pixel to the cycled through the colours
        pixels[which_pixel] = (red, green, blue)
        # To show the pixels
        pixels.show()
        # The time delay between the cycles of colouring the neopixels
        time.sleep(1)