Automation pHAT - save ADC data

Goodmorning everyone, I’m testing the Automation pHAT on my RPI zero and it works great.

Now i’m using this python basic code:

#!/use/bin/env python

import time
import automationhat
time.sleep(1)

print(“”"
Press CTRLC to exit.
“”")

while True:
value = automationhat.analog.one.read()
print(value)
time.sleep(0.25)

I need to start “recording” data and then save the data when i send a stop command.

If you’re looking for a pure Python solution, then the code below will simply write the values to a text file, until it catches a control-c, at which point it will close the file and exit. You could add something like time.strftime("%d%m%%y-%H%M%S", time.localtime()) if you want to timestamp the values.

If you want a quick and dirty solution with the code you already have, then you can just save your code as foo.py, then in the terminal do ./foo.py > output.txt. That will pipe the output of your program (the readings) to a text file and it should exit when you press control-c also.

#!/usr/bin/env python

import sys
import time
import automationhat

time.sleep(1)

f = open("output.txt", "w")

while True:
    try:
        value = automationhat.analog.one.read()
        f.write(str(value) + "\n")
        time.sleep(0.25)

    except KeyboardInterrupt:
        f.close()
        sys.exit(-1)