I'm trying to store data read from the ADC to a file on the SD-card. X1 is connected to an external function generator configured to a 10 Hz triangular wave.
As this plot shows there are some discontinuities in the data: I also tried storing the variable of the for-loop to check if the problem persists without the ADC (zoomed in view): Both scatter plots use time from pyb.micros() for the x-axis.
Any info on what the pyboard is doing when it's not storing values? Would be good to know if there is a way to get better real-time preformance with this simple method. I guess it is better to use read_timed and store values to a buffer, and then only store to file when the buffer is filled, but probably need to understand the limitations to get that working properly as well...
Code: Select all
import pyb
use_adc = True # Read values from ADC
# use_adc = False # Use loop variable instead of ADC values
length = 1000 # Number of values to store to file
adc = pyb.ADC('X1')
# Open file on SD-card in write mode
f = open('/sd/log_1.txt', 'w')
t_start = pyb.micros() # Save start time
for i in range(length):
t = pyb.micros() - t_start # Calculate current time
if (use_adc):
value = adc.read()
value_string = '%i, %i;' %(t, value)
else:
value_string = '%i, %i;' %(t, i)
# Write string with time and data to file for scatter plot
f.write(value_string)
f.close()