Viewing 9 posts - 1 through 9 (of 9 total)
  • Micro bit block coding problem.
  • Premier Icon Ambrose
    Subscriber

    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?

    poly
    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…

    Premier Icon Ambrose
    Subscriber

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

    poly
    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.

    Premier Icon Ambrose
    Subscriber

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

    Premier Icon jimdubleyou
    Subscriber

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

    that help?

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

    poly
    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.

    poly
    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
    
    Premier Icon Ambrose
    Subscriber

    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.

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.