Micro bit block cod...
 

Subscribe now and choose from over 30 free gifts worth up to £49 - Plus get £25 to spend in our shop

[Closed] Micro bit block coding problem.

8 Posts
3 Users
0 Reactions
188 Views
Posts: 6253
Full Member
Topic starter
 

I'm trying to design an impact force device for Yr 7 pupils in the school I work in. I have loads of micro bits.
My code is designed to record an impact >3G and then display this on the LED array of the microbit. I can't do javascript but can give the block editor a good try. Converted to javascript my code looks like this:

input.onGesture(Gesture.ThreeG, function () {
basic.showNumber(input.acceleration(Dimension.Strength))
})

In other words, at a 3G impact the size of the impact is displayed.

BUT... the display scrolls the result through once and once only, so the pupils will not really get much of a chance of seeing/ recording the value if the object rolls away/ lands in an odd position. What I need is a way to keep the impact value scrolling around repeatedly. Any suggestions?


 
Posted : 12/02/2020 10:02 pm
 poly
Posts: 8747
Free Member
 

You probably want it in some sort of loop, or at least to add a delay. You could manually do that in Javascript or using the block editor. JS is not my day-to-day but micro:bit supports python. I don't have one to try... but can probably get you 1/2 way there. i'll go look at the documentation...


 
Posted : 12/02/2020 10:25 pm
Posts: 6253
Full Member
Topic starter
 

Thanks poly. I thought about a loop but I'm out of my comfort zone.


 
Posted : 12/02/2020 10:42 pm
 poly
Posts: 8747
Free Member
 

Here is a crude suggested approaches:


from microbit import *

while True:
    gesture = accelerometer.current_gesture()
    if gesture == "3g":
        display.show(accelerometer.get_z())
        sleep(1000) # will wait 1s (1000ms) before moving on
    else:
        display.show("-")

Note I've assumed you wanted the vertical (z axis) acceleration displayed (swap z for x or y as appropriate - if you don't care you could use:


display.show(max(accelerometer.get_x(),accelerometer.get_y(),accelerometer.get_z()))

and it will show the biggest in any of three directions.

An alternative would be something like:


from microbit import *

while True:
    gesture = accelerometer.current_gesture()
    if gesture == "3g":
        value = max(accelerometer.get_values())
        display.show(value)
    elif gesture == "shake":
        display.show("-")

Which I *think* should display - then when it gets a 3g geture display the largest accelerometer value and leave it on the display until you shake the device to reset it.


 
Posted : 12/02/2020 10:44 pm
Posts: 6253
Full Member
Topic starter
 

Oh you star, I'll give it a go in the morning 🙂


 
Posted : 12/02/2020 10:46 pm
Posts: 7184
Full Member
 

https://makecode.microbit.org/reference/basic/show-number

that help?

or, what poly did whilst is was looking up the syntax. Pffft


 
Posted : 12/02/2020 10:47 pm
 poly
Posts: 8747
Free Member
 

@Ambrose I've edited the shake bit - because what I wrote was dodgy! I also found a "get values" which gives x, y and z with one command.

Have fun.


 
Posted : 12/02/2020 10:57 pm
 poly
Posts: 8747
Free Member
 

If you really want to do it in javascript or the block editor then jim's link will let you translate what I wrote in python (that may be useful if I have any syntax errors there!).

while True: is the equivalent of the "forever" block
sleep is the equivalent of pause block

Also - i've used "show" - if its more than 1 character you probably want "scroll". I'm not sure if it scrolls the same thing continuously until stopped or it scrolls it once. if the latter - you'll need to wrap the scroll in another loop to keep it doing that. Potentially that might be something like:


from microbit import *

while True:
    gesture = accelerometer.current_gesture()
    if gesture == "3g":
        value = max(accelerometer.get_values())
        while accelerometer.current_gesture() != "shake": 
            display.scroll(value)
            sleep(100) # 0.1s pause to make the end more obvious

 
Posted : 12/02/2020 11:23 pm
Posts: 6253
Full Member
Topic starter
 

Cheers Both. I'll admit to getting lazy on this, I just gave the challenge to one of my Yr 10 pupils.

He came up with this:

let _1 = false
let number = 0
input.onButtonPressed(Button.A, function () {
_1 = true
})
input.onButtonPressed(Button.B, function () {
basic.showString("" + number)
})
input.onGesture(Gesture.ThreeG, function () {
if (_1 == true) {
number = input.acceleration(Dimension.Strength)
basic.showString("" + number)
}
basic.pause(100)
_1 = false
})

It works, I'm happy. So is he- he got a load of praise and appreciation.


 
Posted : 14/02/2020 12:00 am