Python Forum
reading raw data until condition is met, then stopping - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: reading raw data until condition is met, then stopping (/thread-13048.html)



reading raw data until condition is met, then stopping - unknowntothem - Sep-25-2018

Hello All,

Long time reader, first time writer Tongue 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()



RE: reading raw data until condition is met, then stopping - nilamo - Sep-25-2018

(Sep-25-2018, 03:38 PM)unknowntothem Wrote: file = open("/home/pi/data_log.csv", "a")
Open the file using write mode (w) instead of append (a), to clear the contents when you open it.


RE: reading raw data until condition is met, then stopping - ikejima - Sep-25-2018

Do you want to clear the file (/home/pi/data_log.csv) every 5 seconds and start write to the same file ?

file = open("/home/pi/data_log.csv", "a") -> the second parameter is mode, try use w
'w' open for writing, truncating the file first


i=0 
 
while adcx.get_last_result() < 840:
    # Create a empty file
    file = open("/home/pi/data_log.csv", "w")
    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()
    # write for 5 seconds
    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")

    file.close() 



RE: reading raw data until condition is met, then stopping - unknowntothem - Sep-26-2018

@ ikejima & nilamo, thank you very much this really helped, super elegant solution, I feel really dumb for not using it properly,

There is one issue with the while statement that I am unable to figure out though. How do I get the while loop to stop once the condition is met (i.e. adcx.get_last_result() > 840), because once the secondary 5 second while loop is complete the adcx sensor is below the 840 threshold again and the loop just continues?


RE: reading raw data until condition is met, then stopping - nilamo - Sep-26-2018

Why have the loop at all, then? If you remove that line, and unindent the whole block, then it'll run for 5 seconds, and be done.


RE: reading raw data until condition is met, then stopping - unknowntothem - Sep-26-2018

Hi Nilamo,

This is for a university project, the sensor pack is left in the test area until the reading on the sensor is more than 840 (raw data). At this point the program should stop looping the 5 second write loop.


RE: reading raw data until condition is met, then stopping - ikejima - Sep-26-2018

unknowntothem,

Yes, when the second loop is complete and the adcx sensor is below 840, the first 'while' will continue to run and it will open the file again and write for 5 seconds.
When the second loop is complete and adcx sensor is greater than 840, the first 'while' stopped.

Do you want to log only the last 5 seconds of sensor until its value is 840 ?

(Sep-26-2018, 02:02 PM)unknowntothem Wrote: @ ikejima & nilamo, thank you very much this really helped, super elegant solution, I feel really dumb for not using it properly,

There is one issue with the while statement that I am unable to figure out though. How do I get the while loop to stop once the condition is met (i.e. adcx.get_last_result() > 840), because once the secondary 5 second while loop is complete the adcx sensor is below the 840 threshold again and the loop just continues?



RE: reading raw data until condition is met, then stopping - unknowntothem - Sep-27-2018

(Sep-26-2018, 04:42 PM)ikejima Wrote: unknowntothem,

Yes, when the second loop is complete and the adcx sensor is below 840, the first 'while' will continue to run and it will open the file again and write for 5 seconds.
When the second loop is complete and adcx sensor is greater than 840, the first 'while' stopped.

Do you want to log only the last 5 seconds of sensor until its value is 840 ?

Hi Ikejima,

I would like to log only 5 seconds of data in total, during the five second loop somewhere in the middle the raw data will exceed 840 for less than 0.01 seconds and generate a overblast pressure graph then return to a value below 840. The issue is that once this happens I need the program to stop recording these 5 second loops, saving the whole graph in the .csv file, so I can't stop the loop the moment that the value exceeds 840, I need to see the following 0.01 seconds of data also.

Thanks for all the info so far, I really feel like this is a noob question, I've gotten this far learning python online but can't quite figure out this last bit.