Python附加一次只列出一个来自GPIO的数据

时间:2018-04-26 06:35:07

标签: 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((float(val/1000)),1))        
        X = []
        X.append (newval)
        print ([X])
        hx.power_down()
        hx.power_up()

我想我会在那段时间内得到一份读数清单,也许是4?但我总是得到一个。数据肯定足以制作一个列表,但我的列表总是只有一个数据。

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

1 个答案:

答案 0 :(得分:0)

我注意到的两件事,这是错误的:

  1. 你使用了for循环,没有在任何指令中使用索引,这个for循环在while循环中。这是毫无意义的,所以删除它。
  2. 在每次迭代中初始化X列表,这就是为什么它只有一个值。你可以将X = []置于while循环之上。
  3. 例如:

    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个值的列表