Skip to content

Commit ecd572d

Browse files
authored
Added Exercise 7 Template
The last exercise in lab 4b is an unnecessary time sink. We provided a template that still makes them apply the averager function they made previously, know how to display things on the I2C, and have general knowledge on the pins. This template could be subject to change to make it harder on the students(right now it is a bit easy).
1 parent bc4d880 commit ecd572d

File tree

1 file changed

+73
-0
lines changed
  • book/labs/4_measure_tall_things/b_build_an_inclinometer

1 file changed

+73
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Imports
2+
import time
3+
import math
4+
from machine import Pin, I2C
5+
from imu import MPU6050
6+
from ssd1306 import SSD1306_I2C
7+
from averager import Averager
8+
9+
10+
def main():
11+
i2c = I2C(id=____, sda=Pin(_____), scl=Pin(____), freq=400_000) # Fill In Based on Pin Sheet
12+
led = Pin(25, Pin.OUT)
13+
button = Pin(____, Pin.IN, Pin.PULL_UP) # Fill In Based on Pin Sheet
14+
inclinometer = Inclinometer(i2c, button, led)
15+
while True:
16+
inclinometer.write()
17+
for i in range(50):
18+
inclinometer.read()
19+
20+
21+
class Inclinometer:
22+
def __init__(self, i2c, button, led, samples = 50):
23+
self.display = Display(i2c)
24+
self.accelerometer = MPU6050(i2c)
25+
self.button = button
26+
self.led = led
27+
# Define x, y, and z as Averager Objects(Look at Parameters For Sample Size!)
28+
self.x = ____
29+
self.y = ____
30+
self.z = ____
31+
32+
def read(self):
33+
x, y, z = self.accelerometer.accel.x, self.accelerometer.accel.y, self.accelerometer.accel.z
34+
# Add x, y, and z values into Averager Object
35+
____ # x
36+
____ # y
37+
____ # z
38+
39+
def write(self):
40+
if self.button.value():
41+
# Get x, y, and z values from Average Object
42+
x = ____
43+
y = ____
44+
z = ____
45+
self.display.update(x, y, z)
46+
else:
47+
self.display.hold()
48+
49+
50+
class Display(SSD1306_I2C):
51+
def __init__(self, i2c):
52+
super().__init__(128, 64, i2c)
53+
self.fill(0)
54+
self.text('loading...', 0, 0, 1)
55+
self.show()
56+
57+
def update(self, x, y, z):
58+
# Print Out x, y, and z values
59+
self.fill(0)
60+
self.text(____, 0, 10, 1)
61+
self.text(____, 0, 20, 1)
62+
self.text(____, 0, 30, 1)
63+
self.text("-", 0, 40, 1)
64+
self.show()
65+
66+
def hold(self):
67+
self.fill_rect(0, 0, 32, 9, 1)
68+
self.text("HOLD", 0, 1, 0)
69+
self.show()
70+
71+
72+
if __name__ == '__main__':
73+
main()

0 commit comments

Comments
 (0)