The reason for the restart is that the filesystem doesn't synchronise with the host PC until it is re-mounted on the host. Peter explained this recently here:
viewtopic.php?f=2&t=7542&p=42993#p42972
The reason the `if` statement is failing is that it needs to be `adc.read() > 1400` not just `adc > 1400` (you need to compare the value, not the actual adc object). I'd probably suggest changing it to:
Code: Select all
sample = adc.read()
if sample > 1400:
f.write(str(tm1) + "," + str(sample) +'\n')
Also, no need to recreate the ADC object every time:
Code: Select all
from pyb import Pin, ADC
import time
f = open('datafile.csv', 'w')
adc = ADC(Pin('X1'))
for i in range(0,1000):
tm1 = pyb.millis()
sample = adc.read()
if sample > 1400:
f.write(str(tm1) + "," + str(sample) +'\n')
f.close()
If you're using firmware v1.12, I'd probably recommend using machine.ADC rather than pyb.ADC (we're planning to move all the pyb APIs into machine, to match the other ports). It's almost identical, other than you use the read_16() method instead (which returns a scaled 16-bit value).
Code: Select all
from machine import Pin, ADC
import time
f = open('datafile.csv', 'w')
adc = ADC(machine.Pin.board.X1)
for i in range(0,1000):
tm1 = pyb.millis()
sample = adc.read_16()
if sample > 1400: # You'll need to change this value to whatever the 16-bit equivalent is.
f.write(str(tm1) + "," + str(sample) +'\n')
f.close()
Also I'd recommend adding a time.sleep_ms() to that loop so you can get values spaced at the right sample rate.