Sep-25-2018, 03:38 PM
Hello All,
Long time reader, first time writer I've got a bit of a noob question I guess, have a python program reading raw data from a sensor, this data is supposed to be "buffered" for 5 second in a csv file then at the end of the 5 seconds the file contents should be cleared and the buffer started again, when the sensor reads more than 840 the loop should stop and the event will be captured in the csv file.
Everything works but for some I can't get a my file to clear the contents and start again every 5 seconds, any advice would be greatly appreciated.
Long time reader, first time writer I've got a bit of a noob question I guess, have a python program reading raw data from a sensor, this data is supposed to be "buffered" for 5 second in a csv file then at the end of the 5 seconds the file contents should be cleared and the buffer started again, when the sensor reads more than 840 the loop should stop and the event will be captured in the csv file.
Everything works but for some I can't get a my file to clear the contents and start again every 5 seconds, any advice would be greatly appreciated.
import time import os from datetime import datetime import Adafruit_ADS1x15 # Or create an ADS1015 ADC (12-bit) instance. adc = Adafruit_ADS1x15.ADS1015() #give i2c address to ADC adcx = Adafruit_ADS1x15.ADS1015(address=0x4b, busnum=1) GAIN = 1 #setup analog to digital board adcx.start_adc(0, gain=GAIN, data_rate=3300) file = open("/home/pi/data_log.csv", "a") i=0 if os.stat("/home/pi/data_log.csv").st_size == 0: file.write("Time,Sensor1,Sensor2,Sensor3,Sensor4\n") file.write(bytes(datetime.now())+"\n") print('Reading ADS1x15 channel 0 for 5 seconds...') start = time.time() while adcx.get_last_result() < 840: while (time.time() - start) <= 5.0: now = datetime.now() usec = "%02d%06d" % (now.second, now.microsecond) value = adcx.get_last_result() file.write(str(usec) + "," + "{0}".format(*value) + "\n") adc.stop_adc()