# readADC_pico.py    readADC(nn,cnvrt)
import machine
import time
# MakPiADC board works on 2021FJun14
# CD0 is ADC0
# Pot is ADC1
#CDS0 & pot work if SW2 & SW# are on

def RAPasc(nn,cvrsFactor):
    if nn==1 :     #ADC1
        strN1=1
        timeNow=time.time()
        strTime=str(timeNow)+".000"
        
        reading=readADC_pico(nn,cvrsFactor)
        if cvrsFactor==1:
            readingHex=hex(int(reading))
            RAPreading="ADC"+strN1+":("+readingHex+"+"+strTime+"j)"
        else:
            strReading=str(reading).split()
            RAPreading="ADC"+strN1+":(",strReading+"+"+strTime+"j)"
            print(RAPreading)
        #end if
    else:
        #nn must be 0  #ADC0
        strN0="0"
        strReading=str(readADC_pico(0))
        #print("|"+strReading+"|")
        strReading=strReading.strip()
        RAPreading="ADC"+strN0+":("+strReading+"+"+strTime+"j)"
        print(RAPreading)
    #end if
    return RAPreading

def readADC_pico(nn,cnvrt=5.035477e-05):
    #nn is the pico GPnn (ADCnn) to read
    #cnvrt = 5.035477e-05 converts the reading to volts.
    # that is the default cnvrt factor.
    adcFirst = machine.ADC(nn)
    adcValue=adcFirst.read_u16()
    #print("The value of ADC0 is", adcValue)

    #conversion_factor = 3.3 / (65535)
    #print("conversion_factor:",conversion_factor)
    adcVoltage=adcValue * cnvrt
    #print("The voltage at ADC0 is", adcVoltage)
    #print("-bye- from readADC_pico.py")
    return adcVoltage
#end def
progName="readADC_pico.py"
print("progName:",progName)
n0=0      #ADC number = 0 or 1 usually
strN0=str(n0)
n1=1      #ADC number = 0 or 1 usually
strN1=str(n1)
cvrsFactor=1.0000
#if conversion Factor = 1 exactly, the reading
# will be printed in hexadecimal
while True:
    timeNow=time.time()
    strTime=str(timeNow)+".000"
    strReading=str(readADC_pico(n0))
    #print("|"+strReading+"|")
    strReading=strReading.strip()
    strRAPasc="ADC"+strN0+":("+strReading+"+"+strTime+"j)"
    print(strRAPasc)
    #print("ADC"+strN1+":"+readADC(1,cvrsFactor))
    reading=readADC_pico(n1,cvrsFactor)
    #print("reading",reading)
    if cvrsFactor==1:
        readingHex=hex(int(reading))
        strRAPasc="ADC"+strN1+":("+readingHex+"+"+strTime+"j)"
        print(strRAPasc)
    else:
        strReading=str(reading).split()
        strRAPasc="ADC"+strN1+":(",strReading+"+"+strTime+"j)"
        print(strRAPasc)
    #end if
    print()   # print a blank line
    time.sleep(1)  # 1 sec
#end while

    
#/readADC_pico.py     stored in Raspberry Pi Pico