I am using the ultimate 2.0 to make a rumba like bot. the problem Im running into is with the onRead function that is called by the ultrasonicRead function. the function will not allow return variables or global variables to use the distance in the rest of the code.
here is a simple test code to use global variables :
from megapi import *
import time
x = 0
dist = 0
def onRead(v):
print(“distance:”+str(v)+“cm”)
global dist
dist = v
def square(num):
global x
x = 3
return num*num
if name== “main”:
bot=MegaPi()
bot.start()
sqr = square(3)
print(sqr)
print(x)
bot.ultrasonicSensorRead(7,onRead)
print(dist)
and here is the result :
Version:3.7.3 (default, Dec 20 2019, 18:57:59)
[GCC 8.3.0]
init Megapi
<megapi_python3.mSerial object at 0xb5ec7e30>
9
3
0
distance 47.01724231640625cm
as you can see my test function works but not the ultrasonic one. Anyone know the reason for this or a way to work around it?