crwdns274640:0crwdne274640:0
crwdns274642:0crwdne274642:0
crwdns274644:0crwdne274644:0
crwdns274646:0crwdne274646:0
crwdns274648:0crwdne274648:0
crwdns274650:0crwdne274650:0
- crwdns274652:0crwdne274652:0
- crwdns274654:0crwdne274654:0
crwdns274656:0crwdne274656:0
- crwdns274658:0crwdne274658:0 crwdns274660:0crwdne274660:0
- crwdns274662:0crwdne274662:0
- crwdns274664:0crwdne274664:0
crwdns274666:0crwdne274666:0
- crwdns274668:0crwdne274668:0
- crwdns274670:0crwdne274670:0
- crwdns274672:0crwdne274672:0
crwdns274674:0crwdne274674:0
1from microbit import *
2
3# function to map any range of numbers to another range
4def map(value, fromMin, fromMax, toMin, toMax):
5 fromRange = fromMax - fromMin
6 toRange = toMax - toMin
7 valueScaled = float(value - fromMin) / float(fromRange)
8 return toMin + (valueScaled * toRange)
9
10# set of images for simple bar chart
11graph5 = Image("99999:"
12 "99999:"
13 "99999:"
14 "99999:"
15 "99999")
16
17graph4 = Image("00000:"
18 "99999:"
19 "99999:"
20 "99999:"
21 "99999")
22
23graph3 = Image("00000:"
24 "00000:"
25 "99999:"
26 "99999:"
27 "99999")
28
29graph2 = Image("00000:"
30 "00000:"
31 "00000:"
32 "99999:"
33 "99999")
34
35graph1 = Image("00000:"
36 "00000:"
37 "00000:"
38 "00000:"
39 "99999")
40
41graph0 = Image("00000:"
42 "00000:"
43 "00000:"
44 "00000:"
45 "00000")
46
47allGraphs = [graph0, graph1, graph2, graph3, graph4, graph5]
48
49# ignore first sound level reading
50soundLevel = microphone.sound_level()
51sleep(200)
52
53while True:
54 # map sound levels from range 0-255 to range 0-5 for choosing graph image
55 soundLevel = int(map(microphone.sound_level(), 0, 255, 0, 5))
56 display.show(allGraphs[soundLevel])
57
crwdns274676:0crwdne274676:0
- crwdns274678:0crwdne274678:0
- crwdns274680:0crwdne274680:0
This content is published under a Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) licence.