''' Mission 11 - Spirit Level - Remix 2B - Medium The remix challenge is to use both the x and y value for tilt. Lines will be vertical and horizontal. ''' from codex import * from time import sleep import math # center of display (240 x 240) CENTER = 120 # draw two lines on the screen # leave space in the middle for the circle display.fill(WHITE) display.draw_line(0, CENTER, 105, CENTER, BLACK) display.draw_line(135, CENTER, 239, CENTER, BLACK) display.draw_line(CENTER, 0, CENTER, 105, BLUE) display.draw_line(CENTER, 135, CENTER, 239, BLUE) # define a variable to use for the circle's position x = CENTER y = CENTER # start an infinite loop while True: # read from the accelerometer and get x value val = accel.read() tilt_x = val[0] tilt_y = val[1] # change tilt_x reading to degrees scaled_x = (tilt_x / 16384) scaled_x = min(max(scaled_x, -1), 1) degrees_x = math.asin(scaled_x) * 180 / math.pi degrees_x = int(degrees_x) # change tilt_y reading to degrees scaled_y = (tilt_y / 16384) scaled_y = min(max(scaled_y, -1), 1) degrees_y = math.asin(scaled_y) * 180 / math.pi degrees_y = int(degrees_y) # erase current circle, recalculate, and draw new circle display.draw_circle(x, y, 15, WHITE) x = CENTER + degrees_x y = CENTER + degrees_y display.draw_circle(x, y, 15, ORANGE) sleep(0.5)