Skip to content

Instantly share code, notes, and snippets.

@mick001
Last active August 27, 2015 23:04
Show Gist options
  • Save mick001/562f92d33c78efdddaf8 to your computer and use it in GitHub Desktop.
Save mick001/562f92d33c78efdddaf8 to your computer and use it in GitHub Desktop.
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