how to put variable in file inside Flash: pycom
-
from machine import ADC import time adc = ADC() fichier=open('valeurenmv.py',"a") while True : bat_voltage = adc.channel(attn=ADC.ATTN_11DB, pin='P16') vbat = bat_voltage.value() # note that the expansionboard 3 has a voltage divider of 1M / 1M to account for # 1M / 1M, ratio = 1:2 vbat = vbat*2 time.sleep(3) print('battery voltage:', vbat, 'mV') fichier.write('\nvbat')
Hi , can you help me to put result of variable vbat in file
-
@HAMMOUTI try to change the name of your file "valeurenmv.py" by "valeurenmv.txt" !
Jiemde
-
This post is deleted!
-
@HAMMOUTI What is the problem. I can't spot it in the capture. The only odd I see is the reported battery voltage.
-
think you for your help, i try this code, but i have this error see capture .
how to fix this error.
-
@HAMMOUTI There are several way in Python to create a formatted string with variable. The most common ones are the % operator on strings or the .format method of strings. Both are implemented in MicroPython and well documented in every Python textbook. Just a few simple examples for your case:
fichier.write('\nvbat=%f' % vbat)
or, if you just want the number:
fichier.write('\n%f' % vbat)
or using the format method:
fichier.write('\nvbat={}'.format(vbat))
or
fichier.write('\n{}'.format(vbat))