from machine import Pin, Timer
import machine
import time

led = Pin(25, Pin.OUT)
button = Pin(14, Pin.IN, Pin.PULL_DOWN)

sensor_cpu_temp = machine.ADC(4)
conversion_factor = 3.3/65535  

#Take sampel Routine
def take_sampel(starttime):
    reading = sensor_cpu_temp.read_u16() * conversion_factor                        # Measurement math
    temperature = 27 - (reading - 0.706)/0.001721
    print(str(int((time.ticks_us()-starttime)/1000)) + "\t" + str(temperature))     # Print Sampel data

# Measurement Routine
def mes(sps, duration):
    starttime = int(time.ticks_us())                                                # Time of measuere start
    endtime = int(time.ticks_us() + (duration*1000000))                             # Time to stop the measurement
    print("Measurement begin")
    led.on()                                                                        # Turn on led to indicate active measurement

    sampel_time = 1/sps*1000000                                                     # Calculate time between sampels
    next_sampel = time.ticks_us()                                                   # Define the variable

    file = open("mes.csv", "w")

    while time.ticks_diff(time.ticks_us(), starttime) <= (duration*1000000):        # Loop during dutation time
        if time.ticks_us() >= next_sampel:                                          # Check if it is time to take a sample
            take_sampel(starttime)
            file.write(str(temperature))
            file.flush()
            next_sampel = next_sampel+sampel_time                                   # Define next sample
    print("End")
    led.off()                                                                       # Turn off led

while True:
    if button.value():
        mes(4,1.1)
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT