Page 1 of 1

microbit micropython help

Posted: Mon Mar 13, 2017 2:36 pm
by kvarneb
Hi

I am currently trying to use this code in order to create different sounds when the microbit is tilted in a different direction. However, it isnt working...can anyone help?

from micro bit import *
import music
on = False
initial = 1000
while True:
if button_a.is_pressed():
on = True
while on:
if accelerometer.get_y() >10:
initial +=100
music.pitch(initial,100)
elif accelerometer.get_y() <10:
initial -=100
music.pitch(initial,100)
else:
music.pitch(initial,100)

if initial >4000:
initial = 4000
if initial < 100:
initial = 100

if button_b.is_pressed():
on = False
sleep(100)

Re: microbit micropython help

Posted: Mon Mar 13, 2017 5:37 pm
by deshipu
What do you mean exactly by "not working"?

Re: microbit micropython help

Posted: Sun Mar 19, 2017 4:49 pm
by pythoncoder
The first line looks wrong to me. Should microbit be one word?
I'm guessing the indentation should look like this:

Code: Select all

from microbit import *
import music
on = False
initial = 1000
while True:
    if button_a.is_pressed():
        on = True
    while on:
        if accelerometer.get_y() >10:
            initial +=100
            music.pitch(initial,100)
        elif accelerometer.get_y() <10:
            initial -=100
            music.pitch(initial,100)
        else:
            music.pitch(initial,100)

        if initial >4000:
            initial = 4000
        if initial < 100:
            initial = 100

        if button_b.is_pressed():
            on = False
        sleep(100)

I'm unfamiliar with the micro:bit but are you sure the units of sleep() are milliseconds rather than seconds? Does your indentation match mine? Do you get an error message and if so what is it?

Re: microbit micropython help

Posted: Sun Mar 19, 2017 9:40 pm
by deshipu
You can see the original indentation of the code by viewing the page source -- just a trick.