Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Measuring function in Python, to be used with the Arduino script
#------------------------------------------------------------------------------
# Measuring function. Makes nSamples measurements through an Arduino object
# Notes:
# The program loops until it collects the nSamples readings
#
# This function accepts an object of the Arduino class
def measure(nSamples,arduino,save=False):
try:
print('Waiting for measurements...\n')
data = np.array(arduino.readData(nSamples,array=True,Floaters=True))
# Convert microseconds readings into seconds (1 s = 10^6 micro s)
data = data/1000000
print(str(nSamples) + ' measurements done:',data,'\n')
except Exception as e:
print(e)
if save:
try:
path = 'C:\\measurements.txt'
file = open(path,'w')
for element in data:
file.write(str(element))
file.write('\n')
file.close()
print('Data saved successfully')
except Exception as e:
print(e)
#------------------------------------------------------------------------------
# Run the program
# Make 1 measurement (the same number of measurement must be set in the Arduino
# sketch for the program to work properly).
measurements = 1
uno = Arduino()
measure(measurements,arduino,save=True)
uno.closeConn()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment