from microbit import *
import math

#プログラムの名前を表示
display.show('suijun')

while True:
    #加速度センサーを用いて,ピッチとロールを計算
    x = accelerometer.get_x() #x軸方向の加速度
    y = accelerometer.get_y() #y軸方向の加速度
    z = accelerometer.get_z() #z軸方向の加速度
    roll = math.atan2(x, -z)  #ロールの計算
    pitch = math.atan2(-y, math.sqrt(x*x + z*z))#ピッチの計算
    
    #水平なら○,さもなければ補正すべき方向の矢印,を表示
    if(-0.02 < roll and 0.02 > roll and -0.02 < pitch and 0.02 > pitch):
        display.show(Image.SQUARE)  #水平
    if(-0.02 >= roll):
        display.show(Image.ARROW_E) #右(東)
    if(0.02 <= roll):
        display.show(Image.ARROW_W) #左(西)
    if(-0.02 >= pitch):
        display.show(Image.ARROW_N) #上(北)
    if(0.02 <= pitch):
        display.show(Image.ARROW_S) #下(南)