【发布时间】:2018-09-18 07:56:04
【问题描述】:
我正在尝试实现一个光传感器输入,以便它在更大的 python 脚本中输出一个值。
具体打印 LED_BRIGHTNESS = 75 或 LED_BRIGHTNESS = 255
基本上,当此脚本运行时,我希望传感器指示 LED 串的 LED 亮度
我开始尝试做以下事情......
import urllib2
import xml.etree.ElementTree as ET
import time
from neopixel import *
import sys
import os
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(4,GPIO.IN)
if GPIO.input(4) == 1:
print "LED_BRIGHTNESS = 75"
else:
print "LED_BRIGHTNESS = 255"
# LED strip configuration:
LED_COUNT = 95 # Number of LED pixels.
LED_PIN = 18 # GPIO pin connected to the pixels (18 uses PWM!).
#LED_PIN = 10 # GPIO pin connected to the pixels (10 uses SPI /dev/spidev0.0).
LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz)
LED_DMA = 5 # DMA channel to use for generating signal (try 5)
#LED_BRIGHTNESS = 255 # Set to 0 for darkest and 255 for brightest
LED_INVERT = False # True to invert the signal (when using NPN transistor level shift)
LED_CHANNEL = 0 # set to '1' for GPIOs 13, 19, 41, 45 or 53
LED_STRIP = ws.WS2811_STRIP_GRB # Strip type and colour ordering
这会将结果输出到终端,但我需要将它输出到更大的脚本中。
那么我就厌倦了下面...
import urllib2
import xml.etree.ElementTree as ET
import time
from neopixel import *
import sys
import os
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(4,GPIO.IN)
if GPIO.input(4) == 1: "LED_BRIGHTNESS = 75"
if GPIO.input(4) == 0: "LED_BRIGHTNESS = 255"
# LED strip configuration:
LED_COUNT = 95 # Number of LED pixels.
LED_PIN = 18 # GPIO pin connected to the pixels (18 uses PWM!).
#LED_PIN = 10 # GPIO pin connected to the pixels (10 uses SPI /dev/spidev0.0).
LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz)
LED_DMA = 5 # DMA channel to use for generating signal (try 5)
#LED_BRIGHTNESS = 255 # Set to 0 for darkest and 255 for brightest
LED_INVERT = False # True to invert the signal (when using NPN transistor level shift)
LED_CHANNEL = 0 # set to '1' for GPIOs 13, 19, 41, 45 or 53
LED_STRIP = ws.WS2811_STRIP_GRB # Strip type and colour ordering
这将返回以下错误。
Traceback (most recent call last):
File "metar.py", line 30, in <module>
strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL, LED_STRIP)
NameError: name 'LED_BRIGHTNESS' is not defined
我在这方面太陌生了,我什至不知道在尝试用谷歌搜索问题时使用什么正确的术语。我尝试研究如何将 if 语句输出打印到字符串中,或者如何将 if 语句输出到字符串中,并且我一直在寻找完全预定义的值而不是来自传感器 if 语句的东西。
如果有人能指出我正确的方向,我将不胜感激。
【问题讨论】:
-
您的意思是不是只想将您的代码包装在一个字符串中...?
-
我想是的。我需要它输出,因此 LED_BRIGHTNESS 值基于传感器输入。
-
您只想动态设置变量?它只是
LED_BRIGHTNESS = 75 if GPIO.input(4) == 1 else 255。 -
谢谢,我认为这会很愚蠢,只是这里没有线索。非常感谢您的时间。
标签: python if-statement input sensors