INTERVAL = 1.0 # which ADC channel ADC_CHANNEL = 0 if 'debug' in sys.argv: DEBUG = True else: DEBUG = False ts = TimeSeries(["voltage"]) if DEBUG: print("\nPress CTRL+C to exit.\n") time.sleep( INTERVAL) # short pause after ads1015 class creation recommended(??) try: while True: t = time.time() value = adc.read_adc(ADC_CHANNEL, gain=GAIN, data_rate=DATA_RATE) volts = float(value) / MAX_VALUE * GAIN_VOLTAGE / VOLT_DIVIDER if DEBUG: print("{0:.3f} {1:5d} {2:.6f}".format(t, value, volts)) ts.store(t, [volts]) time.sleep(INTERVAL) except KeyboardInterrupt: print("...exiting...")
if use_hw_spi: mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(0, 0)) else: mcp = Adafruit_MCP3008.MCP3008(clk=CLK, cs=CS, miso=MISO, mosi=MOSI) ts = TimeSeries(["voltage", "temp"]) print("\nPress CTRL+C to exit.\n") try: while True: # read the analog pin value = mcp.read_adc(TARGET_ADC) volt = voltage(value) temp_C = temp(volt) temp_F = 9 * temp_C / 5 + 32 t = time.time() form = 't={time:.3f} - val= {volt:.3f} V == {temp:.3f} C / {temp_F:.3f} F' print(form.format(time=t, volt=volt, temp=temp_C, temp_F=temp_F)) ts.store(t, [volt, temp_F]) # hang out and do nothing for a second time.sleep(INTERVAL) except KeyboardInterrupt: # normal to exit with ^C pass