My code:
Code: Select all
from machine import DAC, Pin, ADC
dac = DAC(Pin(25))
adc = ADC(Pin(36))
for i in range(0,255,100):
dac.write(i)
val = adc.read_u16()
print("dac=%s, adc=%s" % (i,val))
Code: Select all
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot
dac=0, adc=400
dac=100, adc=65535
dac=200, adc=65535
>>>
The second time I run the code (no edits, just re-run the same code):
Code: Select all
%Run -c $EDITOR_CONTENT
MPY: soft reboot
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
OSError: (-259, 'ESP_ERR_INVALID_STATE')
The error is from reallocating the same GPIO pin for the DAC. Is there a way in the code to say "Hey, I'm done. Forget what just happened."
I tried using Stop/Restart and Send EOF/Soft reboot under the Run menu, but I cannot get the code to run again. Same with Disconnect. The only way to re-run the code is to disconnect power to the ESP32. I have been trying both Thonny and the Arduino micropython IDEs.
Is this the normal process for a micro-controller? Load the code, run it once, edit the code, remove power from the device, reload code and test again?
On a related issue, how do I release the GPIO pin from the DAC? Or, how to detect if it is in use already? I have encapsulated the DAC code in a class, and I want to reuse that class in different parts of my code. However, it will break every time unless I figure out how to re-run the DAC code.
Thanks!