Here is part of my code:
Code: Select all
tempPin = pyb.ADC(pyb.Pin.board.X11)
def getTemp(pin):
temps = []
for i in range(100):
tempReading = pin.read()
tempVolts = tempReading * 3.3 / 4096.0
tempC = (tempVolts - 0.5) * 100.0
temps.append(tempC)
return sum(temps) / float(len(temps))
def displayTemp(pin):
lcd.move_to(0,0)
lcd.clear()
lcd.putstr("Temp: "+str("{0:.1f}".format(getTemp(pin)))+" deg C")
pyb.LED(4).toggle()
timer = pyb.Timer(14)
timer.init(freq=2)
timer.callback(lambda t: displayTemp(tempPin))
I am aware that interrupts are not allowed to allocate memory, and I'm wondering if there is any way to make what I'm trying to do here work. I tried predefining all the variables (all float to 0.0, all strings to "" and all lists to [] so that there would be no memory allocation for variables, but that didn't help. Your wisdom appreciated!
P.S. the lambda is there in the callback, because without it, the code seemed to only ever be called once, even if it was code that didn't cause a memory error, so bonus point for helping me understand that.