from machine import Pin, ADC
import utime
import math

xAxis = ADC(Pin(27))
yAxis = ADC(Pin(26))
button = Pin(17,Pin.IN, Pin.PULL_UP)


rad2deg=(180/3.141592650)

flipped=-1

led1=1
led2=2
led15=15
led16=16
led18=18

ledUp=0
ledRight=0
ledDown=0
ledLeft=0

def setLed(ledNum,onOff) :
    p0 = Pin(ledNum, Pin.OUT)
    p0.value(onOff)


def ledsOff() :
    global led1,led2,led15,led16
    global ledUp,ledRight,ledDown,ledLeft
    ledUp=0
    ledRight=0
    ledDown=0
    ledLeft=0
    setLed(led1,0)
    setLed(led2,0)
    setLed(led15,0)
    setLed(led16,0)
    
while True:
    xRead = xAxis.read_u16()
    utime.sleep(0.01)
    yRead = yAxis.read_u16()
    utime.sleep(0.01)
    buttonValue = 1-button.value()
    xCalc=(100.0-round(xRead/327.67))*flipped
    yCalc=100.0-round(yRead/327.67)
    deg=math.atan2(yCalc,xCalc)*rad2deg
    deg=(450.0+deg)%360
    mag=(xCalc**2)+(yCalc**2)
    mag=math.sqrt(mag)
    setLed(led18,0)
    if abs(mag)<=27:
        setLed(led18,1)
        ledsOff()
    if abs(mag)>27 :
        setLed(led18,0)
        ledsOff()
        if mag>30 :
            if(deg<=1) :
                ledsOff()
                ledUp=1
            if (deg>0 and deg<180) :
                ledRight=1
            if (deg>180 and deg<=360) :
                ledLeft=1
            if (deg>0 and deg<90) :
                ledUp=1
            if (deg>270 and deg<=360) :
                ledUp=1
            if (deg>90 and deg<=270) :
                ledDown=1
        setLed(led1,ledUp)
        setLed(led2,ledRight)
        setLed(led15,ledDown)
        setLed(led16,ledLeft)
    print(xCalc,yCalc,deg,mag,buttonValue)
    utime.sleep(0.5)
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT