Project Files - Circuitpython
Project Files - Circuitpython
import board
import digitalio
import time
led = digitalio.DigitalInOut(board.D13)
led.direction = digitalio.Direction.OUTPUT
while True:
led.value = True
time.sleep(0.5)
led.value = False
time.sleep(0.5)
import math
import time
# Accurate to 5 digits
f1 = 1.111111
f2 = 1.111111
print(f1 + f2)
# Cast with int, float, str
print("Cast ", int(f1))
# Math Operators
print("5 + 2 =", 5 + 2)
# Formatted print with 2 decimals or less
print("5 - 2 = {:.2f}".format(5 - 2))
print("5 * 2 =", 5 * 2)
print("5 / 2 =", 5 / 2)
print("5 % 2 =", 5 % 2)
print("5 ** 2 =", 5 ** 2)
print("5 // 2 =", 5 // 2)
import time
import board
from analogio import AnalogIn
import neopixel
def update_pixels():
# If number of pixels to light changed clear
# and redraw with new value
if px_prev_num is not px_cur_num:
pixels.fill(0)
# Cycles through and lights pixels as voltage changes
for i in range(num_pixels):
pixels[i] = (255, 0, 0)
while True:
# Readings range from 0 to 65535
a_val = analog_in.value
# Convert to a range from 0 to 3.3V
print((a_val * 3.3) / 65536)
# Draw a label
# Set size and scale it by 2 placing it at x/y position
text_group = displayio.Group(max_size=10, scale=2, x=10, y=10)
# Text to display
text = temp_str + sound_str + light_str + tilt_str
# Use the terminalio font in yellow
text_area = label.Label(terminalio.FONT, text=text, color=0xFFFF00)
# Append to a subgroup
text_group.append(text_area)
# Add text to screen
splash.append(text_group)
# Infinite loop that updates data and display
while True:
# Sleep for 1 second and then update data and screen
time.sleep(1)
temp_f = cp.temperature * 9 / 5 + 32
temp_str = "Temp: " + str(temp_f) + "\n"
sound_str = "Sound: " + str(cp.sound_level) + "\n"
light_str = "Light: " + str(cp.light) + "\n"
x, y, z = cp.acceleration
tilt_str = "X: " + str(x) + "\nY: " + str(y) + "\nZ: " + str(z)
NOTE_C4 = 261.63
NOTE_D4 = 293.66
NOTE_E4 = 329.63
# List of Mary Lil Lamb Notes
l1 = [NOTE_E4, NOTE_D4, NOTE_C4, NOTE_D4, NOTE_E4, NOTE_E4, NOTE_E4]
while True:
# Mary Lil Lamb : E D C D E E E
# D D D E G G E D C D E E E E D D E D C
if cp.tapped:
print("Tap")
# Frequency for middle C4 with 1s duration
cp.play_tone(261.63, .5)
# D4
if cp.button_a:
cp.play_tone(293.66, .5)
# E4
if cp.button_b:
cp.play_tone(329.63, .5)
# G4
if cp.touch_A1:
cp.play_tone(392.00, .5)
if cp.touch_A2:
for i in range(len(l1)):
cp.play_tone(l1[i], .5)
while True:
# Read pulses until they stop
pulses = decoder.read_pulses(pulsein)
try:
# Decodes the pulses into bits
received_code = decoder.decode_bits(pulses)
print("Decoded : ", received_code)
except adafruit_irremote.IRNECRepeatException:
print("Received Strange Code")
continue
except adafruit_irremote.IRDecodeException as e:
print("Failed to decode")
continue
import time
from adafruit_crickit import crickit
print("Test Servo")
crickit.servo_1.angle = 15
time.sleep(1)
crickit.servo_1.angle = 90
time.sleep(1)
crickit.servo_1.angle = 165
time.sleep(1)
while True:
print("Repeat")
crickit.servo_1.angle = 15 # right
time.sleep(1)
crickit.servo_1.angle = 90 # middle
time.sleep(1)
crickit.servo_1.angle = 165 # left
time.sleep(1)
crickit.servo_1.angle = 90 # middle
time.sleep(1)
# and repeat!
import time
import board
import pwmio
from adafruit_motor import servo
# Creates a PWMOut object and assigns pin A3 to it
# The duty cycle is the percent in which the pulse is high
# Frequency in hertz
pwm = pwmio.PWMOut(board.A3, duty_cycle=2 ** 15, frequency=50)
while True:
for angle in range(15, 165, 5): # 15 - 165 degrees, 5 degrees at a time.
my_servo.angle = angle
time.sleep(0.05)
for angle in range(165, 15, -5): # 165 - 15 degrees, 5 degrees at a time.
my_servo.angle = angle
time.sleep(0.05)
import time
import board
import adafruit_hcsr04
# For the library to work you just define where the trigger and echo pins
# are connected
# A2 is D9 and A3 is D10
sonar = adafruit_hcsr04.HCSR04(trigger_pin=board.D9, echo_pin=board.D10)
while True:
try:
# Returns distance in cms
print(sonar.distance)
except RuntimeError:
print("Retrying!")
time.sleep(0.1)
import time
import board
import adafruit_hcsr04
# Needed for the Crickit
from adafruit_crickit import crickit
# For the library to work you just define where the trigger and echo pins
# are connected
# A2 is D9 and A3 is D10
sonar = adafruit_hcsr04.HCSR04(trigger_pin=board.D9, echo_pin=board.D10)
while True:
try:
# Go forward 100% with both tires
m_1.throttle = -1
m_2.throttle = 1
# Returns the distance in cms
s_dist = sonar.distance
if s_dist <= 20:
m_1.throttle = 1
m_2.throttle = 0
print(s_dist)
time.sleep(1.5)
else:
m_1.throttle = -1
m_2.throttle = 1
except RuntimeError:
print("Error")
is_off = True
m_1 = crickit.dc_motor_1
m_2 = crickit.dc_motor_2
pow_b = 162
left_b = 48
forward_b = 24
right_b = 122
back_b = 56
# Read IR input from pin D2, record up to 120 pulses and turn idle state on
pulsein = pulseio.PulseIn(board.IR_RX, maxlen=120, idle_state=True)
# Decodes infrared signals
decoder = adafruit_irremote.GenericDecode()
while True:
# Read pulses until they stop
pulses = decoder.read_pulses(pulsein)
# Output number of pulses and their values in a list
print("Heard", len(pulses), "Pulses:", pulses)
try:
# Decodes the pulses into bits
code = decoder.decode_bits(pulses)
print("Decoded:", code)
if (code[3] == pow_b) and is_off:
print("On")
is_off = False
m_1.throttle = -1
m_2.throttle = 1
continue
if (code[3] == pow_b) and not is_off:
print("Off")
is_off = True
m_1.throttle = 0
m_2.throttle = 0
continue
if (code[3] == left_b) and not is_off:
print("Left")
is_off = False
m_1.throttle = -1
m_2.throttle = 0
continue
if (code[3] == right_b) and not is_off:
print("Right")
is_off = False
m_1.throttle = 0
m_2.throttle = 1
continue
if (code[3] == forward_b) and not is_off:
print("Forward")
is_off = False
m_1.throttle = -1
m_2.throttle = 1
continue
if (code[3] == back_b) and not is_off:
print("Backward")
is_off = False
m_1.throttle = 1
m_2.throttle = -1
continue
except adafruit_irremote.IRNECRepeatException: # unusual short code!
print("NEC repeat!")
except adafruit_irremote.IRDecodeException as e: # failed to decode
print("Failed to decode: ", e.args)