''' Mission 11 - Spirit Level The program uses the accelerometer to determine if CodeX is level, using only the x (vertical) measurement. Concepts learned: reading from the accelerometer, using one value from the tuple, filling the display in a different color, drawing lines and drawing circles ''' 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(CENTER, 0, CENTER, 105, BLACK) display.draw_line(CENTER, 135, CENTER, 239, BLACK) # define a variable to use for the circle's position x = CENTER # start an infinite loop while True: # read from the accelerometer and get x value val = accel.read() tilt_x = val[0] # change accelerometer reading to degrees scaled = (tilt_x / 16384) scaled = min(max(scaled, -1), 1) degrees = math.asin(scaled) * 180 / math.pi degrees = int(degrees) # erase current circle, recalculate, and draw new circle display.draw_circle(x, CENTER, 15, WHITE) x = CENTER + degrees display.draw_circle(x, CENTER, 15, ORANGE) sleep(0.5)