# ================================================
# ABElectronics ADC Pi 8-Channel ADC demo
# Version 1.0 Created 09/05/2014
#
# Requires python smbus to be installed
# run with: python demo-readvoltage.py
# ================================================


# Initialise the ADC device using the default addresses and sample rate, change this value if you have changed the address selection jumpers
# Sample rate can be 12,14, 16 or 18
adc = ADCPi(0x68, 0x69, 12)

while (True):

  # clear the console
  os.system('clear')

  # read from adc channels and print to screen
  print ("Channel 1: %02f" % adc.readVoltage(1))
  print ("Channel 2: %02f" % adc.readVoltage(2))
  print ("Channel 3: %02f" % adc.readVoltage(3))
  print ("Channel 4: %02f" % adc.readVoltage(4))
  print ("Channel 5: %02f" % adc.readVoltage(5))
  print ("Channel 6: %02f" % adc.readVoltage(6))
  print ("Channel 7: %02f" % adc.readVoltage(7))
  print ("Channel 8: %02f" % adc.readVoltage(8))

 
  # wait 0.5 seconds before reading the pins again
  time.sleep(0.5)
Exemplo n.º 2
0
last_df_write = ""
while inputs:

    # Wait for at least one of the sockets to be ready for processing
    # or timeout of 1 second
    print >>sys.stderr, '\nwaiting for the next event'
    readable, writable, exceptional = select.select(inputs, outputs, inputs, 1)
    if not (readable or writable or exceptional):
        print >>sys.stderr, '   timed out, go collect some data'
        try:
            if args.test:
                v = uniform(0.4,5.05)
                time.sleep(1)
            else:
                v = adc.readVoltage(args.adcchannel)
            if len(rawReadings) >= args.nsamples:
                del rawReadings[0]
            rawReadings.append(v)
            if len(rawReadings) > 0 : 
                average = math.fsum(rawReadings)/len(rawReadings)
            if args.debug: 
                print >> sys.stderr, "{:.4f},{:.4f}".format(v,average)
            if len(rawReadings) == 1:
                last_df_write = "{},{:.4f},{}\n".format(str(datetime.datetime.now()),average,args.adcchannel)
                df.write(last_df_write)
        finally:
            df.close()
            rt.stop()
        continue
#
# Requires python smbus to be installed
# run with: python demo-readvoltage.py
# ================================================


# Initialise the ADC device using the default addresses and sample rate, change this value if you have changed the address selection jumpers
# Sample rate can be 12,14, 16 or 18
adc = ADCPi(0x68, 0x69, 18)

def writetofile(texttowrtite):
	f = open('adclog.txt', 'a')
	f.write(str(datetime.datetime.now()) + " " + texttowrtite)
	f.closed

while (True):


  # read from adc channels and write to the log file
  writetofile ("Channel 1: %02f\n" % adc.readVoltage(1))
  writetofile ("Channel 2: %02f\n" % adc.readVoltage(2))
  writetofile ("Channel 3: %02f\n" % adc.readVoltage(3))
  writetofile ("Channel 4: %02f\n" % adc.readVoltage(4))
  writetofile ("Channel 5: %02f\n" % adc.readVoltage(5))
  writetofile ("Channel 6: %02f\n" % adc.readVoltage(6))
  writetofile ("Channel 7: %02f\n" % adc.readVoltage(7))
  writetofile ("Channel 8: %02f\n" % adc.readVoltage(8))

 
  # wait 1 second before reading the pins again
  time.sleep(1)