-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtimeNotification_continuous.py
More file actions
executable file
·68 lines (43 loc) · 2.73 KB
/
timeNotification_continuous.py
File metadata and controls
executable file
·68 lines (43 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from Adafruit_ADS1x15 import ADS1x15
import time
import numpy as np
pga = 6144 # Set full-scale range of programable gain amplifier (page 13 of data sheet), change depending on the input voltage range
ADS1115 = 0x01 # Specify that the device being used is the ADS1115, for the ADS1015 used 0x00
adc = ADS1x15(ic=ADS1115) # Create instance of the class ADS1x15 called adc
# Function to print sampled values to the terminal
def logdata():
print "sps value should be one of: 8, 16, 32, 64, 128, 250, 475, 860, otherwise the value will default to 250"
frequency = input("Input sampling frequency (Hz): ") # Get sampling frequency from user
sps = input("Input sps (Hz) : ") # Get ads1115 sps value from the user
time1 = input("Input sample time (seconds): ") # Get how long to sample for from the user
period = 1.0 / frequency # Calculate sampling period
datapoints = int(time1*frequency) # Datapoints is the total number of samples to take, which must be an integer
dataarray=np.zeros([datapoints,2]) # Create numpy array to store value and time at which samples are taken
adc.startContinuousConversion(0, pga, sps) # Begin continuous conversion on input A0
print "Press ENTER to start sampling"
raw_input()
startTime=time.time() # Time of first sample
t1=startTime # T1 is last sample time
t2=t1 # T2 is current time
i=0
for x in range (0,datapoints) : # Loop in which data is sampled
# This prints to terminal the amount of sampling time left each second
i+=1
if (i >= frequency):
print "Time left :" , round((time1 - (t2-startTime))),"s"
i=0
dataarray[x,0]= adc.getLastConversionResults()
dataarray[x,1] = time.time()-startTime
while (t2-t1 < period) : # Check if t2-t1 is less then sample period, if it is then update t2
t2=time.time() # and check again
t1+=period # Update last sample time by the sampling period
return (dataarray)
dataSamples = logdata() # Call function to log data
printchoice=raw_input("Do you want to save data to CSV (Y/N): ") # Ask user if they want to save sampled values
if (printchoice == "Y" or "y") :
np.savetxt('dataSamples.txt',dataSamples, fmt='%.3f', delimiter = ',') # Save dataSamples to the file 'dataSamples.txt' using comma separated values
# Calculate time between succesive samples, store in sampleIntervals array
number_samples = len(dataarray) # Number of samples taken
sampleIntervals = np.zeros(number_samples-1) # Create numpy array of length equal to the number of samples taken
for i in range(0, number_samples-1): # Store time difference between sample i and sample i+1 in each element of the sampleIntervals array
sampleIntervals[i]=dataarray[i+1,1]-dataarray[i,1]