microbit micropython help

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
kvarneb
Posts: 2
Joined: Mon Mar 13, 2017 2:16 pm

microbit micropython help

Post by kvarneb » Mon Mar 13, 2017 2:36 pm

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)

User avatar
deshipu
Posts: 1220
Joined: Thu May 28, 2015 5:54 pm

Re: microbit micropython help

Post by deshipu » Mon Mar 13, 2017 5:37 pm

What do you mean exactly by "not working"?

User avatar
pythoncoder
Posts: 1962
Joined: Fri Jul 18, 2014 8:01 am

Re: microbit micropython help

Post by pythoncoder » Sun Mar 19, 2017 4:49 pm

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?
Peter Hinch

User avatar
deshipu
Posts: 1220
Joined: Thu May 28, 2015 5:54 pm

Re: microbit micropython help

Post by deshipu » Sun Mar 19, 2017 9:40 pm

You can see the original indentation of the code by viewing the page source -- just a trick.

Post Reply