Help with assignment

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Tom.mannix29
Posts: 2
Joined: Mon Nov 07, 2016 7:34 am

Help with assignment

Post by Tom.mannix29 » Mon Nov 07, 2016 7:38 am

I am making an assignment using micropython, i am making a radar gun that uses the accelerometer on one microbit and sends the speed to another microbit using the radio function. Could someone please tell me why this code is not working like i want it to.
from microbit import *
import radio
radio.on()
radio.config(group=23)
while True:
x = accelerometer.get_values()
if x > 1000:
if x <= 100:
display.scroll('10km/hr')
sleep(5000)
elif x <= 200:
display.scroll('20km/hr')
sleep(5000)
elif x <= 300:
display.scroll('30km/hr')
sleep(5000)
elif x <= 400:
display.scroll('40km/hr')
sleep(5000)
elif x <= 500:
display.scroll('50km/hr')
sleep(5000)
elif x <= 600:
display.scroll('60km/hr')
sleep(5000)
elif x <= 700:
display.scroll('70km/hr')
sleep(5000)
elif x <= 800:
display.scroll('80km/hr')
sleep(5000)
elif x <= 900:
display.scroll('90km/hr')
sleep(5000)
elif x > 1000:
display.scroll('max')
sleep(5000)
else:
display.scroll("under")

User avatar
dhylands
Posts: 3506
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Help with assignment

Post by dhylands » Mon Nov 07, 2016 5:59 pm

Without indentation, it's hard to tell exactly what your code is.

But these two lines look suspicious to me:

Code: Select all

if x > 1000: 
if x <= 100: 
If the second if is inside the first if (this is the only way I can see that this code would be valid python), then the second if will never be true.

User avatar
pythoncoder
Posts: 4793
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Help with assignment

Post by pythoncoder » Tue Nov 08, 2016 8:06 am

I'm puzzled :? The accelerometer produces acceleration values. To derive velocity from acceleration you surely need to perform an integration.
Peter Hinch

Lysenko
Posts: 62
Joined: Wed Aug 17, 2016 1:21 pm

Re: Help with assignment

Post by Lysenko » Tue Nov 08, 2016 8:57 am

pythoncoder wrote:I'm puzzled :? The accelerometer produces acceleration values. To derive velocity from acceleration you surely need to perform an integration.
It's not an instant value either. x = 1024 = 1g ~= 35kph/s

This smells very much like a case of "not even wrong".

User avatar
pythoncoder
Posts: 4793
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Help with assignment

Post by pythoncoder » Wed Nov 09, 2016 6:45 am

Lysenko wrote:...This smells very much like a case of "not even wrong".
Quite ;) I could have written a screed on the actual requirements and the probability of achieving any semblance of accuracy, but the first step is to get the dimensional analysis looking plausible :(
Peter Hinch

rhubarbdog
Posts: 166
Joined: Tue Nov 07, 2017 11:45 pm

Re: Help with assignment

Post by rhubarbdog » Wed Nov 08, 2017 12:17 am

Bug 1: x=accelerometer.get_values()
Should read x=accelerometer.get_x()

Bug 2:aswell as display.scroll(...) you need to use radio.send("message") also a consumer micro:bit will need message =radio.receive()

Are [code] [/code] tags not valid on this BB

Post Reply