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)