Python一次只从GPIO追加一个数据

Python一次只从GPIO追加一个数据,python,list,raspberry-pi,append,Python,List,Raspberry Pi,Append,所以我刚开始使用python,从一个连接到树莓圆周率的称重传感器中提取读数。代码如下: hx = HX711(5, 6) hx.set_reading_format("LSB", "MSB") hx.set_reference_unit(26.978) hx.reset() hx.tare() while True: for i in range (15): val = int(hx.get_weight(5)) newval = abs(round(

所以我刚开始使用python,从一个连接到树莓圆周率的称重传感器中提取读数。代码如下:

hx = HX711(5, 6)
hx.set_reading_format("LSB", "MSB")
hx.set_reference_unit(26.978)
hx.reset()
hx.tare()


while True:
    for i in range (15):
        val = int(hx.get_weight(5))
        newval = abs(round((float(val/1000)),1))        
        X = []
        X.append (newval)
        print ([X])
        hx.power_down()
        hx.power_up()
我想我会在这个时间范围内得到一份阅读清单,也许是4?但我总是得到一个。数据肯定会频繁地进入列表,但我的列表总是只有一个数据


我肯定我做错了什么,请帮我大忙。

我注意到的两件事都是错的:

  • 您使用了for循环,没有在任何指令中使用索引,这个for循环在while循环中。这是毫无意义的,所以把它去掉
  • 在每次迭代中初始化X列表,这就是为什么它只有一个值。您可以将X=[]置于while循环之上 e、 g:

    hx = HX711(5, 6)
    hx.set_reading_format("LSB", "MSB")
    hx.set_reference_unit(26.978)
    hx.reset()
    hx.tare()
    
    
    while True:
        X = []
        for i in range (15):
            val = int(hx.get_weight(5))
            newval = abs(round((float(val/1000)),1))        
            X.append (newval)
            hx.power_down()
            hx.power_up()
        print ([X])
    
    将显示一组包含15个值的列表