from machine import Pin
import time
# GP's to be used as In (0=lsb)pins = [machine.Pin(i, machine.Pin.IN) for i in (0, 1, 2, 3)]
# GP's to be used as In (0=lsb)
pins = [machine.Pin(i, machine.Pin.IN) for i in (0, 1, 2, 3)]
# GP's to be used, with Pull_down
pins = [machine.Pin(i, machine.Pin.IN, machine.Pin.PULL_DOWN) for i in (0, 1, 2, 3)]
LED = Pin("LED", Pin.OUT)
binV = 0 # Calculates and updates the weight of the binry input value
back = 0 # Temporary holder for binary input value
sensor_temp = machine.ADC(4) # Assigns the ADC(4) - intitiates the variable
conversion_factor = 3.3 / (65535) # creates the conversion factor - to be used with the A-D conversion
def update(binV):
'''get the Sp offset value (binary weights [0..15]) - passed 0, at every call,
in case of a change'''
if pins[0].value() == 1:
binV = binV + 1
if pins[1].value() == 1:
binV = binV + 2
if pins[2].value() == 1:
binV = binV + 4
if pins[3].value() == 1:
binV = binV + 8
return binV
def Temp_Sensor():
reading = sensor_temp.read_u16() * conversion_factor # reads the onboard sensor, and then quantizes it via the cf
temperature = 27 - (reading - 0.706)/0.001721 # correctly scales to a meanfull value.
return temperature # passes back the sensors value as ℃.
def LED_ON():
if BinV <= 7:
LED.on
else:
LED.off
while True:
back = 0
back = update(back)
temperature = int(Temp_Sensor()) # a call to get the temperature - will result in an integer value
print("Binary weight value = " + str(back),"Pv = " + str(temperature) + "\u2103")
time.sleep_ms(1000) # cause the TC to update at 1 second intervals