def get_moisture(): try: ADC0832.setup() output_fromGPIO(pin, True) while True: moisture = ADC0832.getResult(0) if moisture != -1: return moisture sleep(1) except: ADC0832.destroy() finally: output_fromGPIO(pin, False)
def get_light(): try: ADC0832.setup() output_fromGPIO(pin, True) light = ADC0832.getResult(1) - 80 print(light) if light < 0: light = 0 return light except: ADC0832.destroy() finally: output_fromGPIO(pin, False)
#!/usr/bin/env python import ADC0832 import time def init(): ADC0832.setup() def loop(): while True: res = ADC0832.getResult() print 'res = %d' % res time.sleep(0.2) if __name__ == '__main__': init() try: loop() except KeyboardInterrupt: ADC0832.destroy() print 'The end !'
#!/usr/bin/env python import ADC0832 import time def init(): ADC0832.setup() def loop(): while True: res = ADC0832.getResult() moisture = 255 - res print 'analog value: %03d moisture: %d' %(res, moisture) time.sleep(0.1) if __name__ == '__main__': init() try: loop() except KeyboardInterrupt: ADC0832.destroy() print 'The end !'
# ADC_CLK=clk # ADC_DIO=dio # GPIO.setwarnings(False) # GPIO.setmode(GPIO.BOARD) # GPIO.setup(ADC_CS, GPIO.OUT) # GPIO.setup(ADC_CLK, GPIO.OUT) def loop(): l = [] for i in range(10): digitalVal = ADC0832.getResult() # Get ADC result, input channal # getResult()函数代码实现ADC0832工作原理 # 得到0~255之间的一个数 # 详细参见ADC0832.py l.append(3.3 * float(digitalVal) / 255) # 转换为电压量 time.sleep(0.2) print("{:0.3f}".format(np.array(l).mean())) if __name__ == '__main__': init() try: loop() except KeyboardInterrupt: pass finally: ADC0832.destroy() #调用GPIO.cleanup print("The end!")