Hi,
I'm very new to use the pyvoard v1.1 and I've run into something strange. Basically I want to read an analog signal at pin X19 and store it into a file. However, the pyboard only write into the file the last acquired voltage. Indeed, in the pyboard appear a file named voltage.txt, but there is only one measure. I'm anyway sure that the measure the the last one cause the index i, that the pyboard also writes into the file, has a value which is similar to the number of second I wait before pressing the switch. To be even sure the program was working as expected, I've also set a threshold voltage. If the read voltage is greater than the threshold voltage, then the pyboard stop the voltage acquisition. I've then used the pyboard to read the voltage output of a voltage generator and, when I set the voltage output greater thant the voltage threshold, the pyboard nicely stop the data acquisition and a blue led is turned toggles, just as desired.
Anyway, I would really be grateful to anyone who could tell me why the pyboard writes to the file only the last measure, since I would need, in my project, to keep track of the last 100 measures or so.
For completeness, the pyboard doesn't have a microsd card inserted yet
Below you can fine the code.
#---------------------------------------------------#
# main.py -- put your code here!
import pyb
import time
myled = pyb.LED(3)
signal_led = pyb.LED(4)
V_channel = pyb.ADC(pyb.Pin('X19'))
sw = pyb.Switch()
done = False
i = 0
while done == False:
i = i+1
myled.on()
time.sleep_ms(500)
V_analog = V_channel.read()
V_analog = V_analog*3.3/4096
myled.off()
time.sleep_ms(500)
with open('voltage.txt','w') as myfile:
myfile.write(str(i)+' '+str(V_analog)+' \n')
if sw.value() == True:
done = True
myled.off()
if V_analog > 1.75:
done = True
signal_led.on()
time.sleep_ms(500)
signal_led.off()
myled.off()
#--------------------------------#
As you can see the code is pretty simple, so why does the pyboard store only the last readed value?
pyboard only write to file last value measured by the ADC
Re: pyboard only write to file last value measured by the ADC
Because you open the file in "w" /write) mode, and not in "a" (append). Write mode will start over with an empty file.
For further information consult any Python language lesson, e.g. https://docs.python.org/3.5/tutorial/inputoutput.html
For further information consult any Python language lesson, e.g. https://docs.python.org/3.5/tutorial/inputoutput.html
Re: pyboard only write to file last value measured by the ADC
thank you for your quick answer,
unluckily, the problem persist. Indeed, I changed the open('voltage.txt','w') in open('voltage.txt','a') and still I can only see the last measured value. I would like the pyboard to write to file at the end of each while loop so that I have a time series of the voltage value.
I actually did the following experiment:
After I changed the file mode from 'w' to 'a', I turned on the pyboard and let it execute the main.py script for several seconds. Then I interrupted it pushing the switch and I've started it again, letting it execute for several seconds and the interrupting the script execution using the switch
In the voltage.txt file generate there were only the two following measures,
6 0.4467
8 0.4369
both are correct since the first in the first run the pyboard executed for a shorter time than in the second run, but you see anyway only the last value measured in each run.
I was expecting something like:
0 0.45
1 0.43
2 0.46
...
6 0.4467
0 0.432
...
8 0.456
Can you please help me?
the code is:
unluckily, the problem persist. Indeed, I changed the open('voltage.txt','w') in open('voltage.txt','a') and still I can only see the last measured value. I would like the pyboard to write to file at the end of each while loop so that I have a time series of the voltage value.
I actually did the following experiment:
After I changed the file mode from 'w' to 'a', I turned on the pyboard and let it execute the main.py script for several seconds. Then I interrupted it pushing the switch and I've started it again, letting it execute for several seconds and the interrupting the script execution using the switch
In the voltage.txt file generate there were only the two following measures,
6 0.4467
8 0.4369
both are correct since the first in the first run the pyboard executed for a shorter time than in the second run, but you see anyway only the last value measured in each run.
I was expecting something like:
0 0.45
1 0.43
2 0.46
...
6 0.4467
0 0.432
...
8 0.456
Can you please help me?
the code is:
Code: Select all
# main.py -- put your code here!
import pyb
import time
myled = pyb.LED(3)
signal_led = pyb.LED(4)
V_channel = pyb.ADC(pyb.Pin('X19'))
sw = pyb.Switch()
done = False
i = 0
while done == False:
i = i+1
myled.on()
time.sleep_ms(500)
V_analog = V_channel.read()
V_analog = V_analog*3.3/4096
myled.off()
time.sleep_ms(500)
with open('voltage.txt','a') as myfile:
myfile.write(str(i)+' '+str(V_analog)+' \n')
if sw.value() == True:
done = True
myled.off()
if V_analog > 1.75:
done = True
signal_led.on()
time.sleep_ms(500)
signal_led.off()
myled.off()
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: pyboard only write to file last value measured by the ADC
You may be encountering this problem.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.