I am new to ESP32 programming. I am trying to write some code, test it, edit it, test again. I can write the code, run it once, edit it, but it won't run again. I am not sure how to reset the ESP32 so the edited code runs as if it has never run before.
My code:
from machine import DAC, Pin, ADC
dac = DAC(Pin(25))
adc = ADC(Pin(25))
for i in range(0,255,100):
dac.write(i)
val = adc.read_u16()
print("dac=%s, adc=%s" % (i,val))
It runs the first time
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot
dac=0, adc=0
dac=100, adc=0
dac=200, adc=0
>>>
but not the second time (no edits, just re-run the same code):
>>> %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.
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!