# Write your code here :-)
import board, time, pwmio, neopixel, touchio, adafruit_lis3dh, busio, simpleio, math, digitalio
from adafruit_motor import servo

# Configure CPB Accelerometer - note you can change the RANGE if needed
i2c = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA)
int1 = digitalio.DigitalInOut(board.ACCELEROMETER_INTERRUPT)
accelerometer = adafruit_lis3dh.LIS3DH_I2C(i2c, address=0x19, int1=int1)
accelerometer.range = adafruit_lis3dh.RANGE_8_G

strip_pin = board.A1
strip_num_of_LEDs = 30
strip = neopixel.NeoPixel(strip_pin, strip_num_of_LEDs, brightness=0.5, auto_write=False)

# set up a servo on CPB pin A2
pwm_1 = pwmio.PWMOut(board.A3, frequency=50)
servo_1 = servo.Servo(pwm_1, max_pulse = 2500)

pwm_2 = pwmio.PWMOut(board.A2, frequency=50)
servo_2 = servo.Servo(pwm_2, max_pulse = 2500)

number_of_readings = 8
x_reading = [90] * number_of_readings
y_reading = [90] * number_of_readings

def average_readings(readings_lists):
    return sum(reading_lists) / len(reading_lists)

while True:
    x, y, z = accelerometer.acceleration

    x_axis = simpleio.map_range(x, -10, 10, 70,130)
    y_axis = simpleio.map_range(y, -10, 10, 70, 130)
    x_reading = x_reading[1:]
    y_reading = y_reading[1:]
    x_reading.append(x_axis)
    y_reading.append(y_axis)
    x_axis = average_readings(x_reading)
    y_axis = average_readings(y_reading)
    servo_1.angle = x_axis
    servo_2.angle = y_axis
    print((x_axis,y_axis))
    time.sleep(0.1)
