I am a beginner at Python Programming. What I am essentially trying to create is a program that will sample the voltage from an ADC every millisecond, put this data into a matrix, and then export it to an Excel file. Right now I'm using placeholders for the ADC data. Here's what I've got so far:
import xlsxwriter
import time
import numpy
import threading
a = numpy.array(['Time (ms)','Current (A)']) #Writes header values, sets up array a
maxtime = 200 #Limit for how long the program will run, in ms
elapsed = 0
def readdata(maxtime):
global elapsed
global a
if elapsed <= maxtime:
threading.Timer(0.01, readdata)
elapsed +=1
b = numpy.array(['Test1', 'Test2']) #'Test1' and 'Test2' Will eventually be replaced with ADC data
a = numpy.concatenate((a, b), axis=0) #Combines the arrays
else:
generatespreadsheet(a)
def generatespreadsheet():
global a
workbook = xlsxwriter.Workbook(time.strftime("%Y%m%d-%H%M%S") + ".xlsx") #Define workbook name as date and time
worksheet = workbook.add_worksheet() #Adds the first sheet
row = 0
for col, data in enumerate(a):
worksheet.write_row(col, row, data) #write the array in Excel
workbook.close()
readdata(maxtime)
The issue I'm having is that the code appears to do nothing at all. Not even throwing error codes. Any help is greatly appreciated!
UPDATE: I have switeched over to using a generator, code follows:
import xlsxwriter
import time
import numpy
import threading
a = numpy.array(['Time (ms)','Current (A)']) #Writes header values, sets up array a
maxtime = 200 #Limit for how long the program will run, in ms
elapsed = 0
def do_every(period,f):
def g_tick():
t = time.time()
count = 0
while True:
count += 1
yield max(t + count*period - time.time(),0)
g = g_tick()
while True:
time.sleep(next(g))
f()
global elapsed
global maxtime
elapsed += 1
if elapsed >= maxtime
generatespreadsheet()
def readdata():
global a
b = numpy.array(['Test1', 'Test2']) #'Test1' and 'Test2' Will eventually be replaced with ADC data
a = numpy.concatenate((a, b), axis=0) #Combines the arrays
def generatespreadsheet():
global a
workbook = xlsxwriter.Workbook(time.strftime("%Y%m%d-%H%M%S") + ".xlsx") #Define workbook name as date and time
worksheet = workbook.add_worksheet() #Adds the first sheet
row = 0
for col, data in enumerate(a):
worksheet.write_row(col, row, data) #write the array in Excel
workbook.close()
exit()
do_every(0.001,readdata)