电路| Trackback Valueerror | > button_a正在使用<
上下文
我希望我能得到一些帮助。预先感谢
我最近获得了一个大学的电路游乐场快车作为大学的项目,并且我正在遇到一些问题,特别是因为追溯不断给我一个错误,我不知道该如何解决,我已经尝试了现在有几件事,没有什么不同。
我正在为电路python使用IDE,称为“ MU编辑器” https://codewith.mu/en/nodload ,这为我提供了一些有趣的工具,使我的工作变得更加容易。 (我必须澄清一下我从未在python中编码,所以我有点迷失在所有内容中)
错误
Traceback (ultima llamada reciente):
Archivo "code.py", línea 61, en <module>
ValueError: BUTTON_A está siendo utilizado
El código terminó de ejecutar.
代码,这是错误的代码,
我在这些代码上特别遇到了问题:
- 可变a_but ,它将请允许我使用电路游乐场的按钮_a,因此按下
a_but = DigitalInOut(board.D4)
a_but.direction = Direction.INPUT
a_but.pull = Pull.DOWN
- actible b_but 时,它可以做一些事情,这将使我可以使用电路游乐场的button_b,因此在按下时会执行某些操作
b_but = DigitalInOut(board.D5)
b_but.direction = Direction.INPUT
b_but.pull = Pull.DOWN
- 可变像素将使我可以访问
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=1.0)
使用时上述变量的电路游乐场 示例:
- a_but 示例b_but
while True:
if a_but.value:
print("Button A pressed")
if b_but.value:
print("Button B pressed")
- 像素
if temp < 36:
pixels[0] = (0, 0, 255)
pixels[1] = (0, 0, 255)
pixels[2] = (0, 0, 255)
elif temp >= 36 and temp <= 37.2:
pixels[3] = (0, 0, 255)
pixels[4] = (0, 0, 255)
pixels[5] = (0, 0, 255)
pixels[6] = (0, 0, 255)
else:
pixels[7] = (0, 0, 255)
pixels[8] = (0, 0, 255)
pixels[9] = (0, 0, 255)
在您想查看的情况下,这是完整的代码(这可能会有一些错误,因为该错误阻止了我继续前进):
import time
import analogio
import board
import neopixel
import pulseio
import digitalio
from digitalio import DigitalInOut, Direction, Pull
import adafruit_irremote
import adafruit_thermistor
from adafruit_circuitplayground import cp
def sign(value):
if value > 0:
return 1
if value < 0:
return -1
return 0
def pulse_test():
# Turn only pixel #1 green
pixels[1] = (0, 255, 0)
# How many light readings per sample
NUM_OVERSAMPLE = 10
# How many samples we take to calculate 'average'
NUM_SAMPLES = 20
samples = [0] * NUM_SAMPLES
i = True
while i:
for i in range(NUM_SAMPLES):
# Take NUM_OVERSAMPLE number of readings really fast
oversample = 0
for s in range(NUM_OVERSAMPLE):
oversample += float(light.value)
# and save the aver age from the oversamples
samples[i] = oversample / NUM_OVERSAMPLE # Find the average
mean = sum(samples) / float(len(samples)) # take the average
print((samples[i] - mean,)) # 'center' the reading
if i > 0:
# If the sign of the data has changed munus to plus
# we have one full waveform (2 zero crossings), pulse LED
if sign(samples[i] - mean) <= 0 and sign(samples[i - 1] - mean) > 0:
pixels[9] = (200, 0, 0) # Pulse LED
else:
pixels[9] = (0, 0, 0) # Turn LED off
time.sleep(0.025) # change to go faster/slower
if b_but.value:
i = False
pixels[1] = (0, 0, 0)
pixels[9] = (0, 0, 0)
a_but = DigitalInOut(board.D4)
a_but.direction = Direction.INPUT
a_but.pull = Pull.DOWN
b_but = DigitalInOut(board.D5)
b_but.direction = Direction.INPUT
b_but.pull = Pull.DOWN
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=1.0)
light = analogio.AnalogIn(board.LIGHT)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
GREEN = (0, 255, 0)
CYAN = (0, 255, 255)
BLUE = (0, 0, 255)
PURPLE = (180, 0, 255)
PIXELS_OFF = (0, 0, 0)
pulsein = pulseio.PulseIn(board.IR_RX, maxlen=120, idle_state=True)
decoder = adafruit_irremote.GenericDecode()
while True:
pulse = decoder.read_pulses(pulsein)
try:
received_code = decoder.decode_bits(pulse)
print("\n\nSeñal Adquirida : ", received_code)
if received_code == (223, 32, 111, 144):
print("¡Sospecha de infeccion detectada!\n\n")
for i in range(3):
pixels.fill(YELLOW)
pixels.show()
time.sleep(0.05)
pixels.fill(PIXELS_OFF)
pixels.show()
time.sleep(0.05)
print("¿Desea realizar un testeo basico?:")
print("\n\n Si. Presione A\n No. Presione B")
pixels[0] = (0, 255, 0)
pixels[1] = (0, 255, 0)
pixels[2] = (0, 255, 0)
pixels[3] = (0, 255, 0)
pixels[4] = (0, 255, 0)
pixels[5] = (255, 0, 0)
pixels[6] = (255, 0, 0)
pixels[7] = (255, 0, 0)
pixels[8] = (255, 0, 0)
pixels[9] = (255, 0, 0)
while True:
if a_but.value:
testeo_respuesta = True
for x in range(2):
pixels.fill(PIXELS_OFF)
pixels.show()
for i in range(9):
pixels[10] = (0, 255, 0)
time.sleep(0.075)
break
if b_but.value:
testeo_respuesta = False
for x in range(2):
pixels.fill(PIXELS_OFF)
pixels.show()
for i in range(9):
pixels[i] = (255, 0, 0)
time.sleep(0.075)
break
time.sleep(0.075)
pixels.fill(PIXELS_OFF)
pixels.show()
if testeo_respuesta:
for test_num in range(19):
regTemp = [cp.temperature] * test_num
time.sleep(1.0)
for k in range(19):
pixels[0] = (0, 0, 255)
temp = 0
if temp < regTemp[k]:
temp = regTemp[k]
pixels[0] = (0, 0, 0)
time.sleep(0.5)
if temp < 36:
pixels[0] = (0, 0, 255)
pixels[1] = (0, 0, 255)
pixels[2] = (0, 0, 255)
pixels[3] = (0, 0, 0)
pixels[4] = (0, 0, 0)
pixels[5] = (0, 0, 0)
pixels[6] = (0, 0, 0)
pixels[7] = (0, 0, 0)
pixels[8] = (0, 0, 0)
pixels[9] = (0, 0, 0)
resultado = True
elif temp >= 36 and temp <= 37.2:
pixels[0] = (0, 0, 0)
pixels[1] = (0, 0, 0)
pixels[2] = (0, 0, 0)
pixels[3] = (0, 0, 255)
pixels[4] = (0, 0, 255)
pixels[5] = (0, 0, 255)
pixels[6] = (0, 0, 255)
pixels[7] = (0, 0, 0)
pixels[8] = (0, 0, 0)
pixels[9] = (0, 0, 0)
resultado = False
else:
pixels[0] = (0, 0, 0)
pixels[1] = (0, 0, 0)
pixels[2] = (0, 0, 0)
pixels[3] = (0, 0, 0)
pixels[4] = (0, 0, 0)
pixels[5] = (0, 0, 0)
pixels[6] = (0, 0, 0)
pixels[7] = (0, 0, 255)
pixels[8] = (0, 0, 255)
pixels[9] = (0, 0, 255)
resultado = True
else:
print("¡No hay riesgo detectado!")
for i in range(3):
pixels.fill(GREEN)
pixels.show()
time.sleep(0.05)
pixels.fill(PIXELS_OFF)
pixels.show()
time.sleep(0.05)
except:
continue
time.sleep(0.025)
Context
I hope I can get some help. Thanks in advance
I recently acquired a Circuit Playground Express as a project for college, and I am having some issues programming with it, specially because the Traceback is constantly giving me an error which I don't know how to fix, and I have tried several things now, and couldn't get anything different.
I am using a IDE for CircuitPython called "Mu Editor" https://codewith.mu/en/download, which provides me some interesting tools that have made my work kind of easier. (I have to clarify I have never coded in Python, so I am kinda lost in everything)
Error
Traceback (ultima llamada reciente):
Archivo "code.py", línea 61, en <module>
ValueError: BUTTON_A está siendo utilizado
El código terminó de ejecutar.
Code which is getting error
I am having the issue specially on these pieces of code:
- Variable a_but, which will allow me to use the BUTTON_A of the Circuit Playground, so it does something when it is pressed
a_but = DigitalInOut(board.D4)
a_but.direction = Direction.INPUT
a_but.pull = Pull.DOWN
- Variable b_but, which will allow me to use the BUTTON_B of the Circuit Playground, so it does something when it is pressed
b_but = DigitalInOut(board.D5)
b_but.direction = Direction.INPUT
b_but.pull = Pull.DOWN
- Variable pixels that will allow me to access to modify the neopixels of the Circuit Playground
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=1.0)
Examples of the above variables when being used:
- a_but and b_but
while True:
if a_but.value:
print("Button A pressed")
if b_but.value:
print("Button B pressed")
- pixels
if temp < 36:
pixels[0] = (0, 0, 255)
pixels[1] = (0, 0, 255)
pixels[2] = (0, 0, 255)
elif temp >= 36 and temp <= 37.2:
pixels[3] = (0, 0, 255)
pixels[4] = (0, 0, 255)
pixels[5] = (0, 0, 255)
pixels[6] = (0, 0, 255)
else:
pixels[7] = (0, 0, 255)
pixels[8] = (0, 0, 255)
pixels[9] = (0, 0, 255)
Here is the complete code in case you want to take a look (It might have some errors since that error is blocking me from moving on):
import time
import analogio
import board
import neopixel
import pulseio
import digitalio
from digitalio import DigitalInOut, Direction, Pull
import adafruit_irremote
import adafruit_thermistor
from adafruit_circuitplayground import cp
def sign(value):
if value > 0:
return 1
if value < 0:
return -1
return 0
def pulse_test():
# Turn only pixel #1 green
pixels[1] = (0, 255, 0)
# How many light readings per sample
NUM_OVERSAMPLE = 10
# How many samples we take to calculate 'average'
NUM_SAMPLES = 20
samples = [0] * NUM_SAMPLES
i = True
while i:
for i in range(NUM_SAMPLES):
# Take NUM_OVERSAMPLE number of readings really fast
oversample = 0
for s in range(NUM_OVERSAMPLE):
oversample += float(light.value)
# and save the aver age from the oversamples
samples[i] = oversample / NUM_OVERSAMPLE # Find the average
mean = sum(samples) / float(len(samples)) # take the average
print((samples[i] - mean,)) # 'center' the reading
if i > 0:
# If the sign of the data has changed munus to plus
# we have one full waveform (2 zero crossings), pulse LED
if sign(samples[i] - mean) <= 0 and sign(samples[i - 1] - mean) > 0:
pixels[9] = (200, 0, 0) # Pulse LED
else:
pixels[9] = (0, 0, 0) # Turn LED off
time.sleep(0.025) # change to go faster/slower
if b_but.value:
i = False
pixels[1] = (0, 0, 0)
pixels[9] = (0, 0, 0)
a_but = DigitalInOut(board.D4)
a_but.direction = Direction.INPUT
a_but.pull = Pull.DOWN
b_but = DigitalInOut(board.D5)
b_but.direction = Direction.INPUT
b_but.pull = Pull.DOWN
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=1.0)
light = analogio.AnalogIn(board.LIGHT)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
GREEN = (0, 255, 0)
CYAN = (0, 255, 255)
BLUE = (0, 0, 255)
PURPLE = (180, 0, 255)
PIXELS_OFF = (0, 0, 0)
pulsein = pulseio.PulseIn(board.IR_RX, maxlen=120, idle_state=True)
decoder = adafruit_irremote.GenericDecode()
while True:
pulse = decoder.read_pulses(pulsein)
try:
received_code = decoder.decode_bits(pulse)
print("\n\nSeñal Adquirida : ", received_code)
if received_code == (223, 32, 111, 144):
print("¡Sospecha de infeccion detectada!\n\n")
for i in range(3):
pixels.fill(YELLOW)
pixels.show()
time.sleep(0.05)
pixels.fill(PIXELS_OFF)
pixels.show()
time.sleep(0.05)
print("¿Desea realizar un testeo basico?:")
print("\n\n Si. Presione A\n No. Presione B")
pixels[0] = (0, 255, 0)
pixels[1] = (0, 255, 0)
pixels[2] = (0, 255, 0)
pixels[3] = (0, 255, 0)
pixels[4] = (0, 255, 0)
pixels[5] = (255, 0, 0)
pixels[6] = (255, 0, 0)
pixels[7] = (255, 0, 0)
pixels[8] = (255, 0, 0)
pixels[9] = (255, 0, 0)
while True:
if a_but.value:
testeo_respuesta = True
for x in range(2):
pixels.fill(PIXELS_OFF)
pixels.show()
for i in range(9):
pixels[10] = (0, 255, 0)
time.sleep(0.075)
break
if b_but.value:
testeo_respuesta = False
for x in range(2):
pixels.fill(PIXELS_OFF)
pixels.show()
for i in range(9):
pixels[i] = (255, 0, 0)
time.sleep(0.075)
break
time.sleep(0.075)
pixels.fill(PIXELS_OFF)
pixels.show()
if testeo_respuesta:
for test_num in range(19):
regTemp = [cp.temperature] * test_num
time.sleep(1.0)
for k in range(19):
pixels[0] = (0, 0, 255)
temp = 0
if temp < regTemp[k]:
temp = regTemp[k]
pixels[0] = (0, 0, 0)
time.sleep(0.5)
if temp < 36:
pixels[0] = (0, 0, 255)
pixels[1] = (0, 0, 255)
pixels[2] = (0, 0, 255)
pixels[3] = (0, 0, 0)
pixels[4] = (0, 0, 0)
pixels[5] = (0, 0, 0)
pixels[6] = (0, 0, 0)
pixels[7] = (0, 0, 0)
pixels[8] = (0, 0, 0)
pixels[9] = (0, 0, 0)
resultado = True
elif temp >= 36 and temp <= 37.2:
pixels[0] = (0, 0, 0)
pixels[1] = (0, 0, 0)
pixels[2] = (0, 0, 0)
pixels[3] = (0, 0, 255)
pixels[4] = (0, 0, 255)
pixels[5] = (0, 0, 255)
pixels[6] = (0, 0, 255)
pixels[7] = (0, 0, 0)
pixels[8] = (0, 0, 0)
pixels[9] = (0, 0, 0)
resultado = False
else:
pixels[0] = (0, 0, 0)
pixels[1] = (0, 0, 0)
pixels[2] = (0, 0, 0)
pixels[3] = (0, 0, 0)
pixels[4] = (0, 0, 0)
pixels[5] = (0, 0, 0)
pixels[6] = (0, 0, 0)
pixels[7] = (0, 0, 255)
pixels[8] = (0, 0, 255)
pixels[9] = (0, 0, 255)
resultado = True
else:
print("¡No hay riesgo detectado!")
for i in range(3):
pixels.fill(GREEN)
pixels.show()
time.sleep(0.05)
pixels.fill(PIXELS_OFF)
pixels.show()
time.sleep(0.05)
except:
continue
time.sleep(0.025)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论