class ANGLE(): # 1 def __init__(self, port=PORTB): from machine import ADC self.adc = ADC(36) self.adc.atten(ADC.ATTN_11DB) def deinit(self): self.adc.deinit() def readraw(self): return 4095 - self.adc.readraw() def read(self): data = 0 max = 0 min = 4096 for i in range(0, 10): newdata = 4095 - self.adc.readraw() data += newdata if newdata > max: max = newdata if newdata < min: min = newdata data -= (max + min) data >>= 3 return 100 * data / 4095
class Angle: def __init__(self, port): from machine import ADC self.adc = ADC(port[1]) self.adc.atten(ADC.ATTN_11DB) def deinit(self): self.adc.deinit() def readraw(self): return 4095 - self.adc.readraw() def read(self): data = 0 max = 0 min = 4096 for i in range(0, 10): newdata = 4095 - self.adc.readraw() data += newdata if newdata > max: max = newdata if newdata < min: min = newdata data -= (max + min) data >>= 3 return round(1024 * data / 4095, 2)
class ANGLE(): # 1 def __init__(self, port=PORTB): global class_map from machine import ADC if class_map['adc'] != None: class_map['adc'].deinit() self.adc = ADC(PORTB[1]) self.adc.atten(ADC.ATTN_11DB) class_map['adc'] = self.adc def deinit(self): self.adc.deinit() def readraw(self): return 4095 - self.adc.readraw() def read(self): data = 0 max = 0 min = 4096 for i in range(0, 10): newdata = 4095 - self.adc.readraw() data += newdata if newdata > max: max = newdata if newdata < min: min = newdata data -= (max + min) data >>= 3 return round(1024 * data / 4095, 2)
class Light: def __init__(self, port): from machine import ADC, Pin self.adc = ADC(port[1]) self.adc.atten(ADC.ATTN_11DB) self.d_pin = Pin(port[0], Pin.IN, Pin.PULL_UP) @property def analogValue(self): data = 0 max = 0 min = 4096 for i in range(0, 10): newdata = 4095 - self.adc.readraw() data += newdata if newdata > max: max = newdata if newdata < min: min = newdata data -= (max + min) data >>= 3 return round(1024 * data / 4095, 2) @property def digitalValue(self): return self.d_pin.value() def deinit(self): self.adc.deinit()
class Light: def __init__(self, port=PORTB): global class_map from machine import ADC, Pin if class_map['adc'] != None: class_map['adc'].deinit() self.adc = ADC(PORTB[1]) self.adc.atten(ADC.ATTN_11DB) class_map['adc'] = self.adc self.d_pin = Pin(PORTB[0], Pin.IN, Pin.PULL_UP) def a_read(self): data = 0 max = 0 min = 4096 for i in range(0, 10): newdata = 4095 - self.adc.readraw() data += newdata if newdata > max: max = newdata if newdata < min: min = newdata data -= (max + min) data >>= 3 return round(1024 * data / 4095, 2) def d_read(self): return self.d_pin.value()
xfilt = 0 yfilt = 0 # history old_x = old_y = 0 old_button = None # run program run = True next_blink = 0 # time when next to blink the led while run: gc.collect() # check for messages mqtt.check_msg() xfilt = (1 - alpha) * xfilt + alpha * (xout.readraw() - xoff) yfilt = (1 - alpha) * yfilt + alpha * (yout.readraw() - yoff) xx = xfilt / scale yy = yfilt / scale print("x={:8.3f} y={:8.3f}".format(xx, yy)) # print("x={:40s}| y={:40s}|".format(int(20*(xx+1))*'*', int(20*(yy+1))*'*')) if abs(old_x - xx) > 0.005: print("publish x", xx) mqtt.publish("x", str(xx)) old_x = xx if abs(old_y - yy) > 0.005: print("publish y", yy) mqtt.publish("y", str(yy)) old_y = yy if old_button is not button(): mqtt.publish("stop", str(not button()))