from machine import Pin, Timer import time # Read the analog inputs # Use timer interrupts to set the sampling rate to 100Hz # Send the results to a file Readme.txt on the PICO chip # Convert A/D reding to Volts p16 = Pin(16, Pin.OUT) led = Pin(17, Pin.OUT) tim = Timer() N = 0 flag = 0 def tick(timer): global led global N global flag flag = 1 N += 1 led.toggle() # 100Hz for 2 seconds tim.init(freq=100, mode=Timer.PERIODIC, callback=tick) a2d0 = machine.ADC(0) a2d1 = machine.ADC(1) a2d2 = machine.ADC(2) a2d3 = machine.ADC(3) a2d4 = machine.ADC(4) conversion_factor = 3.3 / (65535) file1 = open("readme.txt", "w") pin = Pin(15,Pin.IN,Pin.PULL_UP) # N = number of samples N = 0 Nmax = 200 print('Collecting ', Nmax, ' samples') while (N < Nmax): if (flag == 1): p16.value(1) a0 = a2d0.read_u16() * conversion_factor a1 = a2d1.read_u16() * conversion_factor a2 = a2d2.read_u16() * conversion_factor a3 = a2d3.read_u16() * conversion_factor a4 = a2d4.read_u16() * conversion_factor # print(a2) # formatted outout: fixed decimal places file1.write(str(N) + " ") file1.write(str("%8.5f"%a0) + " ") file1.write(str("%8.5f"%a1) + " ") file1.write(str("%8.5f"%a2) + " ") file1.write(str("%8.5f"%a3) + " ") file1.write(str("%8.5f"%a4)) file1.write("\n") p16.value(0) flag = 0 p16.value(0) led.value(0) file1.close() print("Sampling Complete")