/ Head_Tilt_Ears / code.py
code.py
1 # SPDX-FileCopyrightText: 2018 Dave Astels for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 """ 6 Circuit Playground Express head-tilt activated ears. 7 8 Adafruit invests time and resources providing this open source code. 9 Please support Adafruit and open source hardware by purchasing 10 products from Adafruit! 11 12 Written by Dave Astels for Adafruit Industries 13 Copyright (c) 2018 Adafruit Industries 14 Licensed under the MIT license. 15 16 All text above must be included in any redistribution. 17 """ 18 19 import time 20 import busio 21 import board 22 import adafruit_lis3dh 23 import pwmio 24 from adafruit_motor import servo 25 26 27 # Setup accelerometer 28 i2c = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA) 29 sensor = adafruit_lis3dh.LIS3DH_I2C(i2c, address=0x19) 30 31 # Setup servos 32 left_pwm = pwmio.PWMOut(board.A1, frequency=50) 33 right_pwm = pwmio.PWMOut(board.A2, frequency=50) 34 35 left_ear = servo.Servo(left_pwm) 36 right_ear = servo.Servo(right_pwm) 37 38 #initialize things 39 left_ear.angle = 0 40 right_ear.angle = 0 41 42 while True: 43 x, _, _ = sensor.acceleration 44 if x < -5.0: 45 left_ear.angle = 90 46 elif x > 5.0: 47 right_ear.angle = 90 48 elif abs(x) < 4.0: 49 left_ear.angle = 0 50 right_ear.angle = 0 51 time.sleep(0.1)