Skip to content

Commit f134b8c

Browse files
committed
New examples: 2D bubble level and 'how I feel today'
1 parent 0082b6e commit f134b8c

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

examples/bubble_level_2d.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
Two-dimensional bubble level which uses the accelerometer.
3+
"""
4+
5+
from microbit import *
6+
7+
sensitivity = 'medium' # Change to 'low', 'medium', or 'high' to adjust
8+
divisors = {'low':64, 'medium':32, 'high':16}
9+
10+
def clamp(number, min, max):
11+
"""Returns number limited to range specified by min and max, inclusive"""
12+
if number < min:
13+
return min
14+
elif number > max:
15+
return max
16+
else:
17+
return number
18+
19+
while True:
20+
x_grav, y_grav, _ = accelerometer.get_values()
21+
# Map raw values from accelerometer to pixels on display
22+
x_pixel = 4 - clamp(2 + x_grav // divisors.get(sensitivity), 0, 4)
23+
y_pixel = 4 - clamp(2 + y_grav // divisors.get(sensitivity), 0, 4)
24+
display.clear()
25+
display.set_pixel(x_pixel, y_pixel, 9)
26+
sleep(100)

examples/i_feel_today.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""
2+
Program that shows different emotions.
3+
Push button "A" to become sadder and "B" to become happier.
4+
"""
5+
6+
from microbit import *
7+
8+
horror = Image("09090:00000:09990:90009:99999")
9+
better_meh = Image("00000:09090:00000:99999:00000")
10+
joy = Image("09090:00000:99999:90009:09990")
11+
12+
emotions = [horror, Image.SAD, better_meh, Image.HAPPY, joy]
13+
current_emotion = 2
14+
15+
while True:
16+
if button_a.get_presses():
17+
current_emotion = max(current_emotion - 1, 0)
18+
elif button_b.get_presses():
19+
current_emotion = min(current_emotion + 1, 4)
20+
display.show(emotions[current_emotion])

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy