#Sara Falkson: PiPico Acceleromter and LED 

from machine import Pin, I2C
from time import sleep

# Constants and MPU6050 setup
MPU6050_ADDR = 0x68
PWR_MGMT_1 = 0x6B
TEMP_OUT_H = 0x41
ACCEL_XOUT_H = 0x3B
GYRO_XOUT_H = 0x43

# Initialize I2C for MPU6050
i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq=400000)  # Setup I2C on GPIO 8 and 9

# Initialize external LED and button
led = Pin(15, Pin.OUT)
button = Pin(16, Pin.IN)

# Helper functions for MPU6050
def write_byte(addr, reg, data):
    i2c.writeto_mem(addr, reg, bytearray([data]))

def read_word(addr, reg):
    data = i2c.readfrom_mem(addr, reg, 2)
    value = (data[0] << 8) | data[1]
    return value if value < 0x8000 else value - 0x10000

def init_mpu6050():
    write_byte(MPU6050_ADDR, PWR_MGMT_1, 0)  # Wake up the MPU6050

def read_accel():
    return (read_word(MPU6050_ADDR, ACCEL_XOUT_H),
            read_word(MPU6050_ADDR, ACCEL_XOUT_H + 2),
            read_word(MPU6050_ADDR, ACCEL_XOUT_H + 4))

def read_gyro():
    return (read_word(MPU6050_ADDR, GYRO_XOUT_H),
            read_word(MPU6050_ADDR, GYRO_XOUT_H + 2),
            read_word(MPU6050_ADDR, GYRO_XOUT_H + 4))

def read_temp():
    temp_raw = read_word(MPU6050_ADDR, TEMP_OUT_H)
    return (temp_raw / 340.0) + 36.53

# Initialize MPU6050
init_mpu6050()

# Main loop
while True:
    # Read sensor data
    accel_x, accel_y, accel_z = read_accel()
    gyro_x, gyro_y, gyro_z = read_gyro()
    temp_celsius = read_temp()
    print(f"Accelerometer X: {accel_x} Y: {accel_y} Z: {accel_z}")
    print(f"Gyroscope X: {gyro_x} Y: {gyro_y} Z: {gyro_z}")
    print(f"Temperature: {temp_celsius:.2f} C")

    # LED control based on button press
    if button.value() == 1:
        led.on()
    else:
        led.off()

    sleep(1) # Short sleep to reduce CPU usage without losing much responsiveness
$abcdeabcde151015202530fghijfghij
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT
pico:GP0
pico:GP1
pico:GND.1
pico:GP2
pico:GP3
pico:GP4
pico:GP5
pico:GND.2
pico:GP6
pico:GP7
pico:GP8
pico:GP9
pico:GND.3
pico:GP10
pico:GP11
pico:GP12
pico:GP13
pico:GND.4
pico:GP14
pico:GP15
pico:GP16
pico:GP17
pico:GND.5
pico:GP18
pico:GP19
pico:GP20
pico:GP21
pico:GND.6
pico:GP22
pico:RUN
pico:GP26
pico:GP27
pico:GND.7
pico:GP28
pico:ADC_VREF
pico:3V3
pico:3V3_EN
pico:GND.8
pico:VSYS
pico:VBUS
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
led1:A
led1:C
r1:1
r1:2
r2:1
r2:2
imu1:INT
imu1:AD0
imu1:XCL
imu1:XDA
imu1:SDA
imu1:SCL
imu1:GND
imu1:VCC