0

The following code works just fine:

# Add your Python code here. E.g.
from microbit import *

score = 0
display.show(str(score))

   while True:
    if accelerometer.was_gesture('face down'):
        score += 1
        if score < 10:
            display.show(score)
        else: 
            display.scroll(score)
    continue

'''But when I try to replace was_gesture('face down') with get_Z i get an error:'''

# Add your Python code here. E.g.

    from microbit import *

    score = 0
    display.show(str(score))

    z = accelerometer.get_z()

    while True:
        if z < accelerometer.get_z(-500) 
            score += 1
            if score < 10:
                display.show(score)
            else: 
                display.scroll(score)
        continue

I get an error? But why? I just want to get the microbit to count every time I move the device below a certain point?

Mikkel
  • 33
  • 2

2 Answers2

1

The accelerometer.get_z() statement needs to be inside the while loop so that it is updated. The loop also needs a sleep statement so that there is not a backlog of detections to display.

I tested the code below on a micro:bit using the mu editor. When the microbit is LED side up, the count increments. When it is face down, the count stops.

from microbit import *
uart.init(baudrate=115200)

score = 0
display.show(str(score))

while True:
    z = accelerometer.get_z()
    if z < -500:
        score += 1
        if score < 10:
            display.show(score)
        else: 
            display.scroll(score)
    sleep(1000)
    continue
Oppy
  • 2,662
  • 16
  • 22
  • It works perfectly as you just explained above - my issue though - is that i would like the device to count just 1 at the time, when the device is brought below a certain point eg. when i move the device towards the ground - at some point the -500 mark will be passed - and the count wil be 1. Then bring the Microbit back to its starting position, and repeat the maneuver and then the count should be 2? any ideas? – Mikkel Feb 11 '20 at 20:23
0

You've missed a colon at the end of this line:

       if z < accelerometer.get_z(-500) 

Also, the get_z() method doesn't take any arguments: https://microbit-micropython.readthedocs.io/en/latest/accelerometer.html#microbit.accelerometer.get_z

carlosperate
  • 594
  • 4
  • 11